github.com/qiniu/x@v1.11.9/ts/testing.go (about) 1 package ts 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "runtime" 8 "strconv" 9 "testing" 10 ) 11 12 const logStackLevel = 2 13 14 func log(skip int, t *testing.T, args ...interface{}) { 15 16 _, file, line, _ := runtime.Caller(skip) 17 _, fname := filepath.Split(file) 18 args1 := make([]interface{}, len(args)+1) 19 args1[0] = fname + ":" + strconv.Itoa(line) + ":" 20 copy(args1[1:], args) 21 22 if os.PathSeparator == '/' { 23 fmt.Fprintln(os.Stdout, args1...) 24 } else { 25 t.Log(args1...) 26 } 27 } 28 29 func logf(skip int, t *testing.T, format string, args ...interface{}) { 30 31 _, file, line, _ := runtime.Caller(skip) 32 _, fname := filepath.Split(file) 33 args2 := make([]interface{}, len(args)+2) 34 args2[0] = fname 35 args2[1] = line 36 copy(args2[2:], args) 37 38 if os.PathSeparator == '/' { 39 fmt.Fprintf(os.Stderr, "%s:%d: "+format+"\n", args2...) 40 } else { 41 t.Logf("%s:%d: "+format, args2...) 42 } 43 } 44 45 // Log formats its arguments using default formatting, analogous to Print(), 46 // and records the text in the error log. 47 func Log(t *testing.T, args ...interface{}) { 48 log(logStackLevel, t, args...) 49 } 50 51 // Logf formats its arguments according to the format, analogous to Printf(), 52 // and records the text in the error log. 53 func Logf(t *testing.T, format string, args ...interface{}) { 54 logf(logStackLevel, t, format, args...) 55 } 56 57 // Error is equivalent to Log() followed by Fail(). 58 func Error(t *testing.T, args ...interface{}) { 59 log(logStackLevel, t, args...) 60 t.Fail() 61 } 62 63 // Errorf is equivalent to Logf() followed by Fail(). 64 func Errorf(t *testing.T, format string, args ...interface{}) { 65 logf(logStackLevel, t, format, args...) 66 t.Fail() 67 } 68 69 // Fatal is equivalent to Log() followed by FailNow(). 70 func Fatal(t *testing.T, args ...interface{}) { 71 log(logStackLevel, t, args...) 72 t.FailNow() 73 } 74 75 // Fatalf is equivalent to Logf() followed by FailNow(). 76 func Fatalf(t *testing.T, format string, args ...interface{}) { 77 logf(logStackLevel, t, format, args...) 78 t.FailNow() 79 }