-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_test.go
97 lines (85 loc) · 1.85 KB
/
assert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package flowtest
import (
"fmt"
"testing"
)
type testWrap struct {
failed bool
message string
}
func (t *testWrap) Helper() {
}
func (t *testWrap) Fatal(args ...interface{}) {
t.failed = true
t.message = fmt.Sprint(args...)
}
func TestEquals(t *testing.T) {
for _, tc := range []struct {
A interface{}
B interface{}
Equal bool
}{
{A: "foo", B: "foo", Equal: true},
{A: "foo", B: "bar", Equal: false},
{A: 1, B: 1, Equal: true},
{A: 1, B: 2, Equal: false},
{A: nil, B: nil, Equal: true},
{A: nil, B: 1, Equal: false},
{A: 1, B: nil, Equal: false},
} {
t.Run(fmt.Sprintf("%v == %v", tc.A, tc.B), func(t *testing.T) {
tw := &testWrap{}
a := &assertion{
fatal: tw.Fatal,
helper: tw.Helper,
}
a.Equal(tc.A, tc.B)
if tc.Equal {
if tw.failed {
t.Errorf("Equals(%#v %T, %#v %T) failed: %s", tc.A, tc.A, tc.B, tc.B, tw.message)
}
} else {
if !tw.failed {
t.Errorf("Equals(%v, %v) did not fail", tc.A, tc.B)
}
}
})
}
}
func TestNotNilHappy(t *testing.T) {
type testStruct struct{}
notNilVals := []interface{}{
"foo",
1,
0,
"",
[]string{"foo"},
[]string{},
[]byte{},
&struct{}{},
testStruct{},
&testStruct{},
}
for idx, val := range notNilVals {
t.Run(fmt.Sprintf("happy case %d", idx), func(t *testing.T) {
if isNil(val) {
t.Errorf("val (%v) assessed as nil", val)
}
})
}
nilVals := []func() interface{}{
func() interface{} { return nil },
func() interface{} { var a *string; return a },
func() interface{} { var a *[]byte; return a },
func() interface{} { var a *struct{}; return a },
func() interface{} { var a *testStruct; return a },
}
for idx, valFunc := range nilVals {
t.Run(fmt.Sprintf("sad case %d", idx), func(t *testing.T) {
val := valFunc()
if !isNil(val) {
t.Errorf("val (%v) assessed as not nil", val)
}
})
}
}