-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertions.go
More file actions
203 lines (160 loc) · 4.94 KB
/
assertions.go
File metadata and controls
203 lines (160 loc) · 4.94 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
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
package assert
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/gohobby/assert/tablewriter"
"github.com/kr/pretty"
)
// Formatting can be controlled with these flags.
const (
// Print a Go-syntax representation of the value.
GoSyntax uint = 1 << iota
// Pretty-printing of the value.
Pretty
)
func ParseMsgAndArgs(args ...interface{}) (msgAndArgs []interface{}, format uint) {
msgAndArgs = append(make([]interface{}, 0), args...)
format = 0
for k, arg := range args {
if val, ok := arg.(uint); ok {
switch val {
case GoSyntax, Pretty:
k -= len(args) - len(msgAndArgs)
msgAndArgs = append(msgAndArgs[:k], msgAndArgs[k+1:]...)
format = val
}
}
}
return msgAndArgs, format
}
// Equal asserts that two objects are equal.
//
// assert.Equal(t, 123, 123)
//
// Function equality cannot be determined and will always fail.
func Equal(t testing.TB, expected, actual interface{}, msgAndArgs ...interface{}) bool {
t.Helper()
msgAndArgs, format := ParseMsgAndArgs(msgAndArgs...)
if err := validateEqualArgs(expected, actual); err != nil {
return Fail(t, fmt.Sprintf("Invalid operation: %#v == %#v (%s)", expected, actual, err), nil, msgAndArgs...)
}
if reflect.DeepEqual(expected, actual) {
return true
}
if expected == nil {
return Fail(t, fmt.Sprintf("Expected nil, but got: %#v", actual), nil, msgAndArgs...)
}
return Fail(t, "Not equal", func(formatter *tablewriter.Table) {
switch format {
case GoSyntax:
formatter.Writef("\nExpect:\t%#v", expected)
formatter.Writef("\nActual:\t%#v", actual)
case Pretty:
formatter.Writef("\nExpect:\t%s", strings.ReplaceAll(pretty.Sprintf("%# v", expected), "\n", "\n\t"))
formatter.Writef("\nActual:\t%s", strings.ReplaceAll(pretty.Sprintf("%# v", actual), "\n", "\n\t"))
default:
formatter.Writef("\nExpect:\t%+v\t(%T)", expected, expected)
formatter.Writef("\nActual:\t%+v\t(%T)", actual, actual)
}
}, msgAndArgs...)
}
// NotEqual asserts that the specified values are NOT equal.
//
// assert.NotEqual(t, obj1, obj2)
//
// Function equality cannot be determined and will always fail.
func NotEqual(t testing.TB, expected, actual interface{}, msgAndArgs ...interface{}) bool {
t.Helper()
if err := validateEqualArgs(expected, actual); err != nil {
return Fail(t, fmt.Sprintf("Invalid operation: %#v == %#v (%s)", expected, actual, err), nil, msgAndArgs...)
}
if !reflect.DeepEqual(expected, actual) {
return true
}
if expected == nil {
return Fail(t, "Expected value not to be nil.", nil, msgAndArgs...)
}
return Fail(t, fmt.Sprintf("Should not be: %#v", actual), nil, msgAndArgs...)
}
// True asserts that the specified value is true.
//
// assert.True(t, myBool)
func True(t *testing.T, value bool, msgAndArgs ...interface{}) bool {
if !value {
t.Helper()
return Fail(t, "Should be true", nil, msgAndArgs...)
}
return true
}
// False asserts that the specified value is false.
//
// assert.False(t, myBool)
func False(t *testing.T, value bool, msgAndArgs ...interface{}) bool {
if value {
t.Helper()
return Fail(t, "Should be false", nil, msgAndArgs...)
}
return true
}
// Nil asserts that the specified object is nil.
//
// assert.Nil(t, err)
func Nil(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool {
if isNil(object) {
return true
}
t.Helper()
return Fail(t, fmt.Sprintf("Expected nil, but got: %#v", object), nil, msgAndArgs...)
}
// NotNil asserts that the specified object is not nil.
//
// assert.NotNil(t, err)
func NotNil(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool {
if !isNil(object) {
return true
}
t.Helper()
return Fail(t, "Expected value not to be nil.", nil, msgAndArgs...)
}
// Implements asserts that an object is implemented by the specified interface.
//
// assert.Implements(t, (*MyInterface)(nil), new(MyObject))
func Implements(t *testing.T, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool {
t.Helper()
interfaceType := reflect.TypeOf(interfaceObject).Elem()
if object == nil {
return Fail(t, fmt.Sprintf("Cannot check if nil implements %v", interfaceType), nil, msgAndArgs...)
}
if !reflect.TypeOf(object).Implements(interfaceType) {
return Fail(t, fmt.Sprintf("%T must implement %v", object, interfaceType), nil, msgAndArgs...)
}
return true
}
// Fail reports a failure
func Fail(
t testing.TB,
failureMessage string,
callback func(formatter *tablewriter.Table),
msgAndArgs ...interface{},
) bool {
t.Helper()
stackTrace := StackTrace(3) // StackTrace + Fail + public function
table := tablewriter.New()
table.WriteRow("Test:", t.Name())
table.Writef("\nTrace:\t%s", strings.Join(stackTrace, "\n\t"))
if len(failureMessage) != 0 {
table.WriteRow("Error:", failureMessage)
}
msgAndArgs, _ = ParseMsgAndArgs(msgAndArgs...)
message := messageFromMsgAndArgs(msgAndArgs...)
if len(message) > 0 {
table.WriteRow("Message:", message)
}
if callback != nil {
callback(table)
}
t.Error(table)
return false
}