-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert.go
More file actions
56 lines (43 loc) · 1.1 KB
/
assert.go
File metadata and controls
56 lines (43 loc) · 1.1 KB
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
package typotestcolor
import (
"reflect"
"strings"
"testing"
)
func AssertSameType(t *testing.T, expected any, got any) {
t.Helper()
expectedType := reflect.TypeOf(expected)
gotType := reflect.TypeOf(got)
if expectedType != gotType {
t.Errorf("expected: two variables of the same type, got: %T and %T", expected, got)
}
}
func AssertDifferentTypes(t *testing.T, expected any, got any) {
t.Helper()
if reflect.TypeOf(expected) == reflect.TypeOf(got) {
t.Errorf("expected: two variables of different types, got: %T", expected)
}
}
func AssertError(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Error("expected: an error, got: no error")
}
}
func AssertErrorStrict(t *testing.T, err error, contains string) {
t.Helper()
errMessage := err.Error()
if err != nil && !strings.Contains(errMessage, contains) {
t.Errorf("expected: %s, got: %s", contains, errMessage)
return
}
if err == nil {
t.Error("expected: an error, got: no error")
}
}
func AssertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("expected: no error, got: %s", err.Error())
}
}