Commit 6cabc980 authored by Kirill Smelkov's avatar Kirill Smelkov

my: Add File, Line and Frame

To determine current function's file name, line number and runtime.Frame
parent cf55754e
......@@ -60,3 +60,20 @@ func PkgName() string {
}
return myfunc[:iafterslash+idot]
}
// File returns path of currently running function's file
func File() string {
f := _myframe(3)
return f.File
}
// Line returns currently running function's line
func Line() int {
f := _myframe(3)
return f.Line
}
// Frame returns currently running functions's frame
func Frame() runtime.Frame {
return _myframe(3)
}
......@@ -22,6 +22,16 @@ import (
"testing"
)
// goes first not to be broken by other edits
func TestMyLine(t *testing.T) {
myline := Line()
want := 27
if myline != want {
t.Errorf("my.Line() -> %v ; want %v", myline, want)
}
}
func TestMyFuncName(t *testing.T) {
myfunc := FuncName()
// go test changes full package name (putting filesystem of the tree into it)
......@@ -31,3 +41,13 @@ func TestMyFuncName(t *testing.T) {
t.Errorf("my.FuncName() -> %v ; want *%v", myfunc, wantsuffix)
}
}
// XXX how to test PkgName? (go test changes full package name - see ^^^)
func TestMyFile(t *testing.T) {
myfile := File()
wantsuffix := "my_test.go"
if !strings.HasSuffix(myfile, wantsuffix) {
t.Errorf("my.File() -> %v ; want *%v", myfile, wantsuffix)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment