/
assertion_helper.go
237 lines (203 loc) · 5.32 KB
/
assertion_helper.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package internal
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"reflect"
"regexp"
"strings"
"time"
)
// CompletesIn returns if a function completes in a specific time.
func CompletesIn(duration time.Duration, f func()) bool {
done := make(chan bool)
go func() {
f()
done <- true
}()
select {
case <-time.After(duration):
return false
case <-done:
return true
}
}
// IsEqual checks if two objects are equal.
func IsEqual(expected any, actual any) bool {
if expected == nil || actual == nil {
return expected == actual
}
expectedB, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
actualB, ok := actual.([]byte)
if !ok {
return false
}
if expectedB == nil || actualB == nil {
return expectedB == nil && actualB == nil
}
return bytes.Equal(expectedB, actualB)
}
// HasEqualValues checks if two objects have equal values.
func HasEqualValues(expected any, actual any) bool {
if IsEqual(expected, actual) {
return true
}
actualType := reflect.TypeOf(actual)
if actualType == nil {
return false
}
expectedValue := reflect.ValueOf(expected)
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual)
}
return false
}
// DoesContain checks that ab objects contains an element.
func DoesContain(object, element any) bool {
objectValue := reflect.ValueOf(object)
objectKind := reflect.TypeOf(object).Kind()
switch objectKind {
case reflect.String:
return strings.Contains(objectValue.String(), reflect.ValueOf(element).String())
case reflect.Map:
default:
for i := 0; i < objectValue.Len(); i++ {
if IsEqual(objectValue.Index(i).Interface(), element) {
return true
}
}
}
return false
}
// AssertCompareHelper option: 1 = increasing, 0 = equal, -1 = decreasing
func AssertCompareHelper(t testRunner, object any, option int, msg ...any) {
if test, ok := t.(helper); ok {
test.Helper()
}
defer func() {
if e := recover(); e != nil {
Fail(t, "The 'object' !!must be a numeric slice!!.", NewObjectsSingleUnknown(object), msg...)
}
}()
v := reflect.ValueOf(object)
objKind := v.Kind()
if objKind != reflect.Slice && objKind != reflect.Array {
Fail(t, "The 'object' !!is neither a slice nor an array!!.", NewObjectsSingleUnknown(object), msg...)
return
}
if v.Len() < 2 {
Fail(t, "The 'object' !!is not long enough!!.", NewObjectsSingleUnknown(object), msg...)
return
}
firstValue := v.Index(0).Interface()
var ok bool
switch firstValue.(type) {
case int, int8, int16, int32, int64:
ok = CompareTwoValuesInASlice(v, func(a, b reflect.Value) bool {
if option == 1 {
return a.Int() < b.Int()
} else if option == 0 {
return a.Int() == b.Int()
} else if option == -1 {
return a.Int() > b.Int()
}
return false
})
case uint, uint8, uint16, uint32, uint64:
ok = CompareTwoValuesInASlice(v, func(a, b reflect.Value) bool {
if option == 1 {
return a.Uint() < b.Uint()
} else if option == 0 {
return a.Uint() == b.Uint()
} else if option == -1 {
return a.Uint() > b.Uint()
}
return false
})
case float32, float64:
ok = CompareTwoValuesInASlice(v, func(a, b reflect.Value) bool {
if option == 1 {
return a.Float() < b.Float()
} else if option == 0 {
return a.Float() == b.Float()
} else if option == -1 {
return a.Float() > b.Float()
}
return false
})
default:
Fail(t, "The 'object' !!must be a numeric slice!!.", NewObjectsSingleUnknown(object), msg...)
}
if !ok {
var order string
switch option {
case 1:
order = "increasing"
case 0:
order = "equal"
case -1:
order = "decreasing"
}
Fail(t, fmt.Sprintf("The 'object' !!is not %s!!.", order), NewObjectsSingleUnknown(object), msg...)
}
}
func AssertRegexpHelper(t testRunner, regex any, txt any, shouldMatch bool, msg ...any) {
if test, ok := t.(helper); ok {
test.Helper()
}
regexString := fmt.Sprint(regex)
txtString := fmt.Sprint(txt)
match, _ := regexp.MatchString(regexString, txtString)
if shouldMatch != match {
failText := "!!does not match!! the string."
if !shouldMatch {
failText = "!!does match!! the string, but !!should not!!."
}
Fail(t, "The regex pattern "+failText, Objects{
NewObjectsSingleNamed("Regex Pattern", regexString+"\n")[0],
NewObjectsSingleNamed("String", txtString+"\n")[0],
}, msg...)
}
}
// AssertDirEmptyHelper checks for io.EOF to determine if directory empty or not
func AssertDirEmptyHelper(t testRunner, dir string) bool {
if test, ok := t.(helper); ok {
test.Helper()
}
f, err := os.Open(dir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return true
}
Fail(t, "Error opening directory specified", NewObjectsSingleNamed("dir", dir), err.Error())
}
defer f.Close()
_, err = f.Readdirnames(1)
return errors.Is(err, io.EOF)
}
func isSubset[T comparable](list []T, subset []T) bool {
for _, element := range subset {
contains := DoesContain(list, element)
if !contains {
return false
}
}
return true
}
func HasSameElements[T comparable](expected []T, actual []T) bool {
return isSubset(expected, actual) && isSubset(actual, expected)
}
func IsSubset[T comparable](t testRunner, list []T, subset []T) bool {
if test, ok := t.(helper); ok {
test.Helper()
}
if subset == nil {
return true
}
return isSubset(list, subset)
}