-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
assert.go
55 lines (46 loc) · 1.26 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
package lungo
import (
"reflect"
"testing"
)
func isNil(i any) bool {
if i == nil {
return true
}
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Chan,
reflect.Func,
reflect.Map,
reflect.Ptr,
reflect.UnsafePointer,
reflect.Interface,
reflect.Slice:
return v.IsNil()
}
return false
}
func assertNil(t *testing.T, actual any) {
if !isNil(actual) {
t.Errorf("Test %s: Expected value to be nil, Received `%v` (type %v)", t.Name(), actual, reflect.TypeOf(actual))
}
}
func assertNotNil(t *testing.T, actual any) {
if isNil(actual) {
t.Errorf("Test %s: Expected value to not be nil, Received `%v` (type %v)", t.Name(), actual, reflect.TypeOf(actual))
}
}
func assertEqual(t *testing.T, expected, actual any) {
if (isNil(expected) && isNil(actual)) || reflect.DeepEqual(expected, actual) {
return
}
t.Errorf("Test %s: Expected `%v` (type %v), Received `%v` (type %v)", t.Name(), expected, reflect.TypeOf(expected), actual, reflect.TypeOf(actual))
}
func assertPanic(t *testing.T, expected any, f func()) {
defer func() {
if r := recover(); r == nil || r != expected {
t.Errorf("Test %s: Expected Panic `%v` (type %v), Received Panic `%v` (type %v)", t.Name(), expected, reflect.TypeOf(expected), r, reflect.TypeOf(r))
}
}()
f()
}