-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
81 lines (70 loc) · 1.81 KB
/
util_test.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
package serrors_test
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"runtime/debug"
"slices"
"testing"
"github.com/Eun/serrors"
)
func CompareErrorStack(t *testing.T, expected, actual []serrors.ErrorStack) {
encode := func(v any) (string, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", "\t")
if err := enc.Encode(v); err != nil {
return "", serrors.Wrap(err, "unable to encode").With("obj", fmt.Sprintf("%+v", v))
}
return buf.String(), nil
}
expectedStack, err := encode(expected)
Nil(t, err)
actualStack, err := encode(actual)
Nil(t, err)
Equal(t, expectedStack, actualStack)
}
func Equal(t *testing.T, expected, actual any) {
if expected == nil && actual == nil {
return
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("expected %+v, but was %+v\n%s", expected, actual, string(debug.Stack()))
}
}
func NotEqual(t *testing.T, expected, actual any) {
if expected == nil && actual == nil {
t.Fatalf("expected not %+v, but was %+v\n%s", expected, actual, string(debug.Stack()))
}
if reflect.DeepEqual(expected, actual) {
t.Fatalf("expected not %+v, but was %+v\n%s", expected, actual, string(debug.Stack()))
}
}
func isNil(actual any) bool {
if actual == nil {
return true
}
value := reflect.ValueOf(actual)
kind := value.Kind()
isNilableKind := slices.Contains(
[]reflect.Kind{
reflect.Chan, reflect.Func,
reflect.Interface, reflect.Map,
reflect.Ptr, reflect.Slice, reflect.UnsafePointer},
kind)
if isNilableKind && value.IsNil() {
return true
}
return false
}
func Nil(t *testing.T, actual any) {
if !isNil(actual) {
t.Fatalf("expected %+v to be nil\n%s", actual, string(debug.Stack()))
}
}
func NotNil(t *testing.T, actual any) {
if isNil(actual) {
t.Fatalf("expected %+v to be not nil\n%s", actual, string(debug.Stack()))
}
}