-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_test.go
146 lines (120 loc) · 4.28 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package assert
import (
"errors"
"reflect"
"testing"
)
func TestNoError(t *testing.T) {
check(t, false, func(t testing.TB) { NoError(t, nil) })
check(t, true, func(t testing.TB) { NoError(t, errors.New("some error")) })
}
func TestError(t *testing.T) {
check(t, true, func(t testing.TB) { Error(t, nil) })
check(t, false, func(t testing.TB) { Error(t, errors.New("some error")) })
}
func TestEqual(t *testing.T) {
check(t, false, func(t testing.TB) { Equal(t, nil, nil) })
check(t, true, func(t testing.TB) { Equal(t, nil, 0) })
check(t, true, func(t testing.TB) { Equal(t, 0, nil) })
numericTypes := []reflect.Type{
reflect.TypeOf(int(0)), reflect.TypeOf(uint(0)),
reflect.TypeOf(int8(0)), reflect.TypeOf(uint8(0)),
reflect.TypeOf(int16(0)), reflect.TypeOf(uint16(0)),
reflect.TypeOf(int32(0)), reflect.TypeOf(uint32(0)),
reflect.TypeOf(int64(0)), reflect.TypeOf(uint64(0)),
}
for _, t1 := range numericTypes {
for _, t2 := range numericTypes {
v1 := reflect.New(t1).Elem().Interface()
v2 := reflect.New(t2).Elem().Interface()
check(t, false, func(t testing.TB) { Equal(t, v1, v2) })
}
}
type (
b bool
s string
f32 float32
f64 float64
c64 complex64
c128 complex128
)
check(t, false, func(t testing.TB) { Equal(t, b(true), true) })
check(t, true, func(t testing.TB) { Equal(t, b(false), true) })
check(t, false, func(t testing.TB) { Equal(t, s("hi"), "hi") })
check(t, true, func(t testing.TB) { Equal(t, s("hi"), "he") })
check(t, false, func(t testing.TB) { Equal(t, f32(1.0), 1.0) })
check(t, false, func(t testing.TB) { Equal(t, f64(1.0), 1.0) })
check(t, false, func(t testing.TB) { Equal(t, f32(1.0), f64(1.0)) })
check(t, true, func(t testing.TB) { Equal(t, f32(1.0), 0.0) })
check(t, true, func(t testing.TB) { Equal(t, f64(1.0), 0.0) })
check(t, true, func(t testing.TB) { Equal(t, f32(1.0), f64(0.0)) })
check(t, false, func(t testing.TB) { Equal(t, c64(1.0+1.0i), 1.0+1.0i) })
check(t, false, func(t testing.TB) { Equal(t, c128(1.0+1.0i), 1.0+1.0i) })
check(t, false, func(t testing.TB) { Equal(t, c64(1.0+1.0i), c128(1.0+1.0i)) })
check(t, true, func(t testing.TB) { Equal(t, c64(1.0+1.0i), 1.0) })
check(t, true, func(t testing.TB) { Equal(t, c128(1.0+1.0i), 1.0) })
check(t, true, func(t testing.TB) { Equal(t, c64(1.0+1.0i), c128(1.0)) })
check(t, false, func(t testing.TB) { Equal(t, []byte("hi"), []byte("hi")) })
check(t, true, func(t testing.TB) { Equal(t, []byte("hi"), []byte("he")) })
}
func TestDeepEqual(t *testing.T) {
check(t, false, func(t testing.TB) { DeepEqual(t, []byte("hi"), []byte("hi")) })
check(t, true, func(t testing.TB) { DeepEqual(t, []byte("hi"), []byte("he")) })
check(t, true, func(t testing.TB) { DeepEqual(t, int(1), uint(1)) })
}
func TestThat(t *testing.T) {
check(t, false, func(t testing.TB) { That(t, true) })
check(t, true, func(t testing.TB) { That(t, false) })
}
func TestTrue(t *testing.T) {
check(t, false, func(t testing.TB) { True(t, true) })
check(t, true, func(t testing.TB) { True(t, false) })
}
func TestFalse(t *testing.T) {
check(t, false, func(t testing.TB) { False(t, false) })
check(t, true, func(t testing.TB) { False(t, true) })
}
func TestNil(t *testing.T) {
check(t, false, func(t testing.TB) { Nil(t, nil) })
check(t, false, func(t testing.TB) { Nil(t, (*int)(nil)) })
check(t, true, func(t testing.TB) { Nil(t, new(int)) })
check(t, true, func(t testing.TB) { Nil(t, 1) })
}
func TestNotNil(t *testing.T) {
check(t, true, func(t testing.TB) { NotNil(t, nil) })
check(t, true, func(t testing.TB) { NotNil(t, (*int)(nil)) })
check(t, false, func(t testing.TB) { NotNil(t, new(int)) })
check(t, false, func(t testing.TB) { NotNil(t, 1) })
}
//
// helpers
//
var sentinel = new(byte)
type recordingTB struct {
testing.TB // must embed in order to implement
failed bool
}
func (tb *recordingTB) Fatal(args ...interface{}) {
tb.failed = true
panic(sentinel)
}
func (tb *recordingTB) Fatalf(format string, args ...interface{}) {
tb.failed = true
panic(sentinel)
}
func (tb *recordingTB) Helper() {}
func check(t testing.TB, failed bool, fn func(t testing.TB)) {
rec := new(recordingTB)
func() {
defer func() {
if rec := recover(); rec != nil && rec != sentinel {
panic(rec)
}
}()
fn(rec)
}()
if rec.failed != failed {
t.Helper()
t.Fatal("failed does not match")
}
}