Debian Codegraph @

then/basic.go

raw
1 package then
2
3 import (
4 "errors"
5 "strings"
6 "testing"
7 )
8
9 // Equals compares two values, in some rare cases due to generic limitations
10 // you may have to use `reflect.DeepEquals` instead.
11 func Equals[T comparable](t *testing.T, expected, actual T) {
12 t.Helper()
13
14 if expected != actual {
15 t.Logf("expected '%v' to equal '%v'", expected, actual)
16 t.FailNow()
17 }
18 }
19
20 // MapEquals compares two map values for same length and same key+value pairs.
21 func MapEquals[M1, M2 ~map[K]V, K, V comparable](t *testing.T, expected M1, actual M2) {
22 t.Helper()
23
24 if len(expected) != len(actual) {
25 t.Logf("length of expected does not equal actual: %v != %v", len(expected), len(actual))
26 t.FailNow()
27 }
28
29 for k, v1 := range expected {
30 v2, ok := actual[k]
31 if !ok {
32 t.Logf(
33 "actual map is missing key '%v', expected value: '%v'",
34 k,
35 v1,
36 )
37 t.FailNow()
38 }
39
40 if v1 != v2 {
41 t.Logf(
42 "expected value does not equal actual of key '%v': expected '%v' != '%v'",
43 k,
44 v1,
45 v2,
46 )
47 t.FailNow()
48 }
49 }
50 }
51
52 // SliceLen compares the length of a slice to a value.
53 func SliceLen[T any](t *testing.T, expected int, actual []T) {
54 t.Helper()
55
56 if expected != len(actual) {
57 t.Logf("expected does not equal len actual: %d != %v", expected, len(actual))
58 t.FailNow()
59 }
60 }
61
62 // MapLen compares the count of key values in a map.
63 func MapLen[M1 ~map[K]V, K comparable, V any](t *testing.T, expected int, actual M1) {
64 t.Helper()
65
66 if expected != len(actual) {
67 t.Logf("expected does not equal len actual: %d != %v", expected, len(actual))
68 t.FailNow()
69 }
70 }
71
72 // SliceEquals compares two slices for same length and same values at each index.
73 func SliceEquals[T comparable](t *testing.T, expected, actual []T) {
74 t.Helper()
75
76 if len(expected) != len(actual) {
77 t.Logf("length of expected does not equal actual: %v != %v", len(expected), len(actual))
78 t.FailNow()
79 }
80
81 for i := range expected {
82 if expected[i] != actual[i] {
83 t.Logf(
84 "expected value does not equal actual at index %v: expected '%v' != '%v'",
85 i,
86 expected[i],
87 actual[i],
88 )
89 t.FailNow()
90 }
91 }
92 }
93
94 // Nil compares a value to nil, in some cases you may need to do `Equals(t, value, nil)`
95 func Nil(t *testing.T, value any) {
96 t.Helper()
97
98 if value != nil {
99 t.Logf("expected '%v' to be nil", value)
100 t.FailNow()
101 }
102 }
103
104 // NotNil compares a value is not nil.
105 func NotNil(t *testing.T, value any) {
106 t.Helper()
107
108 if value == nil {
109 t.Logf("expected '%v' not to be nil", value)
110 t.FailNow()
111 }
112 }
113
114 // Err checks if our actual error is the expected error or wrapped in the expected error.
115 func Err(t *testing.T, expected, actual error) {
116 t.Helper()
117
118 if !errors.Is(actual, expected) {
119 t.Logf("expected '%v' to be '%v'", expected, actual)
120 t.FailNow()
121 }
122 }
123
124 // Panic checks if our func would panic.
125 func Panic(t *testing.T, f func()) {
126 defer func() {
127 // we don't care what the value is, only that we had to recover
128 _ = recover()
129 }()
130
131 f()
132 t.Error("expected func to panic")
133 t.FailNow()
134 }
135
136 // True checks if a value is true.
137 func True(t *testing.T, value bool) {
138 t.Helper()
139
140 if !value {
141 t.Error("expected value to be true")
142 t.FailNow()
143 }
144 }
145
146 // False checks if a value is false.
147 func False(t *testing.T, value bool) {
148 t.Helper()
149
150 if value {
151 t.Error("expected value to be false")
152 t.FailNow()
153 }
154 }
155
156 // Contains checks if our substring is contained in the full string
157 func Contains(t *testing.T, sub, full string) {
158 t.Helper()
159
160 if !strings.Contains(full, sub) {
161 t.Logf("expected '%v' to be in '%v'", sub, full)
162 t.FailNow()
163 }
164 }