Debian Codegraph @

expect_numeric_test.go

raw
1 package expect
2
3 import (
4 "testing"
5 "time"
6 )
7
8 type ExpectNumericTests struct{}
9
10 func Test_ExpectNumeric(t *testing.T) {
11 Expectify(new(ExpectNumericTests), t)
12 }
13
14 func (_ *ExpectNumericTests) Greater_Success() {
15 eachNumeric(10, 9, func(a, b interface{}) {
16 Expect(a).Greater.Than(b)
17 })
18
19 Expect(float32(10.1)).Greater.Than(float32(-10.0))
20 Expect(float64(-3.33)).Greater.Than(float64(-4))
21 }
22
23 func (_ *ExpectNumericTests) Greater_Success_Time() {
24 Expect(time.Now()).Greater.Than(time.Now().Add(time.Second * -2))
25 }
26
27 func (_ *ExpectNumericTests) Greater_Failures() {
28 failing("expected 9 to be greater than 10", 9, func() {
29 eachNumeric(9, 10, func(a, b interface{}) {
30 Expect(a).Greater.Than(b)
31 })
32 })
33
34 failing("expected 10 to be greater than 10", 9, func() {
35 eachNumeric(10, 10, func(a, b interface{}) {
36 Expect(a).Greater.Than(b)
37 })
38 })
39
40 failing("expected -2.3 to be greater than -2.299", 2, func() {
41 Expect(float32(-2.3)).Greater.Than(float32(-2.299))
42 Expect(float64(-2.3)).Greater.Than(float64(-2.299))
43 })
44
45 failing("expected 33 to be greater than 1.2 - type mismatch int != float64", 1, func() {
46 Expect(33).Greater.Than(1.2)
47 })
48
49 failing("cannot use greater than for type string", 1, func() {
50 Expect("a").Greater.Than("b")
51 })
52 }
53
54 func (_ *ExpectNumericTests) Greater_Failure_Time() {
55 failing("expected 2014-12-07 04:18:10 +0000 UTC to be greater than 2014-12-07 04:18:11 +0000 UTC", 1, func() {
56 Expect(time.Unix(1417925890, 0).UTC()).Greater.Than(time.Unix(1417925891, 0).UTC())
57 })
58
59 failing("expected 2014-12-07 04:18:10 +0000 UTC to be greater than 2014-12-07 04:18:10 +0000 UTC", 1, func() {
60 Expect(time.Unix(1417925890, 0).UTC()).Greater.Than(time.Unix(1417925890, 0).UTC())
61 })
62 }
63
64 func (_ *ExpectNumericTests) GreaterOrEqual_Success() {
65 eachNumeric(10, 9, func(a, b interface{}) {
66 Expect(a).GreaterOrEqual.To(b)
67 })
68 eachNumeric(10, 10, func(a, b interface{}) {
69 Expect(a).GreaterOrEqual.To(b)
70 })
71
72 Expect(float32(10.1)).GreaterOrEqual.To(float32(-10.0))
73 Expect(float32(10.1)).GreaterOrEqual.To(float32(10.1))
74 Expect(float64(-3.33)).GreaterOrEqual.To(float64(-4))
75 Expect(float64(-3.33)).GreaterOrEqual.To(float64(-3.33))
76 }
77
78 func (_ *ExpectNumericTests) GreaterOrEqual_Success_Time() {
79 now := time.Now()
80 Expect(now).GreaterOrEqual.To(now.Add(time.Second * -2))
81 Expect(now).GreaterOrEqual.To(now)
82 }
83
84 func (_ *ExpectNumericTests) GreaterOrEqual_Failures() {
85 failing("expected 9 to be greater or equal to 10", 9, func() {
86 eachNumeric(9, 10, func(a, b interface{}) {
87 Expect(a).GreaterOrEqual.To(b)
88 })
89 })
90
91 failing("expected -2.3 to be greater or equal to -2.299", 2, func() {
92 Expect(float32(-2.3)).GreaterOrEqual.To(float32(-2.299))
93 Expect(float64(-2.3)).GreaterOrEqual.To(float64(-2.299))
94 })
95 }
96
97 func (_ *ExpectNumericTests) GreaterOrEqual_Failure_Time() {
98 failing("expected 2014-12-07 04:18:10 +0000 UTC to be greater or equal to 2014-12-07 04:18:11 +0000 UTC", 1, func() {
99 Expect(time.Unix(1417925890, 0).UTC()).GreaterOrEqual.To(time.Unix(1417925891, 0).UTC())
100 })
101 }
102
103 func (_ *ExpectNumericTests) Not_Success() {
104 Expect(492).Not.Greater.Than(493)
105 Expect(493).Not.Greater.Than(493)
106
107 Expect(492).Not.GreaterOrEqual.To(493)
108
109 Expect(495).Not.Less.Than(494)
110 Expect(495).Not.Less.Than(495)
111
112 Expect(496).Not.LessOrEqual.To(495)
113 }
114
115 func (_ *ExpectNumericTests) Not_Failures() {
116 failing("expected 494 not to be greater than 493", 1, func() {
117 Expect(494).Not.Greater.Than(493)
118 })
119
120 failing("expected 495 not to be greater or equal to 494", 1, func() {
121 Expect(495).Not.GreaterOrEqual.To(494)
122 })
123
124 failing("expected 495 not to be greater or equal to 495", 1, func() {
125 Expect(495).Not.GreaterOrEqual.To(495)
126 })
127
128 failing("expected 22 not to be less than 23", 1, func() {
129 Expect(22).Not.Less.Than(23)
130 })
131
132 failing("expected 11 not to be less or equal to 12", 1, func() {
133 Expect(11).Not.LessOrEqual.To(12)
134 })
135
136 failing("expected 2 not to be less or equal to 2", 1, func() {
137 Expect(2).Not.LessOrEqual.To(2)
138 })
139 }
140
141 func (_ *ExpectNumericTests) Less_Success() {
142 eachNumeric(9, 10, func(a, b interface{}) {
143 Expect(a).Less.Than(b)
144 })
145
146 Expect(float32(-10.0)).Less.Than(float32(11.0))
147 Expect(float64(-3.34)).Less.Than(float64(-3.33339))
148 }
149
150 func (_ *ExpectNumericTests) Less_Success_Time() {
151 Expect(time.Now()).Less.Than(time.Now().Add(time.Second * 2))
152 }
153
154 func (_ *ExpectNumericTests) Less_Failures() {
155 failing("expected 23 to be less than 22", 9, func() {
156 eachNumeric(23, 22, func(a, b interface{}) {
157 Expect(a).Less.Than(b)
158 })
159 })
160
161 failing("expected 123 to be less than 123", 9, func() {
162 eachNumeric(123, 123, func(a, b interface{}) {
163 Expect(a).Less.Than(b)
164 })
165 })
166
167 failing("expected -2.299 to be less than -2.3", 2, func() {
168 Expect(float32(-2.299)).Less.Than(float32(-2.3))
169 Expect(float64(-2.299)).Less.Than(float64(-2.3))
170 })
171
172 failing("expected 4.01 to be less than 4.01", 2, func() {
173 Expect(float32(4.01)).Less.Than(float32(4.01))
174 Expect(float64(4.01)).Less.Than(float64(4.01))
175 })
176
177 failing("expected 33 to be less than 1.2 - type mismatch int != float64", 1, func() {
178 Expect(33).Less.Than(1.2)
179 })
180
181 failing("cannot use less than for type string", 1, func() {
182 Expect("a").Less.Than("b")
183 })
184 }
185
186 func (_ *ExpectNumericTests) Less_Failure_Time() {
187 failing("expected 2014-12-07 04:18:11 +0000 UTC to be less than 2014-12-07 04:18:10 +0000 UTC", 1, func() {
188 Expect(time.Unix(1417925891, 0).UTC()).Less.Than(time.Unix(1417925890, 0).UTC())
189 })
190
191 failing("expected 2014-12-07 04:18:10 +0000 UTC to be less than 2014-12-07 04:18:10 +0000 UTC", 1, func() {
192 Expect(time.Unix(1417925890, 0).UTC()).Less.Than(time.Unix(1417925890, 0).UTC())
193 })
194 }
195
196 func (_ *ExpectNumericTests) LessOrEqual_Success() {
197 eachNumeric(9, 10, func(a, b interface{}) {
198 Expect(a).LessOrEqual.To(b)
199 })
200 eachNumeric(10, 10, func(a, b interface{}) {
201 Expect(a).LessOrEqual.To(b)
202 })
203
204 Expect(float32(3.2)).LessOrEqual.To(float32(44.1))
205 Expect(float32(0.1)).LessOrEqual.To(float32(0.1))
206 Expect(float64(-4.1)).LessOrEqual.To(float64(-4.0001))
207 Expect(float64(-3.33)).LessOrEqual.To(float64(-3.33))
208 }
209
210 func (_ *ExpectNumericTests) LessOrEqual_Success_Time() {
211 now := time.Now()
212 Expect(now).LessOrEqual.To(now.Add(time.Second * 2))
213 Expect(now).LessOrEqual.To(now)
214 }
215
216 func (_ *ExpectNumericTests) LessOrEqual_Failures() {
217 failing("expected 3 to be less or equal to 1", 9, func() {
218 eachNumeric(3, 1, func(a, b interface{}) {
219 Expect(a).LessOrEqual.To(b)
220 })
221 })
222
223 failing("expected -2.291 to be less or equal to -2.292", 2, func() {
224 Expect(float32(-2.291)).LessOrEqual.To(float32(-2.292))
225 Expect(float64(-2.291)).LessOrEqual.To(float64(-2.292))
226 })
227 }
228
229 func (_ *ExpectNumericTests) LessOrEqual_Failure_Time() {
230 failing("expected 2014-12-07 04:18:11 +0000 UTC to be less or equal to 2014-12-07 04:18:10 +0000 UTC", 1, func() {
231 Expect(time.Unix(1417925891, 0).UTC()).LessOrEqual.To(time.Unix(1417925890, 0).UTC())
232 })
233 }
234
235 func (_ *ExpectNumericTests) Equal_Success() {
236 eachNumeric(101, 101, func(a, b interface{}) {
237 Expect(a).To.Equal(b)
238 })
239
240 Expect(float32(0.12)).To.Equal(float32(0.12))
241 Expect(float64(-3.33011)).To.Equal(float64(-3.33011))
242 }
243
244 func (_ *ExpectNumericTests) Equal_Failures() {
245 failing("expected 3 to be equal to 1", 9, func() {
246 eachNumeric(3, 1, func(a, b interface{}) {
247 Expect(a).To.Equal(b)
248 })
249 })
250
251 failing("expected -2.291 to be equal to -2.292", 2, func() {
252 Expect(float32(-2.291)).To.Equal(float32(-2.292))
253 Expect(float64(-2.291)).To.Equal(float64(-2.292))
254 })
255 }
256
257 func (_ *ExpectNumericTests) NotEqual_Success() {
258 eachNumeric(101, 100, func(a, b interface{}) {
259 Expect(a).Not.To.Equal(b)
260 })
261
262 Expect(float32(0.12)).Not.To.Equal(float32(0.112))
263 Expect(float64(-3.33011)).Not.To.Equal(float64(-3.330111))
264 }
265
266 func (_ *ExpectNumericTests) NotEqual_Failures() {
267 failing("expected 33 not to equal 33", 9, func() {
268 eachNumeric(33, 33, func(a, b interface{}) {
269 Expect(a).Not.To.Equal(b)
270 })
271 })
272
273 failing("expected -2.291 not to equal -2.291", 2, func() {
274 Expect(float32(-2.291)).Not.To.Equal(float32(-2.291))
275 Expect(float64(-2.291)).Not.To.Equal(float64(-2.291))
276 })
277 }
278
279 func eachNumeric(a, b int, f func(ai, bi interface{})) {
280 f(a, b)
281 f(int8(a), int8(b))
282 f(int16(a), int16(b))
283 f(int32(a), int32(b))
284 f(int64(a), int64(b))
285 f(uint8(a), uint8(b))
286 f(uint16(a), uint16(b))
287 f(uint32(a), uint32(b))
288 f(uint64(a), uint64(b))
289 }