Skip to content

Commit 1f9aa4b

Browse files
authored
Allow <nil> in AssertEqual (#4)
* Update utils.go * Update utils_test.go
1 parent 889adf2 commit 1f9aa4b

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

utils.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package utils
77
import (
88
"bytes"
99
"fmt"
10+
"log"
1011
"os"
1112
"path/filepath"
1213
"reflect"
@@ -139,11 +140,16 @@ func AssertEqual(t testing.TB, a interface{}, b interface{}, description ...stri
139140
bType = reflect.TypeOf(b).Name()
140141
}
141142

143+
testName := "AssertEqual"
144+
if t != nil {
145+
testName = t.Name()
146+
}
147+
142148
_, file, line, _ := runtime.Caller(1)
143149

144150
var buf bytes.Buffer
145151
w := tabwriter.NewWriter(&buf, 0, 0, 5, ' ', 0)
146-
fmt.Fprintf(w, "\nTest:\t%s", t.Name())
152+
fmt.Fprintf(w, "\nTest:\t%s", testName)
147153
fmt.Fprintf(w, "\nTrace:\t%s:%d", filepath.Base(file), line)
148154
fmt.Fprintf(w, "\nError:\tNot equal")
149155
fmt.Fprintf(w, "\nExpect:\t%v\t[%s]", a, aType)
@@ -153,11 +159,16 @@ func AssertEqual(t testing.TB, a interface{}, b interface{}, description ...stri
153159
fmt.Fprintf(w, "\nDescription:\t%s", description[0])
154160
}
155161

156-
err := w.Flush()
157-
if err != nil {
158-
t.Fatal(err)
162+
result := ""
163+
if err := w.Flush(); err != nil {
164+
result = err.Error()
165+
} else {
166+
result = buf.String()
167+
}
168+
if t != nil {
169+
t.Fatal(result)
159170
} else {
160-
t.Fatal(buf.String())
171+
log.Fatal(result)
161172
}
162173
}
163174

utils_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,9 @@ func Test_Utils_StatusMessage(t *testing.T) {
169169
res = StatusMessage(1337)
170170
AssertEqual(t, "", res)
171171
}
172+
173+
func Test_Utils_AssertEqual(t *testing.T) {
174+
t.Parallel()
175+
AssertEqual(nil, []string{}, []string{})
176+
AssertEqual(t, []string{}, []string{})
177+
}

0 commit comments

Comments
 (0)