This repository has been archived by the owner on May 31, 2023. It is now read-only.
forked from chai2010/webp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtesting_test.go
573 lines (530 loc) · 18.1 KB
/
testing_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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright 2015 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// testing helper, please fork this file, and fix the package name.
//
// See http://github.com/chai2010/assert
package webp
import (
"fmt"
"math"
"os"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
)
func tIsIntType(v interface{}) bool {
switch reflect.ValueOf(v).Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
return true
}
return false
}
func tIsUintType(v interface{}) bool {
switch reflect.ValueOf(v).Kind() {
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr:
return true
}
return false
}
func tIsFloatType(v interface{}) bool {
switch reflect.ValueOf(v).Kind() {
case reflect.Float32,
reflect.Float64:
return true
}
return false
}
func tIsNumberType(v interface{}) bool {
switch reflect.ValueOf(v).Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64,
reflect.Complex64,
reflect.Complex128:
return true
}
return false
}
func tIsNumberEqual(a, b interface{}) bool {
if tIsNumberType(a) && tIsNumberType(b) {
return fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b)
}
return false
}
func tCallerFileLine(skip int) (file string, line int) {
_, file, line, ok := runtime.Caller(skip + 1)
if ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
file = file[index+1:]
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
file = file[index+1:]
}
} else {
file = "???"
line = 1
}
return
}
func tAssert(t testing.TB, condition bool, args ...interface{}) {
if !condition {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssert failed, %s", file, line, msg)
} else {
t.Fatalf("%s:%d: tAssert failed", file, line)
}
}
}
func tAssertf(t *testing.T, condition bool, format string, a ...interface{}) {
if !condition {
file, line := tCallerFileLine(1)
if msg := fmt.Sprintf(format, a...); msg != "" {
t.Fatalf("%s:%d: tAssert failed, %s", file, line, msg)
} else {
t.Fatalf("%s:%d: tAssert failed", file, line)
}
}
}
func tAssertNil(t testing.TB, p interface{}, args ...interface{}) {
if p != nil {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
if err, ok := p.(error); ok && err != nil {
t.Fatalf("%s:%d: tAssertNil failed, err = %v, %s", file, line, err, msg)
} else {
t.Fatalf("%s:%d: tAssertNil failed, %s", file, line, msg)
}
} else {
if err, ok := p.(error); ok && err != nil {
t.Fatalf("%s:%d: tAssertNil failed, err = %v", file, line, err)
} else {
t.Fatalf("%s:%d: tAssertNil failed", file, line)
}
}
}
}
func tAssertNotNil(t testing.TB, p interface{}, args ...interface{}) {
if p == nil {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertNotNil failed, %s", file, line, msg)
} else {
t.Fatalf("%s:%d: tAssertNotNil failed", file, line)
}
}
}
func tAssertTrue(t testing.TB, condition bool, args ...interface{}) {
if condition != true {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertTrue failed, %s", file, line, msg)
} else {
t.Fatalf("%s:%d: tAssertTrue failed", file, line)
}
}
}
func tAssertFalse(t testing.TB, condition bool, args ...interface{}) {
if condition != false {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertFalse failed, %s", file, line, msg)
} else {
t.Fatalf("%s:%d: tAssertFalse failed", file, line)
}
}
}
func tAssertEqual(t testing.TB, expected, got interface{}, args ...interface{}) {
if !reflect.DeepEqual(expected, got) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertEqual failed, expected = %v, got = %v, %s", file, line, expected, got, msg)
} else {
t.Fatalf("%s:%d: tAssertEqual failed, expected = %v, got = %v", file, line, expected, got)
}
}
}
func tAssertNotEqual(t testing.TB, expected, got interface{}, args ...interface{}) {
if reflect.DeepEqual(expected, got) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertNotEqual failed, expected = %v, got = %v, %s", file, line, expected, got, msg)
} else {
t.Fatalf("%s:%d: tAssertNotEqual failed, expected = %v, got = %v", file, line, expected, got)
}
}
}
func tAssertNear(t testing.TB, expected, got, abs float64, args ...interface{}) {
if math.Abs(expected-got) > abs {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertNear failed, expected = %v, got = %v, abs = %v, %s", file, line, expected, got, abs, msg)
} else {
t.Fatalf("%s:%d: tAssertNear failed, expected = %v, got = %v, abs = %v", file, line, expected, got, abs)
}
}
}
func tAssertBetween(t testing.TB, min, max, val float64, args ...interface{}) {
if val < min || max < val {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertBetween failed, min = %v, max = %v, val = %v, %s", file, line, min, max, val, msg)
} else {
t.Fatalf("%s:%d: tAssertBetween failed, min = %v, max = %v, val = %v", file, line, min, max, val)
}
}
}
func tAssertNotBetween(t testing.TB, min, max, val float64, args ...interface{}) {
if min <= val && val <= max {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertNotBetween failed, min = %v, max = %v, val = %v, %s", file, line, min, max, val, msg)
} else {
t.Fatalf("%s:%d: tAssertNotBetween failed, min = %v, max = %v, val = %v", file, line, min, max, val)
}
}
}
func tAssertMatch(t testing.TB, expectedPattern string, got []byte, args ...interface{}) {
if matched, err := regexp.Match(expectedPattern, got); err != nil || !matched {
file, line := tCallerFileLine(1)
if err != nil {
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMatch failed, expected = %q, got = %v, err = %v, %s", file, line, expectedPattern, got, err, msg)
} else {
t.Fatalf("%s:%d: tAssertMatch failed, expected = %q, got = %v, err = %v", file, line, expectedPattern, got, err)
}
} else {
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMatch failed, expected = %q, got = %v, %s", file, line, expectedPattern, got, msg)
} else {
t.Fatalf("%s:%d: tAssertMatch failed, expected = %q, got = %v", file, line, expectedPattern, got)
}
}
}
}
func tAssertMatchString(t testing.TB, expectedPattern, got string, args ...interface{}) {
if matched, err := regexp.MatchString(expectedPattern, got); err != nil || !matched {
file, line := tCallerFileLine(1)
if err != nil {
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMatchString failed, expected = %q, got = %v, err = %v, %s", file, line, expectedPattern, got, err, msg)
} else {
t.Fatalf("%s:%d: tAssertMatchString failed, expected = %q, got = %v, err = %v", file, line, expectedPattern, got, err)
}
} else {
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMatchString failed, expected = %q, got = %v, %s", file, line, expectedPattern, got, msg)
} else {
t.Fatalf("%s:%d: tAssertMatchString failed, expected = %q, got = %v", file, line, expectedPattern, got)
}
}
}
}
func tAssertSliceContain(t testing.TB, slice, val interface{}, args ...interface{}) {
sliceVal := reflect.ValueOf(slice)
if sliceVal.Kind() != reflect.Slice {
panic(fmt.Sprintf("tAssertSliceContain called with non-slice value of type %T", slice))
}
var contained bool
for i := 0; i < sliceVal.Len(); i++ {
if reflect.DeepEqual(sliceVal.Index(i).Interface(), val) {
contained = true
break
}
}
if !contained {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertSliceContain failed, slice = %v, val = %v, %s", file, line, slice, val, msg)
} else {
t.Fatalf("%s:%d: tAssertSliceContain failed, slice = %v, val = %v", file, line, slice, val)
}
}
}
func tAssertSliceNotContain(t testing.TB, slice, val interface{}, args ...interface{}) {
sliceVal := reflect.ValueOf(slice)
if sliceVal.Kind() != reflect.Slice {
panic(fmt.Sprintf("tAssertSliceNotContain called with non-slice value of type %T", slice))
}
var contained bool
for i := 0; i < sliceVal.Len(); i++ {
if reflect.DeepEqual(sliceVal.Index(i).Interface(), val) {
contained = true
break
}
}
if contained {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertSliceNotContain failed, slice = %v, val = %v, %s", file, line, slice, val, msg)
} else {
t.Fatalf("%s:%d: tAssertSliceNotContain failed, slice = %v, val = %v", file, line, slice, val)
}
}
}
func tAssertMapContain(t testing.TB, m, key, val interface{}, args ...interface{}) {
mapVal := reflect.ValueOf(m)
if mapVal.Kind() != reflect.Map {
panic(fmt.Sprintf("tAssertMapContain called with non-map value of type %T", m))
}
elemVal := mapVal.MapIndex(reflect.ValueOf(key))
if !elemVal.IsValid() || !reflect.DeepEqual(elemVal.Interface(), val) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMapContain failed, map = %v, key = %v, val = %v, %s", file, line, m, key, val, msg)
} else {
t.Fatalf("%s:%d: tAssertMapContain failed, map = %v, key = %v, val = %v", file, line, m, key, val)
}
}
}
func tAssertMapContainKey(t testing.TB, m, key interface{}, args ...interface{}) {
mapVal := reflect.ValueOf(m)
if mapVal.Kind() != reflect.Map {
panic(fmt.Sprintf("tAssertMapContainKey called with non-map value of type %T", m))
}
elemVal := mapVal.MapIndex(reflect.ValueOf(key))
if !elemVal.IsValid() {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMapContainKey failed, map = %v, key = %v, %s", file, line, m, key, msg)
} else {
t.Fatalf("%s:%d: tAssertMapContainKey failed, map = %v, key = %v", file, line, m, key)
}
}
}
func tAssertMapContainVal(t testing.TB, m, val interface{}, args ...interface{}) {
mapVal := reflect.ValueOf(m)
if mapVal.Kind() != reflect.Map {
panic(fmt.Sprintf("tAssertMapContainVal called with non-map value of type %T", m))
}
var contained bool
for _, key := range mapVal.MapKeys() {
elemVal := mapVal.MapIndex(key)
if elemVal.IsValid() && reflect.DeepEqual(elemVal.Interface(), val) {
contained = true
break
}
}
if !contained {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMapContainVal failed, map = %v, val = %v, %s", file, line, m, val, msg)
} else {
t.Fatalf("%s:%d: tAssertMapContainVal failed, map = %v, val = %v", file, line, m, val)
}
}
}
func tAssertMapNotContain(t testing.TB, m, key, val interface{}, args ...interface{}) {
mapVal := reflect.ValueOf(m)
if mapVal.Kind() != reflect.Map {
panic(fmt.Sprintf("tAssertMapNotContain called with non-map value of type %T", m))
}
elemVal := mapVal.MapIndex(reflect.ValueOf(key))
if elemVal.IsValid() && reflect.DeepEqual(elemVal.Interface(), val) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMapNotContain failed, map = %v, key = %v, val = %v, %s", file, line, m, key, val, msg)
} else {
t.Fatalf("%s:%d: tAssertMapNotContain failed, map = %v, key = %v, val = %v", file, line, m, key, val)
}
}
}
func tAssertMapNotContainKey(t testing.TB, m, key interface{}, args ...interface{}) {
mapVal := reflect.ValueOf(m)
if mapVal.Kind() != reflect.Map {
panic(fmt.Sprintf("tAssertMapNotContainKey called with non-map value of type %T", m))
}
elemVal := mapVal.MapIndex(reflect.ValueOf(key))
if elemVal.IsValid() {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMapNotContainKey failed, map = %v, key = %v, %s", file, line, m, key, msg)
} else {
t.Fatalf("%s:%d: tAssertMapNotContainKey failed, map = %v, key = %v", file, line, m, key)
}
}
}
func tAssertMapNotContainVal(t testing.TB, m, val interface{}, args ...interface{}) {
mapVal := reflect.ValueOf(m)
if mapVal.Kind() != reflect.Map {
panic(fmt.Sprintf("tAssertMapNotContainVal called with non-map value of type %T", m))
}
var contained bool
for _, key := range mapVal.MapKeys() {
elemVal := mapVal.MapIndex(key)
if elemVal.IsValid() && reflect.DeepEqual(elemVal.Interface(), val) {
contained = true
break
}
}
if contained {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertMapNotContainVal failed, map = %v, val = %v, %s", file, line, m, val, msg)
} else {
t.Fatalf("%s:%d: tAssertMapNotContainVal failed, map = %v, val = %v", file, line, m, val)
}
}
}
func tAssertZero(t testing.TB, val interface{}, args ...interface{}) {
if !reflect.DeepEqual(reflect.Zero(reflect.TypeOf(val)).Interface(), val) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertZero failed, val = %v, %s", file, line, val, msg)
} else {
t.Fatalf("%s:%d: tAssertZero failed, val = %v", file, line, val)
}
}
}
func tAssertNotZero(t testing.TB, val interface{}, args ...interface{}) {
if reflect.DeepEqual(reflect.Zero(reflect.TypeOf(val)).Interface(), val) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertNotZero failed, val = %v, %s", file, line, val, msg)
} else {
t.Fatalf("%s:%d: tAssertNotZero failed, val = %v", file, line, val)
}
}
}
func tAssertFileExists(t testing.TB, path string, args ...interface{}) {
if _, err := os.Stat(path); err != nil {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
if err != nil {
t.Fatalf("%s:%d: tAssertFileExists failed, path = %v, err = %v, %s", file, line, path, err, msg)
} else {
t.Fatalf("%s:%d: tAssertFileExists failed, path = %v, %s", file, line, path, msg)
}
} else {
if err != nil {
t.Fatalf("%s:%d: tAssertFileExists failed, path = %v, err = %v", file, line, path, err)
} else {
t.Fatalf("%s:%d: tAssertFileExists failed, path = %v", file, line, path)
}
}
}
}
func tAssertFileNotExists(t testing.TB, path string, args ...interface{}) {
if _, err := os.Stat(path); !os.IsNotExist(err) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
if err != nil {
t.Fatalf("%s:%d: tAssertFileNotExists failed, path = %v, err = %v, %s", file, line, path, err, msg)
} else {
t.Fatalf("%s:%d: tAssertFileNotExists failed, path = %v, %s", file, line, path, msg)
}
} else {
if err != nil {
t.Fatalf("%s:%d: tAssertFileNotExists failed, path = %v, err = %v", file, line, path, err)
} else {
t.Fatalf("%s:%d: tAssertFileNotExists failed, path = %v", file, line, path)
}
}
}
}
func tAssertImplements(t testing.TB, interfaceObj, obj interface{}, args ...interface{}) {
if !reflect.TypeOf(obj).Implements(reflect.TypeOf(interfaceObj).Elem()) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertImplements failed, interface = %T, obj = %T, %s", file, line, interfaceObj, obj, msg)
} else {
t.Fatalf("%s:%d: tAssertImplements failed, interface = %T, obj = %T", file, line, interfaceObj, obj)
}
}
}
func tAssertSameType(t testing.TB, expectedType interface{}, obj interface{}, args ...interface{}) {
if !reflect.DeepEqual(reflect.TypeOf(obj), reflect.TypeOf(expectedType)) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertSameType failed, expected = %T, obj = %T, %s", file, line, expectedType, obj, msg)
} else {
t.Fatalf("%s:%d: tAssertSameType failed, expected = %T, obj = %T", file, line, expectedType, obj)
}
}
}
func tAssertPanic(t testing.TB, f func(), args ...interface{}) {
panicVal := func() (panicVal interface{}) {
defer func() {
panicVal = recover()
}()
f()
return
}()
if panicVal == nil {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertPanic failed, %s", file, line, msg)
} else {
t.Fatalf("%s:%d: tAssertPanic failed", file, line)
}
}
}
func tAssertNotPanic(t testing.TB, f func(), args ...interface{}) {
panicVal := func() (panicVal interface{}) {
defer func() {
panicVal = recover()
}()
f()
return
}()
if panicVal != nil {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertNotPanic failed, panic = %v, %s", file, line, panicVal, msg)
} else {
t.Fatalf("%s:%d: tAssertNotPanic failed, panic = %v", file, line, panicVal)
}
}
}
func tAssertEQ(t testing.TB, expected, got interface{}, args ...interface{}) {
if !reflect.DeepEqual(expected, got) && !tIsNumberEqual(expected, got) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertEQ failed, expected = %v, got = %v, %s", file, line, expected, got, msg)
} else {
t.Fatalf("%s:%d: tAssertEQ failed, expected = %v, got = %v", file, line, expected, got)
}
}
}
func tAssertNE(t testing.TB, expected, got interface{}, args ...interface{}) {
if reflect.DeepEqual(expected, got) || tIsNumberEqual(expected, got) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertNE failed, expected = %v, got = %v, %s", file, line, expected, got, msg)
} else {
t.Fatalf("%s:%d: tAssertNE failed, expected = %v, got = %v", file, line, expected, got)
}
}
}
func tAssertLE(t testing.TB, a, b int, args ...interface{}) {
if !(a <= b) {
file, line := tCallerFileLine(1)
if msg := fmt.Sprint(args...); msg != "" {
t.Fatalf("%s:%d: tAssertLE failed, expected %v <= %v, %s", file, line, a, b, msg)
} else {
t.Fatalf("%s:%d: tAssertLE failed, expected %v <= %v", file, line, a, b)
}
}
}