a43a1be0 by skyblue

add some useful func

1 parent 52ebaece
1 package utils
2
3 import (
4 "reflect"
5 "runtime"
6 )
7
8 func GetFuncName(i interface{}) string {
9 return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
10 }
1 package utils
2
3 import (
4 "strings"
5 "testing"
6 )
7
8 func TestGetFuncName(t *testing.T) {
9 name := GetFuncName(TestGetFuncName)
10 t.Log(name)
11 if !strings.HasSuffix(name, ".TestGetFuncName") {
12 t.Error("get func name error")
13 }
14 }
...@@ -43,7 +43,7 @@ func LookFile(filename string, paths ...string) (fullpath string, err error) { ...@@ -43,7 +43,7 @@ func LookFile(filename string, paths ...string) (fullpath string, err error) {
43 // like command grep -E 43 // like command grep -E
44 // for example: GrepE(`^hello`, "hello.txt") 44 // for example: GrepE(`^hello`, "hello.txt")
45 // \n is striped while read 45 // \n is striped while read
46 func GrepE(patten string, filename string) (lines []string, err error) { 46 func GrepFile(patten string, filename string) (lines []string, err error) {
47 re, err := regexp.Compile(patten) 47 re, err := regexp.Compile(patten)
48 if err != nil { 48 if err != nil {
49 return 49 return
......
...@@ -22,7 +22,7 @@ func TestSelfDir(t *testing.T) { ...@@ -22,7 +22,7 @@ func TestSelfDir(t *testing.T) {
22 } 22 }
23 23
24 func TestFileExists(t *testing.T) { 24 func TestFileExists(t *testing.T) {
25 if !FileExists("/bin/echo") { 25 if !FileExists("./file.go") {
26 t.Errorf("/bin/echo should exists, but it didn't") 26 t.Errorf("/bin/echo should exists, but it didn't")
27 } 27 }
28 28
...@@ -44,14 +44,14 @@ func TestLookFile(t *testing.T) { ...@@ -44,14 +44,14 @@ func TestLookFile(t *testing.T) {
44 } 44 }
45 } 45 }
46 46
47 func TestGrepE(t *testing.T) { 47 func TestGrepFile(t *testing.T) {
48 _, err := GrepE("", noExistedFile) 48 _, err := GrepFile("", noExistedFile)
49 if err == nil { 49 if err == nil {
50 t.Error("expect file-not-existed error, but got nothing") 50 t.Error("expect file-not-existed error, but got nothing")
51 } 51 }
52 52
53 path := filepath.Join(".", "testdata", "grepe.test") 53 path := filepath.Join(".", "testdata", "grepe.test")
54 lines, err := GrepE(`^\s*[^#]+`, path) 54 lines, err := GrepFile(`^\s*[^#]+`, path)
55 if err != nil { 55 if err != nil {
56 t.Error(err) 56 t.Error(err)
57 } 57 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!