aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/zeebo/assert/assert.go
blob: 787920793d741bdc8ded698fd4c5e4576a8af65d (plain) (blame)
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
package assert

import (
	"bytes"
	"reflect"
	"testing"
)

func NoError(t testing.TB, err error) {
	if err != nil {
		t.Helper()
		t.Fatalf("%+v", err)
	}
}

func Error(t testing.TB, err error) {
	if err == nil {
		t.Helper()
		t.Fatal("expected an error")
	}
}

func Equal(t testing.TB, a, b interface{}) {
	if ta, tb := reflect.TypeOf(a), reflect.TypeOf(b); ta != nil && tb != nil {
		if ta.Comparable() && tb.Comparable() {
			if a == b || literalConvert(a) == literalConvert(b) {
				return
			}
		}
	}

	if deepEqual(a, b) {
		return
	}

	t.Helper()
	t.Fatalf("%#v != %#v", a, b)
}

func NotEqual(t testing.TB, a, b interface{}) {
	if ta, tb := reflect.TypeOf(a), reflect.TypeOf(b); ta != nil && tb != nil {
		if ta.Comparable() && tb.Comparable() {
			if !(a == b || literalConvert(a) == literalConvert(b)) {
				return
			}
		}
	}

	if !deepEqual(a, b) {
		return
	}

	t.Helper()
	t.Fatalf("%#v == %#v", a, b)
}

func DeepEqual(t testing.TB, a, b interface{}) {
	if !deepEqual(a, b) {
		t.Helper()
		t.Fatalf("%#v != %#v", a, b)
	}
}

func That(t testing.TB, v bool) {
	if !v {
		t.Helper()
		t.Fatal("expected condition failed")
	}
}

func True(t testing.TB, v bool) {
	if !v {
		t.Helper()
		t.Fatal("expected condition failed")
	}
}

func False(t testing.TB, v bool) {
	if v {
		t.Helper()
		t.Fatal("expected condition failed")
	}
}

func Nil(t testing.TB, a interface{}) {
	if a == nil {
		return
	}

	rv := reflect.ValueOf(a)
	if !canNil(rv) {
		t.Helper()
		t.Fatalf("%#v cannot be nil", a)
	}
	if !rv.IsNil() {
		t.Helper()
		t.Fatalf("%#v != nil", a)
	}
}

func NotNil(t testing.TB, a interface{}) {
	if a == nil {
		t.Helper()
		t.Fatal("expected not nil")
	}

	rv := reflect.ValueOf(a)
	if !canNil(rv) {
		return
	}
	if rv.IsNil() {
		t.Helper()
		t.Fatalf("%#v == nil", a)
	}
}

func deepEqual(a, b interface{}) bool {
	ab, aok := a.([]byte)
	bb, bok := b.([]byte)
	if aok && bok {
		return bytes.Equal(ab, bb)
	}
	return reflect.DeepEqual(a, b)
}

func canNil(rv reflect.Value) bool {
	switch rv.Kind() {
	case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
		return true
	}
	return false
}

func literalConvert(val interface{}) interface{} {
	switch val := reflect.ValueOf(val); val.Kind() {
	case reflect.Bool:
		return val.Bool()

	case reflect.String:
		return val.Convert(reflect.TypeOf("")).Interface()

	case reflect.Float32, reflect.Float64:
		return val.Float()

	case reflect.Complex64, reflect.Complex128:
		return val.Complex()

	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		if asInt := val.Int(); asInt < 0 {
			return asInt
		}
		return val.Convert(reflect.TypeOf(uint64(0))).Uint()

	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return val.Uint()

	default:
		return val
	}
}