-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathassert.go
More file actions
134 lines (115 loc) · 3.57 KB
/
Copy pathassert.go
File metadata and controls
134 lines (115 loc) · 3.57 KB
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
package restest
import (
"encoding/json"
"fmt"
"reflect"
"testing"
res "github.com/jirenius/go-res"
)
// AssertEqualJSON expects that a and b json marshals into equal values, and
// returns true if they do, otherwise logs a fatal error and returns false.
func AssertEqualJSON(t *testing.T, name string, result, expected interface{}, ctx ...interface{}) bool {
aa, aj := jsonMap(result)
bb, bj := jsonMap(expected)
if !reflect.DeepEqual(aa, bb) {
t.Fatalf("expected %s to be:\n\t%s\nbut got:\n\t%s%s", name, bj, aj, ctxString(ctx))
return false
}
return true
}
// AssertTrue expects that a condition is true.
func AssertTrue(t *testing.T, expectation string, isTrue bool, ctx ...interface{}) bool {
if !isTrue {
t.Fatalf("expected %s%s", expectation, ctxString(ctx))
return false
}
return true
}
// AssertNoError expects that err is nil, otherwise logs an error
// with t.Fatalf
func AssertNoError(t *testing.T, err error, ctx ...interface{}) {
if err != nil {
t.Fatalf("expected no error but got:\n%s%s", err, ctxString(ctx))
}
}
// AssertError expects that err is not nil, otherwise logs an error
// with t.Fatalf
func AssertError(t *testing.T, err error, ctx ...interface{}) {
if err == nil {
t.Fatalf("expected an error but got none%s", ctxString(ctx))
}
}
// AssertResError expects that err is of type *res.Error and matches rerr.
func AssertResError(t *testing.T, err error, rerr *res.Error, ctx ...interface{}) {
AssertError(t, err, ctx...)
v, ok := err.(*res.Error)
if !ok {
t.Fatalf("expected error to be of type *res.Error%s", ctxString(ctx))
}
AssertEqualJSON(t, "error", v, rerr, ctx...)
}
// AssertErrorCode expects that err is of type *res.Error with given code.
func AssertErrorCode(t *testing.T, err error, code string, ctx ...interface{}) {
AssertError(t, err, ctx...)
v, ok := err.(*res.Error)
if !ok {
t.Fatalf("expected error to be of type *res.Error%s", ctxString(ctx))
}
AssertEqualJSON(t, "error code", v.Code, code, ctx...)
}
// AssertPanic expects the callback function to panic, otherwise
// logs an error with t.Errorf
func AssertPanic(t *testing.T, cb func(), ctx ...interface{}) {
defer func() {
v := recover()
if v == nil {
t.Errorf("expected callback to panic, but it didn't%s", ctxString(ctx))
}
}()
cb()
}
// AssertPanicNoRecover expects the callback function to panic, otherwise
// logs an error with t.Errorf. Does not recover from the panic
func AssertPanicNoRecover(t *testing.T, cb func(), ctx ...interface{}) {
panicking := true
defer func() {
if !panicking {
t.Errorf(`expected callback to panic, but it didn't%s`, ctxString(ctx))
}
}()
cb()
panicking = false
}
// AssertNil expects that a value is nil, otherwise it
// logs an error with t.Fatalf.
func AssertNil(t *testing.T, v interface{}, ctx ...interface{}) {
if v != nil && !reflect.ValueOf(v).IsNil() {
t.Fatalf("expected non-nil but got nil%s", ctxString(ctx))
}
}
// AssertNotNil expects that a value is non-nil, otherwise it
// logs an error with t.Fatalf.
func AssertNotNil(t *testing.T, v interface{}, ctx ...interface{}) {
if v == nil || reflect.ValueOf(v).IsNil() {
t.Fatalf("expected nil but got %+v%s", v, ctxString(ctx))
}
}
func ctxString(ctx []interface{}) string {
if len(ctx) == 0 {
return ""
}
return "\nin " + fmt.Sprint(ctx...)
}
func jsonMap(v interface{}) (interface{}, []byte) {
var err error
j, err := json.Marshal(v)
if err != nil {
panic("test: error marshaling value: " + err.Error())
}
var m interface{}
err = json.Unmarshal(j, &m)
if err != nil {
panic("test: error unmarshaling value: " + err.Error())
}
return m, j
}