add some useful func

This commit is contained in:
skyblue 2013-12-11 22:18:45 +08:00
parent 52ebaece73
commit a43a1be0b4
4 changed files with 29 additions and 5 deletions

10
utils/caller.go Normal file
View File

@ -0,0 +1,10 @@
package utils
import (
"reflect"
"runtime"
)
func GetFuncName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

14
utils/caller_test.go Normal file
View File

@ -0,0 +1,14 @@
package utils
import (
"strings"
"testing"
)
func TestGetFuncName(t *testing.T) {
name := GetFuncName(TestGetFuncName)
t.Log(name)
if !strings.HasSuffix(name, ".TestGetFuncName") {
t.Error("get func name error")
}
}

View File

@ -43,7 +43,7 @@ func LookFile(filename string, paths ...string) (fullpath string, err error) {
// like command grep -E
// for example: GrepE(`^hello`, "hello.txt")
// \n is striped while read
func GrepE(patten string, filename string) (lines []string, err error) {
func GrepFile(patten string, filename string) (lines []string, err error) {
re, err := regexp.Compile(patten)
if err != nil {
return

View File

@ -22,7 +22,7 @@ func TestSelfDir(t *testing.T) {
}
func TestFileExists(t *testing.T) {
if !FileExists("/bin/echo") {
if !FileExists("./file.go") {
t.Errorf("/bin/echo should exists, but it didn't")
}
@ -44,14 +44,14 @@ func TestLookFile(t *testing.T) {
}
}
func TestGrepE(t *testing.T) {
_, err := GrepE("", noExistedFile)
func TestGrepFile(t *testing.T) {
_, err := GrepFile("", noExistedFile)
if err == nil {
t.Error("expect file-not-existed error, but got nothing")
}
path := filepath.Join(".", "testdata", "grepe.test")
lines, err := GrepE(`^\s*[^#]+`, path)
lines, err := GrepFile(`^\s*[^#]+`, path)
if err != nil {
t.Error(err)
}