add some useful func
This commit is contained in:
		
							parent
							
								
									52ebaece73
								
							
						
					
					
						commit
						a43a1be0b4
					
				
							
								
								
									
										10
									
								
								utils/caller.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								utils/caller.go
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										14
									
								
								utils/caller_test.go
									
									
									
									
									
										Normal 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") | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @ -43,7 +43,7 @@ func LookFile(filename string, paths ...string) (fullpath string, err error) { | |||||||
| // like command grep -E | // like command grep -E | ||||||
| // for example: GrepE(`^hello`, "hello.txt") | // for example: GrepE(`^hello`, "hello.txt") | ||||||
| // \n is striped while read | // \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) | 	re, err := regexp.Compile(patten) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return | 		return | ||||||
|  | |||||||
| @ -22,7 +22,7 @@ func TestSelfDir(t *testing.T) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestFileExists(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") | 		t.Errorf("/bin/echo should exists, but it didn't") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -44,14 +44,14 @@ func TestLookFile(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestGrepE(t *testing.T) { | func TestGrepFile(t *testing.T) { | ||||||
| 	_, err := GrepE("", noExistedFile) | 	_, err := GrepFile("", noExistedFile) | ||||||
| 	if err == nil { | 	if err == nil { | ||||||
| 		t.Error("expect file-not-existed error, but got nothing") | 		t.Error("expect file-not-existed error, but got nothing") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	path := filepath.Join(".", "testdata", "grepe.test") | 	path := filepath.Join(".", "testdata", "grepe.test") | ||||||
| 	lines, err := GrepE(`^\s*[^#]+`, path) | 	lines, err := GrepFile(`^\s*[^#]+`, path) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Error(err) | 		t.Error(err) | ||||||
| 	} | 	} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user