Debian Codegraph @

expect_test.go

raw
1 package expect
2
3 import (
4 "fmt"
5 "testing"
6 )
7
8 var originalErrorf = Errorf
9 var beforeEachCalled = 0
10
11 type ExpectTests struct{}
12
13 func init() {
14 BeforeEach(func() {
15 beforeEachCalled++
16 })
17 }
18
19 func Test_Expect(t *testing.T) {
20 Expectify(new(ExpectTests), t)
21 }
22
23 func (_ ExpectTests) Equal_Success() {
24 Expect("abc").To.Equal("abc")
25 Expect(true).To.Equal(true)
26 Expect(false).To.Equal(false)
27 Expect([]int{1, 2, 3}).To.Equal([]int{1, 2, 3})
28 Expect([]bool{true, false, false}).To.Equal([]bool{true, false, false})
29 Expect(map[string]int{"a": 1, "b": 2}).To.Equal(map[string]int{"a": 1, "b": 2})
30 }
31
32 func (_ ExpectTests) Equal_Failures() {
33 failing(`expected "abc" to be equal to "123"`, 1, func() {
34 Expect("abc").To.Equal("123")
35 })
36
37 failing("expected true to be equal to false", 1, func() {
38 Expect(true).To.Equal(false)
39 })
40
41 failing("expected [1 2] to be equal to [1]", 1, func() {
42 Expect([]int{1, 2}).To.Equal([]int{1})
43 })
44
45 // hard to test, the print out is indeterministic, maps aren't sorted
46 // failing("expected map[a:1 b:2] to be equal to map[b:1 a:2]", 1, func(){
47 // Expect(map[string]int{"a":1,"b":2}).Equal.To(map[string]int{"b":1,"a":2})
48 // })
49 }
50
51 func (_ ExpectTests) NotEqual_Success() {
52 Expect("abc").Not.To.Equal("123")
53 }
54
55 func (_ ExpectTests) NoEqual_Failures() {
56 failing(`expected "123" not to equal "123"`, 1, func() {
57 Expect("123").Not.To.Equal("123")
58 })
59
60 failing("expected true not to equal true", 1, func() {
61 Expect(true).Not.To.Equal(true)
62 })
63
64 failing("expected [1 2] not to equal [1 2]", 1, func() {
65 Expect([]int{1, 2}).Not.To.Equal([]int{1, 2})
66 })
67 }
68
69 func (_ ExpectTests) Contain_Success() {
70 Expect("abc").To.Contain("bc")
71 Expect("abc").Not.To.Contain("99")
72
73 Expect([]int{1, 3, 4}).To.Contain(4)
74 Expect([]int{2, 3, 4}).Not.To.Contain(5)
75
76 Expect(map[string]int{"a": 3}).To.Contain("a")
77 Expect(map[string]int{"a": 3}).Not.To.Contain("b")
78
79 Expect([]byte{1, 2, 3}).To.Contain(byte(1))
80 Expect([]byte{1, 2, 3}).Not.To.Contain(byte(5))
81 Expect([]byte{1, 2, 3}).Not.To.Contain([]byte{2, 3, 4})
82 }
83
84 func (_ ExpectTests) Contain_Failures() {
85 failing("abc does not contain zz", 1, func() {
86 Expect("abc").To.Contain("zz")
87 })
88 failing("aazzbb contains zz", 1, func() {
89 Expect("aazzbb").Not.To.Contain("zz")
90 })
91
92 failing("[1 2] does not contain 3", 1, func() {
93 Expect([]int{1, 2}).To.Contain(3)
94 })
95 failing("[1 2] contains 2", 1, func() {
96 Expect([]int{1, 2}).Not.To.Contain(2)
97 })
98
99 failing("map[a:3] does not contain b", 1, func() {
100 Expect(map[string]int{"a": 3}).To.Contain("b")
101 })
102 failing("map[a:3] contains a", 1, func() {
103 Expect(map[string]int{"a": 3}).Not.To.Contain("a")
104 })
105
106 failing("[1 2 3] does not contain [3 4]", 1, func() {
107 Expect([]byte{1, 2, 3}).To.Contain([]byte{3, 4})
108 })
109 failing("[1 2 3] contains [2 3]", 1, func() {
110 Expect([]byte{1, 2, 3}).Not.To.Contain([]byte{2, 3})
111 })
112 }
113
114 func (_ ExpectTests) ExpectMulipleValues_Success() {
115 Expect(1, true, "over 9000").To.Equal(1, true, "over 9000")
116 }
117
118 func (_ ExpectTests) ExpectMulipleValues_Failure() {
119 failing("mismatch number of values and expectations 3 != 2", 1, func() {
120 Expect(1, true, "over 9000").To.Equal(1, true)
121 })
122 failing("mismatch number of values and expectations 2 != 3", 1, func() {
123 Expect(1, true).To.Equal(1, true, "a")
124 })
125 failing("expected 1 to be equal to 2", 1, func() {
126 Expect(1, "a").To.Equal(2, "a")
127 })
128 failing(`expected "a" to be equal to "b"`, 1, func() {
129 Expect(1, "a").To.Equal(1, "b")
130 })
131 }
132
133 func (_ ExpectTests) ExpectNil_Success() {
134 var n *string
135 Expect(nil).To.Equal(nil)
136 Expect(n).To.Equal(nil)
137 Expect(n).To.Equal(n)
138 Expect(nil).Not.To.Equal(32)
139 Expect(32).Not.To.Equal(n)
140 }
141
142 func (_ ExpectTests) ExpectNil_Failure() {
143 var n *string
144 failing("expected <nil> to be equal to 44", 2, func() {
145 Expect(nil).To.Equal(44)
146 Expect(n).To.Equal(44)
147 })
148 failing("expected <nil> not to equal <nil>", 3, func() {
149 Expect(nil).Not.To.Equal(nil)
150 Expect(nil).Not.To.Equal(n)
151 Expect(n).Not.To.Equal(n)
152 })
153 }
154
155 func (_ ExpectTests) Fail() {
156 failing("the failure reason 123", 1, func() {
157 Fail("the failure reason %d", 123)
158 })
159 }
160
161 func (_ ExpectTests) Skip_After() {
162 Fail("failed")
163 Skip("the skip reason %d", 9001)
164 }
165
166 func (_ ExpectTests) Skip_Before() {
167 Skip("the skip reason")
168 Fail("failed")
169 }
170
171 func (_ ExpectTests) Skip_NoPanic() {
172 Skip("the skip reason")
173 panic("should be swallowed")
174 }
175
176 func (_ ExpectTests) AltenrativeSyntax() {
177 Expect(true).ToEqual(true)
178 Expect(123).Not.ToEqual(11)
179 Expect("it's over", 9000).ToEqual("it's over", 9000)
180 Expect("it's not over", 90000).Not.ToEqual("it's over", 9000)
181
182 Expect(94).GreaterThan(93)
183 Expect(94).Not.GreaterThan(94)
184 Expect(94).Not.GreaterThan(95)
185 Expect(94).GreaterOrEqualTo(93)
186 Expect(94).GreaterOrEqualTo(94)
187 Expect(94).Not.GreaterOrEqualTo(95)
188
189 Expect(12).LessThan(13)
190 Expect(12).Not.LessThan(12)
191 Expect(12).Not.LessThan(-1)
192 Expect(12).LessOrEqualTo(12)
193 Expect(12).LessOrEqualTo(12)
194 Expect(12).Not.LessOrEqualTo(1)
195 }
196
197 func (_ ExpectTests) Json() {
198 Expect(`{"about":true}`).To.Equal(JSON(`{
199 "about": true
200 }`))
201 }
202
203 func (_ ExpectTests) GlobalBeforEEach() {
204 Expect(beforeEachCalled).Greater.Than(0)
205 }
206
207 func (_ ExpectTests) EqlForInts() {
208 Expect(uint16(1)).To.Eql(1)
209 Expect(uint32(2)).To.Eql(2)
210 Expect(uint64(3)).To.Eql(3)
211 Expect(int16(4)).To.Eql(4)
212 Expect(int32(5)).To.Eql(5)
213 Expect(int64(6)).To.Eql(6)
214 Expect(byte(7)).To.Eql(7)
215 }
216
217 func (_ ExpectTests) EqlForStringAndBytes() {
218 Expect([]byte{65, 66}).To.Eql("AB")
219 Expect("CD").To.Eql([]byte{67, 68})
220 }
221
222 func failing(expected string, count int, f func()) {
223 actuals := make([]string, 0, 5)
224 Errorf = func(format string, args ...interface{}) {
225 actuals = append(actuals, fmt.Sprintf(format, args...))
226 }
227 f()
228 Errorf = originalErrorf
229 if len(actuals) != count {
230 Errorf("expected %d failures got %d", count, len(actuals))
231 return
232 }
233 for i := 0; i < count; i++ {
234 if actuals[i] != expected {
235 if count == 1 {
236 Errorf("expected failure '%s' got '%s'", expected, actuals[i])
237 } else {
238 Errorf("expected failure '%s' got '%s' index %d", expected, actuals[i], i+1)
239 }
240 }
241 }
242 }