-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassert.go
161 lines (127 loc) · 2.72 KB
/
assert.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
package odize
import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"
)
// Assert value is nil
//
// Example:
//
// AssertNil(t, myValue)
func AssertNil(t *testing.T, value any) {
t.Helper()
if !isNil(value) {
log(t, decorateDiff("<nil>", value))
}
}
// AssertTrue checks if value is true
//
// Example:
//
// AssertTrue(t, methodReturnsTrue())
func AssertTrue(t *testing.T, value bool) {
t.Helper()
if !value {
log(t, decorateDiff(true, value))
}
}
// AssertFalse checks if value is true
//
// Example:
//
// AssertFalse(t, methodReturnsFalse())
func AssertFalse(t *testing.T, value bool) {
t.Helper()
if value {
log(t, decorateDiff(false, value))
}
}
// AssertNoError checks if error is nil
//
// Example:
//
// AssertNoError(t, err)
func AssertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
log(t, decorateDiff("<nil>", err))
}
}
// AssertError checks if error is not nil
//
// Example:
//
// AssertError(t, err)
func AssertError(t *testing.T, err error) {
t.Helper()
if err == nil {
log(t, decorateDiff("<error>", err))
}
}
// AssertEqual checks if two values are equal
//
// Example:
//
// AssertEqual(t, "a", "b")
func AssertEqual(t *testing.T, expected any, actual any) {
t.Helper()
if !isEqual(expected, actual) {
logf(t, decorateDiff(expected, actual))
}
}
// isNil - Check if a value is nil
func isNil(value any) bool {
if value == nil {
return true
}
v := reflect.ValueOf(value)
kind := v.Kind()
// check chan, func, interface, map, pointer, or slice value is nil
if (kind >= reflect.Chan && kind <= reflect.Slice) && v.IsNil() {
return true
}
return false
}
// isEqual - Check if two values are equal
func isEqual(expected any, actual any) bool {
if isNil(expected) && isNil(actual) {
return true
}
if isNil(expected) || isNil(actual) {
return false
}
if reflect.DeepEqual(expected, actual) {
return true
}
valueExpected := reflect.ValueOf(expected)
valueActual := reflect.ValueOf(actual)
return valueExpected == valueActual
}
func decorateDiff(expected, actual any) string {
buf := new(bytes.Buffer)
exp := decorateBlock("Expected", fmt.Sprintf("%v", expected), "+")
got := decorateBlock("Got", fmt.Sprintf("%v", actual), "-")
buf.WriteString(exp)
buf.WriteString(got)
return buf.String()
}
func decorateBlock(label string, content string, lineDecoration string) string {
buf := new(bytes.Buffer)
buf.WriteByte('\n')
buf.WriteString(fmt.Sprintf("%s:\n", label))
lines := strings.Split(content, "\n")
if l := len(lines); l > 1 && lines[l-1] == "" {
lines = lines[:l-1]
}
for i, line := range lines {
if i > 0 {
buf.WriteString("\n")
}
buf.WriteString(fmt.Sprintf("%s\t%v", lineDecoration, line))
}
buf.WriteByte('\n')
return buf.String()
}