Move testing utils to its own package
So as not to pollute command-line flags with test.* options. It appears that importing "testing" (from stdlib) will add flags. To avoid this, we need code to not depend on testing. But, since testing_utils was previously in the same package as the rest of the code, it dragged testing in. Renaming testing_util.go to e.g., util_for_test.go might be enough to avoid it being compiled with the regular code. We may want it to be available to tests for other packages -- after all it is utilities for testing -- so it goes in its own package.
This commit is contained in:
1 parent
f70dfe9de8
commit
97bece2d7b
6 files changed
+6
-6
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func CallSite() string {
|
||||
_, file, line, ok := runtime.Caller(2)
|
||||
if ok {
|
||||
// Truncate file name at last file name separator.
|
||||
if index := strings.LastIndex(file, "/"); index >= 0 {
|
||||
file = file[index+1:]
|
||||
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
|
||||
file = file[index+1:]
|
||||
}
|
||||
} else {
|
||||
file = "???"
|
||||
line = 1
|
||||
}
|
||||
return fmt.Sprintf("%s:%d: ", file, line)
|
||||
}
|
||||
|
||||
func AssertNoErr(t *testing.T, err error) {
|
||||
if err != nil {
|
||||
t.Fatal(CallSite(), err)
|
||||
}
|
||||
}
|
||||
|
||||
func AssertEqualInt(t *testing.T, got, wanted int, desc string) {
|
||||
if got != wanted {
|
||||
t.Fatalf("%s: Expected %s %d but got %d", CallSite(), desc, wanted, got)
|
||||
}
|
||||
}
|
||||
|
||||
func AssertEqualString(t *testing.T, got, wanted string, desc string) {
|
||||
if got != wanted {
|
||||
t.Fatalf("%s: Expected %s '%s' but got '%s'", CallSite(), desc, wanted, got)
|
||||
}
|
||||
}
|
||||
|
||||
func AssertStatus(t *testing.T, got int, wanted int, desc string) {
|
||||
if got != wanted {
|
||||
t.Fatalf("%s: Expected %s %d but got %d", CallSite(), desc, wanted, got)
|
||||
}
|
||||
}
|
||||
|
||||
func AssertErrorInterface(t *testing.T, got interface{}, wanted interface{}, desc string) {
|
||||
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
|
||||
if !gotT.Implements(wantedT) {
|
||||
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
|
||||
}
|
||||
}
|
||||
|
||||
func AssertErrorType(t *testing.T, got interface{}, wanted interface{}, desc string) {
|
||||
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted).Elem()
|
||||
if gotT != wantedT {
|
||||
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
|
||||
}
|
||||
}
|
||||
|
||||
func AssertType(t *testing.T, got interface{}, wanted interface{}, desc string) {
|
||||
gotT, wantedT := reflect.TypeOf(got), reflect.TypeOf(wanted)
|
||||
if gotT != wantedT {
|
||||
t.Fatalf("%s: Expected %s but got %s (%s)", CallSite(), wantedT.String(), gotT.String(), desc)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user