Commit a43a1be0 authored by skyblue's avatar skyblue

add some useful func

parent 52ebaece
package utils
import (
"reflect"
"runtime"
)
func GetFuncName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
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")
}
}
......@@ -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
......
......@@ -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)
}
......
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