forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensure_test.go
85 lines (78 loc) · 1.94 KB
/
ensure_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
package goavro
// NOTE: This file was copied from https://github.com/karrick/gorill
import (
"fmt"
"strings"
"testing"
)
func ensureBuffer(tb testing.TB, buf []byte, n int, want string) {
tb.Helper()
if got, want := n, len(want); got != want {
tb.Fatalf("GOT: %v; WANT: %v", got, want)
}
if got, want := string(buf[:n]), want; got != want {
tb.Errorf("GOT: %v; WANT: %v", got, want)
}
}
func ensureError(tb testing.TB, err error, contains ...string) {
tb.Helper()
if len(contains) == 0 || (len(contains) == 1 && contains[0] == "") {
if err != nil {
tb.Fatalf("GOT: %v; WANT: %v", err, contains)
}
} else if err == nil {
tb.Errorf("GOT: %v; WANT: %v", err, contains)
} else {
for _, stub := range contains {
if stub != "" && !strings.Contains(err.Error(), stub) {
tb.Errorf("GOT: %v; WANT: %q", err, stub)
}
}
}
}
func ensurePanic(tb testing.TB, want string, callback func()) {
tb.Helper()
defer func() {
r := recover()
if r == nil {
tb.Fatalf("GOT: %v; WANT: %v", r, want)
return
}
if got := fmt.Sprintf("%v", r); got != want {
tb.Fatalf("GOT: %v; WANT: %v", got, want)
}
}()
callback()
}
// ensureNoPanic prettifies the output so one knows which test case caused a
// panic.
func ensureNoPanic(tb testing.TB, label string, callback func()) {
tb.Helper()
defer func() {
if r := recover(); r != nil {
tb.Fatalf("TEST: %s: GOT: %v", label, r)
}
}()
callback()
}
func ensureStringSlicesMatch(tb testing.TB, actual, expected []string) {
tb.Helper()
if got, want := len(actual), len(expected); got != want {
tb.Errorf("GOT: %v; WANT: %v", got, want)
}
la := len(actual)
le := len(expected)
for i := 0; i < la || i < le; i++ {
if i < la {
if i < le {
if got, want := actual[i], expected[i]; got != want {
tb.Errorf("GOT: %q; WANT: %q", got, want)
}
} else {
tb.Errorf("GOT: %q (extra)", actual[i])
}
} else if i < le {
tb.Errorf("WANT: %q (missing)", expected[i])
}
}
}