-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helpers_test.go
106 lines (85 loc) · 1.63 KB
/
test_helpers_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
package fixenv
import (
"encoding/json"
"reflect"
"testing"
)
func requireTrue(t *testing.T, val bool) {
t.Helper()
if !val {
t.Fatal("Must be true")
}
}
func requireFalse(t *testing.T, val bool) {
t.Helper()
if val {
t.Fatal("Must be false")
}
}
func requireEquals(t *testing.T, v1, v2 interface{}) {
t.Helper()
if !reflect.DeepEqual(v1, v2) {
t.Fatal("Must be equals")
}
}
func requireNotEquals(t *testing.T, v1, v2 interface{}) {
if reflect.DeepEqual(v1, v2) {
t.Fatal("Must be not equals")
}
}
func isError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatal("Must be error")
}
}
func noError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("err must be nil, but got: %+v", err)
}
}
func requireNil(t *testing.T, v interface{}) {
t.Helper()
if v == nil {
return
}
val := reflect.ValueOf(v)
if !val.IsNil() {
t.Fatal("Must be nil")
}
}
func requireNotNil(t *testing.T, v interface{}) {
t.Helper()
if v == nil {
return
}
val := reflect.ValueOf(v)
if val.IsNil() {
t.Fatal("Must be not nil")
}
}
func requirePanic(t *testing.T, f func()) {
t.Helper()
defer func() {
_ = recover()
}()
f()
t.Fatal("the function must raise panic")
}
func requireJSONEquals(t *testing.T, v1 string, v2 string) {
t.Helper()
var vj1 interface{}
var vj2 interface{}
err := json.Unmarshal([]byte(v1), &vj1)
noError(t, err)
err = json.Unmarshal([]byte(v2), &vj2)
noError(t, err)
data1, err := json.Marshal(vj1)
noError(t, err)
data2, err := json.Marshal(vj2)
noError(t, err)
normalized1 := string(data1)
normalized2 := string(data2)
requireEquals(t, normalized1, normalized2)
}