-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_test.go
146 lines (118 loc) · 3.21 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_test
import (
"fmt"
"github.com/waybeams/assert"
"testing"
)
type foo struct{}
type fakeConst int
const (
fakeConstValueA = iota
fakeConstValueB
)
func ensurePanicWith(expectedMessage string) func() {
return func() {
if r := recover(); r != nil {
if r != expectedMessage {
panic(fmt.Sprintf("Received unexpected panic: %v", r))
}
} else {
panic(fmt.Sprintf("Expected panic (%v) but did not receive one", expectedMessage))
}
}
}
func TestPanicAsserts(t *testing.T) {
t.Run("Match", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
assert.Match("foo", "sdffoosdf")
})
t.Run("Failure", func(t *testing.T) {
defer ensurePanicWith("Expected: \"foo\", but received: \"sdf\"")()
assert.Match("foo", "sdf")
})
})
t.Run("NotNil", func(t *testing.T) {
// The following is NOT TRUE: returnNil() != nil
// This makes me such a sad panda.
var returnNil = func() *foo {
return nil
}
t.Run("Object results should be nil", func(t *testing.T) {
t.Skip("Please help make the following call fail (and the test pass)")
defer ensurePanicWith("Expected nil return value to fail NoNil check")()
assert.NotNil(returnNil())
})
t.Run("Failure", func(t *testing.T) {
defer ensurePanicWith("Expected <nil> to not be nil")()
assert.NotNil(nil)
})
t.Run("Success", func(t *testing.T) {
assert.NotNil(true)
})
})
t.Run("Failure message", func(t *testing.T) {
defer ensurePanicWith("Expected <nil> to not be nil")()
assert.NotNil(nil)
})
t.Run("Nil", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
assert.Nil(nil)
})
t.Run("Failure", func(t *testing.T) {
defer ensurePanicWith("Expected true of type: bool to be nil")()
assert.Nil(true)
})
})
t.Run("True", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
assert.True(true)
})
t.Run("Failure", func(t *testing.T) {
defer ensurePanicWith("Expected false to be true")()
assert.True(false)
})
})
t.Run("False", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
assert.False(false)
})
t.Run("Failure message", func(t *testing.T) {
defer ensurePanicWith("Expected true to be false")()
assert.False(true)
})
})
t.Run("StrictEqual", func(t *testing.T) {
t.Run("Success", func(t *testing.T) {
assert.StrictEqual(0.0, 0.0)
})
t.Run("Failure", func(t *testing.T) {
defer ensurePanicWith("Expected 0 to STRICTLY equal 0")()
assert.StrictEqual(0.0, 0)
})
})
t.Run("Equal", func(t *testing.T) {
t.Run("0.0 == 0.0", func(t *testing.T) {
assert.Equal(0.0, 0.0)
})
t.Run("0.0 == 0", func(t *testing.T) {
assert.Equal(0.0, 0)
})
t.Run("0 == 0.0", func(t *testing.T) {
assert.Equal(0, 0.0)
})
t.Run("0 == 0", func(t *testing.T) {
assert.Equal(0, 0)
})
t.Run("Enum values match", func(t *testing.T) {
assert.Equal(fakeConstValueB, fakeConstValueB)
})
t.Run("Enum values mismatch", func(t *testing.T) {
defer ensurePanicWith("Expected 0 to equal 1")()
assert.Equal(fakeConstValueA, fakeConstValueB)
})
t.Run("failure with custom message", func(t *testing.T) {
defer ensurePanicWith("Expected 1 to equal 2. Fake custom message")()
assert.Equal(1, 2, "Fake custom message")
})
})
}