Skip to content

Commit

Permalink
test: remove caller() helper
Browse files Browse the repository at this point in the history
t.Helper() fixes this issue now, so we don't need to inspect the call
stack ourselves.
  • Loading branch information
kevinburke committed Feb 3, 2021
1 parent 3134fc7 commit d876623
Showing 1 changed file with 12 additions and 33 deletions.
45 changes: 12 additions & 33 deletions test/test-tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,56 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"reflect"
"runtime"
"strings"
"testing"
)

// Return short format caller info for printing errors, so errors don't all
// appear to come from test-tools.go.
func caller() string {
_, file, line, _ := runtime.Caller(2)
splits := strings.Split(file, "/")
filename := splits[len(splits)-1]
return fmt.Sprintf("%s:%d:", filename, line)
}

// Assert a boolean
func Assert(t testing.TB, result bool, message string) {
t.Helper()
if !result {
t.Fatalf("%s %s", caller(), message)
t.Fatal(message)
}
}

// AssertNotNil checks an object to be non-nil
func AssertNotNil(t testing.TB, obj interface{}, message string) {
t.Helper()
if obj == nil {
t.Fatalf("%s %s", caller(), message)
t.Fatal(message)
}
}

// AssertNotError checks that err is nil
func AssertNotError(t testing.TB, err error, message string) {
t.Helper()
if err != nil {
t.Fatalf("%s %s: %s", caller(), message, err)
t.Fatalf("%s: %s", message, err)
}
}

// AssertError checks that err is non-nil
func AssertError(t testing.TB, err error, message string) {
t.Helper()
if err == nil {
t.Fatalf("%s %s: expected error but received none", caller(), message)
t.Fatalf("%s: expected error but received none", message)
}
}

// AssertEquals uses the equality operator (==) to measure one and two
func AssertEquals(t testing.TB, one interface{}, two interface{}) {
t.Helper()
if one != two {
t.Fatalf("%s [%v] != [%v]", caller(), one, two)
t.Fatalf("[%v] != [%v]", one, two)
}
}

// AssertDeepEquals uses the reflect.DeepEqual method to measure one and two
func AssertDeepEquals(t testing.TB, one interface{}, two interface{}) {
t.Helper()
if !reflect.DeepEqual(one, two) {
t.Fatalf("%s [%+v] !(deep)= [%+v]", caller(), one, two)
t.Fatalf("[%+v] !(deep)= [%+v]", one, two)
}
}

Expand All @@ -89,7 +78,7 @@ func AssertMarshaledEquals(t testing.TB, one interface{}, two interface{}) {
AssertNotError(t, err, "Could not marshal 2nd argument")

if !bytes.Equal(oneJSON, twoJSON) {
t.Fatalf("%s [%s] !(json)= [%s]", caller(), oneJSON, twoJSON)
t.Fatalf("[%s] !(json)= [%s]", oneJSON, twoJSON)
}
}

Expand All @@ -98,16 +87,15 @@ func AssertMarshaledEquals(t testing.TB, one interface{}, two interface{}) {
func AssertNotEquals(t testing.TB, one interface{}, two interface{}) {
t.Helper()
if one == two {
t.Fatalf("%s [%v] == [%v]", caller(), one, two)
t.Fatalf("[%v] == [%v]", one, two)
}
}

// AssertByteEquals uses bytes.Equal to measure one and two for equality.
func AssertByteEquals(t testing.TB, one []byte, two []byte) {
t.Helper()
if !bytes.Equal(one, two) {
t.Fatalf("%s Byte [%s] != [%s]",
caller(),
t.Fatalf("Byte [%s] != [%s]",
base64.StdEncoding.EncodeToString(one),
base64.StdEncoding.EncodeToString(two))
}
Expand All @@ -117,32 +105,23 @@ func AssertByteEquals(t testing.TB, one []byte, two []byte) {
func AssertIntEquals(t testing.TB, one int, two int) {
t.Helper()
if one != two {
t.Fatalf("%s Int [%d] != [%d]", caller(), one, two)
}
}

// AssertBigIntEquals uses the big.Int.cmp() method to measure whether
// one and two are equal
func AssertBigIntEquals(t testing.TB, one *big.Int, two *big.Int) {
t.Helper()
if one.Cmp(two) != 0 {
t.Fatalf("%s Int [%d] != [%d]", caller(), one, two)
t.Fatalf("Int [%d] != [%d]", one, two)
}
}

// AssertContains determines whether needle can be found in haystack
func AssertContains(t testing.TB, haystack string, needle string) {
t.Helper()
if !strings.Contains(haystack, needle) {
t.Fatalf("%s String [%s] does not contain [%s]", caller(), haystack, needle)
t.Fatalf("String [%s] does not contain [%s]", haystack, needle)
}
}

// AssertNotContains determines if needle is not found in haystack
func AssertNotContains(t testing.TB, haystack string, needle string) {
t.Helper()
if strings.Contains(haystack, needle) {
t.Fatalf("%s String [%s] contains [%s]", caller(), haystack, needle)
t.Fatalf("String [%s] contains [%s]", haystack, needle)
}
}

Expand Down

0 comments on commit d876623

Please sign in to comment.