File: common_test.go

package info (click to toggle)
golang-github-vdemeester-shakers 0.0~git20160210.0.24d7f1d-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 192 kB
  • ctags: 158
  • sloc: sh: 159; makefile: 25
file content (401 lines) | stat: -rw-r--r-- 17,616 bytes parent folder | download | duplicates (3)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package shakers

import (
	"reflect"
	"testing"
	"time"

	"github.com/go-check/check"
)

func init() {
	check.Suite(&CommonCheckerS{})
}

type CommonCheckerS struct{}

func (s *CommonCheckerS) TestEqualsInfo(c *check.C) {
	testInfo(c, Equals, "Equals", []string{"obtained", "expected"})
}

func (s *CommonCheckerS) TestEqualsValidsEquals(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, Equals, true, "", "string", "string")
	testCheck(c, Equals, true, "", 0, 0)
	testCheck(c, Equals, true, "", anEqualer{1}, anEqualer{1})
	testCheck(c, Equals, true, "", myTime, myTime)
	testCheck(c, Equals, true, "", myTime, "2018-01-01")
	testCheck(c, Equals, true, "", myTime, "2018-01-01T00:00:00Z")
}

func (s *CommonCheckerS) TestEqualsValidsDifferent(c *check.C) {
	myTime1, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}
	myTime2, err := time.Parse("2006-01-02", "2018-01-02")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, Equals, false, "", "string", "astring")
	testCheck(c, Equals, false, "", 0, 1)
	testCheck(c, Equals, false, "", anEqualer{1}, anEqualer{0})
	testCheck(c, Equals, false, "", myTime1, myTime2)
	testCheck(c, Equals, false, "", myTime1, "2018-01-02")
	testCheck(c, Equals, false, "", myTime1, "2018-01-02T00:00:00Z")
}

func (s *CommonCheckerS) TestEqualsInvalids(c *check.C) {
	// Incompatible type time.Time
	testCheck(c, Equals, false, "obtained value and expected value have not the same type.", "2015-01-01", time.Now())
	testCheck(c, Equals, false, "expected must be a Time struct, or parseable.", time.Now(), 0)

	// Incompatible type Equaler
	testCheck(c, Equals, false, "expected value must be an Equaler - implementing Equal(Equaler).", anEqualer{0}, 0)

	testCheck(c, Equals, false, "obtained value and expected value have not the same type.", 0, anEqualer{0})

	// Nils
	testCheck(c, Equals, false, "obtained value and expected value have not the same type.", nil, 0)
	testCheck(c, Equals, false, "obtained value and expected value have not the same type.", 0, nil)
}

type anEqualer struct {
	value int
}

func (a anEqualer) Equal(b Equaler) bool {
	if bEqualer, ok := b.(anEqualer); ok {
		return a.value == bEqualer.value
	}
	return false
}

func (s *CommonCheckerS) TestGreaterThanInfo(c *check.C) {
	testInfo(c, GreaterThan, "GreaterThan", []string{"obtained", "expected"})
}

func (s *CommonCheckerS) TestGreaterThanValidsGreater(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, GreaterThan, true, "", 2, 1)
	testCheck(c, GreaterThan, true, "", 0, -1)
	testCheck(c, GreaterThan, true, "", float32(2), float32(0))
	testCheck(c, GreaterThan, true, "", float64(2), float64(0))
	testCheck(c, GreaterThan, true, "", byte(2), byte(1))
	testCheck(c, GreaterThan, true, "", int(2), int(1))
	testCheck(c, GreaterThan, true, "", int8(2), int8(1))
	testCheck(c, GreaterThan, true, "", int16(2), int16(1))
	testCheck(c, GreaterThan, true, "", int32(2), int32(1))
	testCheck(c, GreaterThan, true, "", int64(2), int64(1))
	testCheck(c, GreaterThan, true, "", uint(2), uint(1))
	testCheck(c, GreaterThan, true, "", uint8(2), uint8(1))
	testCheck(c, GreaterThan, true, "", uint16(2), uint16(1))
	testCheck(c, GreaterThan, true, "", uint32(2), uint32(1))
	testCheck(c, GreaterThan, true, "", uint64(2), uint64(1))
	testCheck(c, GreaterThan, true, "", myTime, "2017-12-31")
}

func (s *CommonCheckerS) TestGreaterThanValidsDifferent(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, GreaterThan, false, "", 1, 2)
	testCheck(c, GreaterThan, false, "", -1, 0)
	testCheck(c, GreaterThan, false, "", float32(0), float32(2))
	testCheck(c, GreaterThan, false, "", float64(0), float64(2))
	testCheck(c, GreaterThan, false, "", byte(1), byte(2))
	testCheck(c, GreaterThan, false, "", int(1), int(2))
	testCheck(c, GreaterThan, false, "", int8(1), int8(2))
	testCheck(c, GreaterThan, false, "", int16(1), int16(2))
	testCheck(c, GreaterThan, false, "", int32(1), int32(2))
	testCheck(c, GreaterThan, false, "", int64(1), int64(2))
	testCheck(c, GreaterThan, false, "", uint(1), uint(2))
	testCheck(c, GreaterThan, false, "", uint8(1), uint8(2))
	testCheck(c, GreaterThan, false, "", uint16(1), uint16(2))
	testCheck(c, GreaterThan, false, "", uint32(1), uint32(2))
	testCheck(c, GreaterThan, false, "", uint64(1), uint64(2))
	testCheck(c, GreaterThan, false, "", myTime, "2018-01-01")
	testCheck(c, GreaterThan, false, "", myTime, "2018-01-02")
}

func (s *CommonCheckerS) TestGreaterThanInvalids(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, GreaterThan, false, "obtained value and expected value have not the same type.", 1, "string")
	testCheck(c, GreaterThan, false, "obtained value and expected value have not the same type.", "string", 1)
	testCheck(c, GreaterThan, false, "obtained value and expected value have not the same type.", 1, complex128(1+2i))
	testCheck(c, GreaterThan, false, "expected must be a Time struct, or parseable.", myTime, 1)
	testCheck(c, GreaterThan, false, "expected must be a Time struct, or parseable.", myTime, "invalid")
}

func (s *CommonCheckerS) TestGreaterOrEqualThanInfo(c *check.C) {
	testInfo(c, GreaterOrEqualThan, "GreaterOrEqualThan", []string{"obtained", "expected"})
}

func (s *CommonCheckerS) TestGreaterOrEqualThanValidsGreaterOrEquals(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, GreaterOrEqualThan, true, "", 2, 1)
	testCheck(c, GreaterOrEqualThan, true, "", 0, -1)
	testCheck(c, GreaterOrEqualThan, true, "", float32(2), float32(0))
	testCheck(c, GreaterOrEqualThan, true, "", float64(2), float64(0))
	testCheck(c, GreaterOrEqualThan, true, "", byte(2), byte(1))
	testCheck(c, GreaterOrEqualThan, true, "", int(2), int(1))
	testCheck(c, GreaterOrEqualThan, true, "", int8(2), int8(1))
	testCheck(c, GreaterOrEqualThan, true, "", int16(2), int16(1))
	testCheck(c, GreaterOrEqualThan, true, "", int32(2), int32(1))
	testCheck(c, GreaterOrEqualThan, true, "", int64(2), int64(1))
	testCheck(c, GreaterOrEqualThan, true, "", uint(2), uint(1))
	testCheck(c, GreaterOrEqualThan, true, "", uint8(2), uint8(1))
	testCheck(c, GreaterOrEqualThan, true, "", uint16(2), uint16(1))
	testCheck(c, GreaterOrEqualThan, true, "", uint32(2), uint32(1))
	testCheck(c, GreaterOrEqualThan, true, "", uint64(2), uint64(1))
	testCheck(c, GreaterOrEqualThan, true, "", myTime, "2017-12-31")
	testCheck(c, GreaterOrEqualThan, true, "", 2, 2)
	testCheck(c, GreaterOrEqualThan, true, "", -1, -1)
	testCheck(c, GreaterOrEqualThan, true, "", float32(2), float32(2))
	testCheck(c, GreaterOrEqualThan, true, "", float64(2), float64(2))
	testCheck(c, GreaterOrEqualThan, true, "", byte(2), byte(2))
	testCheck(c, GreaterOrEqualThan, true, "", int(2), int(2))
	testCheck(c, GreaterOrEqualThan, true, "", int8(2), int8(2))
	testCheck(c, GreaterOrEqualThan, true, "", int16(2), int16(2))
	testCheck(c, GreaterOrEqualThan, true, "", int32(2), int32(2))
	testCheck(c, GreaterOrEqualThan, true, "", int64(2), int64(2))
	testCheck(c, GreaterOrEqualThan, true, "", uint(2), uint(2))
	testCheck(c, GreaterOrEqualThan, true, "", uint8(2), uint8(2))
	testCheck(c, GreaterOrEqualThan, true, "", uint16(2), uint16(2))
	testCheck(c, GreaterOrEqualThan, true, "", uint32(2), uint32(2))
	testCheck(c, GreaterOrEqualThan, true, "", uint64(2), uint64(2))
	// testCheck(c, GreaterOrEqualThan, true, "", myTime, "2018-01-01")
}

func (s *CommonCheckerS) TestGreaterOrEqualThanValidsDifferent(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, GreaterOrEqualThan, false, "", 1, 2)
	testCheck(c, GreaterOrEqualThan, false, "", -1, 0)
	testCheck(c, GreaterOrEqualThan, false, "", float32(0), float32(2))
	testCheck(c, GreaterOrEqualThan, false, "", float64(0), float64(2))
	testCheck(c, GreaterOrEqualThan, false, "", byte(1), byte(2))
	testCheck(c, GreaterOrEqualThan, false, "", int(1), int(2))
	testCheck(c, GreaterOrEqualThan, false, "", int8(1), int8(2))
	testCheck(c, GreaterOrEqualThan, false, "", int16(1), int16(2))
	testCheck(c, GreaterOrEqualThan, false, "", int32(1), int32(2))
	testCheck(c, GreaterOrEqualThan, false, "", int64(1), int64(2))
	testCheck(c, GreaterOrEqualThan, false, "", uint(1), uint(2))
	testCheck(c, GreaterOrEqualThan, false, "", uint8(1), uint8(2))
	testCheck(c, GreaterOrEqualThan, false, "", uint16(1), uint16(2))
	testCheck(c, GreaterOrEqualThan, false, "", uint32(1), uint32(2))
	testCheck(c, GreaterOrEqualThan, false, "", uint64(1), uint64(2))
	testCheck(c, GreaterOrEqualThan, false, "", myTime, "2018-01-01")
	testCheck(c, GreaterOrEqualThan, false, "", myTime, "2018-01-02")
}

func (s *CommonCheckerS) TestGreaterOrEqualThanInvalids(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, GreaterOrEqualThan, false, "obtained value and expected value have not the same type.", 1, "string")
	testCheck(c, GreaterOrEqualThan, false, "obtained value and expected value have not the same type.", "string", 1)
	testCheck(c, GreaterOrEqualThan, false, "obtained value and expected value have not the same type.", 1, complex128(1+2i))
	testCheck(c, GreaterOrEqualThan, false, "expected must be a Time struct, or parseable.", myTime, 1)
	testCheck(c, GreaterOrEqualThan, false, "expected must be a Time struct, or parseable.", myTime, "invalid")
}

func (s *CommonCheckerS) TestLessThanInfo(c *check.C) {
	testInfo(c, LessThan, "LessThan", []string{"obtained", "expected"})
}

func (s *CommonCheckerS) TestLessThanValidsLess(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, LessThan, true, "", 1, 2)
	testCheck(c, LessThan, true, "", -1, 0)
	testCheck(c, LessThan, true, "", float32(0), float32(2))
	testCheck(c, LessThan, true, "", float64(0), float64(2))
	testCheck(c, LessThan, true, "", byte(1), byte(2))
	testCheck(c, LessThan, true, "", int(1), int(2))
	testCheck(c, LessThan, true, "", int8(1), int8(2))
	testCheck(c, LessThan, true, "", int16(1), int16(2))
	testCheck(c, LessThan, true, "", int32(1), int32(2))
	testCheck(c, LessThan, true, "", int64(1), int64(2))
	testCheck(c, LessThan, true, "", uint(1), uint(2))
	testCheck(c, LessThan, true, "", uint8(1), uint8(2))
	testCheck(c, LessThan, true, "", uint16(1), uint16(2))
	testCheck(c, LessThan, true, "", uint32(1), uint32(2))
	testCheck(c, LessThan, true, "", uint64(1), uint64(2))
	testCheck(c, LessThan, true, "", myTime, "2018-01-02")
}

func (s *CommonCheckerS) TestLessThanValidsDifferent(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, LessThan, false, "", 2, 1)
	testCheck(c, LessThan, false, "", 0, -1)
	testCheck(c, LessThan, false, "", float32(2), float32(0))
	testCheck(c, LessThan, false, "", float64(2), float64(0))
	testCheck(c, LessThan, false, "", byte(2), byte(1))
	testCheck(c, LessThan, false, "", int(2), int(1))
	testCheck(c, LessThan, false, "", int8(2), int8(1))
	testCheck(c, LessThan, false, "", int16(2), int16(1))
	testCheck(c, LessThan, false, "", int32(2), int32(1))
	testCheck(c, LessThan, false, "", int64(2), int64(1))
	testCheck(c, LessThan, false, "", uint(2), uint(1))
	testCheck(c, LessThan, false, "", uint8(2), uint8(1))
	testCheck(c, LessThan, false, "", uint16(2), uint16(1))
	testCheck(c, LessThan, false, "", uint32(2), uint32(1))
	testCheck(c, LessThan, false, "", uint64(2), uint64(1))
	testCheck(c, LessThan, false, "", myTime, "2018-01-01")
	testCheck(c, LessThan, false, "", myTime, "2017-12-31")
}

func (s *CommonCheckerS) TestLessThanInvalids(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, LessThan, false, "obtained value and expected value have not the same type.", 1, "string")
	testCheck(c, LessThan, false, "obtained value and expected value have not the same type.", "string", 1)
	testCheck(c, LessThan, false, "obtained value and expected value have not the same type.", 1, complex128(1+2i))
	testCheck(c, LessThan, false, "expected must be a Time struct, or parseable.", myTime, 1)
	testCheck(c, LessThan, false, "expected must be a Time struct, or parseable.", myTime, "invalid")
}

func (s *CommonCheckerS) TestLessEqualThanInfo(c *check.C) {
	testInfo(c, LessOrEqualThan, "LessOrEqualThan", []string{"obtained", "expected"})
}

func (s *CommonCheckerS) TestLessOrEqualThanValidsLessOrEquals(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, LessOrEqualThan, true, "", 1, 2)
	testCheck(c, LessOrEqualThan, true, "", -1, 0)
	testCheck(c, LessOrEqualThan, true, "", float32(0), float32(2))
	testCheck(c, LessOrEqualThan, true, "", float64(0), float64(2))
	testCheck(c, LessOrEqualThan, true, "", byte(1), byte(2))
	testCheck(c, LessOrEqualThan, true, "", int(1), int(2))
	testCheck(c, LessOrEqualThan, true, "", int8(1), int8(2))
	testCheck(c, LessOrEqualThan, true, "", int16(1), int16(2))
	testCheck(c, LessOrEqualThan, true, "", int32(1), int32(2))
	testCheck(c, LessOrEqualThan, true, "", int64(1), int64(2))
	testCheck(c, LessOrEqualThan, true, "", uint(1), uint(2))
	testCheck(c, LessOrEqualThan, true, "", uint8(1), uint8(2))
	testCheck(c, LessOrEqualThan, true, "", uint16(1), uint16(2))
	testCheck(c, LessOrEqualThan, true, "", uint32(1), uint32(2))
	testCheck(c, LessOrEqualThan, true, "", uint64(1), uint64(2))
	testCheck(c, LessOrEqualThan, true, "", myTime, "2018-01-02")
	testCheck(c, LessOrEqualThan, true, "", 1, 1)
	testCheck(c, LessOrEqualThan, true, "", -1, -1)
	testCheck(c, LessOrEqualThan, true, "", float32(2), float32(2))
	testCheck(c, LessOrEqualThan, true, "", float64(2), float64(2))
	testCheck(c, LessOrEqualThan, true, "", byte(2), byte(2))
	testCheck(c, LessOrEqualThan, true, "", int(2), int(2))
	testCheck(c, LessOrEqualThan, true, "", int8(2), int8(2))
	testCheck(c, LessOrEqualThan, true, "", int16(2), int16(2))
	testCheck(c, LessOrEqualThan, true, "", int32(2), int32(2))
	testCheck(c, LessOrEqualThan, true, "", int64(2), int64(2))
	testCheck(c, LessOrEqualThan, true, "", uint(2), uint(2))
	testCheck(c, LessOrEqualThan, true, "", uint8(2), uint8(2))
	testCheck(c, LessOrEqualThan, true, "", uint16(2), uint16(2))
	testCheck(c, LessOrEqualThan, true, "", uint32(2), uint32(2))
	testCheck(c, LessOrEqualThan, true, "", uint64(2), uint64(2))
	// testCheck(c, LessOrEqualThan, true, "", myTime, "2018-01-02")
}

func (s *CommonCheckerS) TestLessOrEqualThanValidsDifferent(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, LessOrEqualThan, false, "", 2, 1)
	testCheck(c, LessOrEqualThan, false, "", 0, -1)
	testCheck(c, LessOrEqualThan, false, "", float32(2), float32(0))
	testCheck(c, LessOrEqualThan, false, "", float64(2), float64(0))
	testCheck(c, LessOrEqualThan, false, "", byte(2), byte(1))
	testCheck(c, LessOrEqualThan, false, "", int(2), int(1))
	testCheck(c, LessOrEqualThan, false, "", int8(2), int8(1))
	testCheck(c, LessOrEqualThan, false, "", int16(2), int16(1))
	testCheck(c, LessOrEqualThan, false, "", int32(2), int32(1))
	testCheck(c, LessOrEqualThan, false, "", int64(2), int64(1))
	testCheck(c, LessOrEqualThan, false, "", uint(2), uint(1))
	testCheck(c, LessOrEqualThan, false, "", uint8(2), uint8(1))
	testCheck(c, LessOrEqualThan, false, "", uint16(2), uint16(1))
	testCheck(c, LessOrEqualThan, false, "", uint32(2), uint32(1))
	testCheck(c, LessOrEqualThan, false, "", uint64(2), uint64(1))
	testCheck(c, LessOrEqualThan, false, "", myTime, "2018-01-01")
	testCheck(c, LessOrEqualThan, false, "", myTime, "2017-12-31")
}

func (s *CommonCheckerS) TestLessOrEqualThanInvalids(c *check.C) {
	myTime, err := time.Parse("2006-01-02", "2018-01-01")
	if err != nil {
		c.Fatal(err)
	}

	testCheck(c, LessOrEqualThan, false, "obtained value and expected value have not the same type.", 1, "string")
	testCheck(c, LessOrEqualThan, false, "obtained value and expected value have not the same type.", "string", 1)
	testCheck(c, LessOrEqualThan, false, "obtained value and expected value have not the same type.", 1, complex128(1+2i))
	testCheck(c, LessOrEqualThan, false, "expected must be a Time struct, or parseable.", myTime, 1)
	testCheck(c, LessOrEqualThan, false, "expected must be a Time struct, or parseable.", myTime, "invalid")
}

func Test(t *testing.T) {
	check.TestingT(t)
}

func testInfo(c *check.C, checker check.Checker, name string, paramNames []string) {
	info := checker.Info()
	if info.Name != name {
		c.Fatalf("Got name %s, expected %s", info.Name, name)
	}
	if !reflect.DeepEqual(info.Params, paramNames) {
		c.Fatalf("Got param names %#v, expected %#v", info.Params, paramNames)
	}
}

func testCheck(c *check.C, checker check.Checker, expectedResult bool, expectedError string, params ...interface{}) ([]interface{}, []string) {
	info := checker.Info()
	if len(params) != len(info.Params) {
		c.Fatalf("unexpected param count in test; expected %d got %d", len(info.Params), len(params))
	}
	names := append([]string{}, info.Params...)
	result, error := checker.Check(params, names)
	if result != expectedResult || error != expectedError {
		c.Fatalf("%s.Check(%#v) returned (%#v, %#v) rather than (%#v, %#v)",
			info.Name, params, result, error, expectedResult, expectedError)
	}
	return params, names
}