Skip to content

Commit 2386a55

Browse files
committed
feat: add map assertion functions.
1 parent 49b188f commit 2386a55

File tree

4 files changed

+405
-0
lines changed

4 files changed

+405
-0
lines changed

builtin.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,112 @@ func NotHasSuffixStringNow(t *testing.T, str, suffix string, message ...any) err
324324
return tryNotHasSuffixString(t, true, str, suffix, message...)
325325
}
326326

327+
// MapHasKey tests whether the map contains the specified key or not, it will fail if the map does
328+
// not contain the key, or the type of the key cannot assign to the type of the key of the map.
329+
//
330+
// assertion.MapHasKey(map[string]int{"a":1}, "a") // success
331+
// assertion.MapHasKey(map[string]int{"a":1}, "b") // fail
332+
// assertion.MapHasKey(map[string]int{"a":1}, 1) // fail
333+
func MapHasKey(t *testing.T, m, key any, message ...any) error {
334+
t.Helper()
335+
336+
return tryMapHasKey(t, false, m, key, message...)
337+
}
338+
339+
// MapHasKeyNow tests whether the map contains the specified key or not, and it will terminate the
340+
// execution if the test fails. It will fail if the map does not contain the key, or the type of
341+
// the key cannot assign to the type of the key of the map.
342+
//
343+
// assertion.MapHasKeyNow(map[string]int{"a":1}, "a") // success
344+
// assertion.MapHasKeyNow(map[string]int{"a":1}, "b") // fail and terminate
345+
// // never run
346+
func MapHasKeyNow(t *testing.T, m, key any, message ...any) error {
347+
t.Helper()
348+
349+
return tryMapHasKey(t, true, m, key, message...)
350+
}
351+
352+
// NotMapHasKey tests whether the map contains the specified key or not, it will fail if the map
353+
// contain the key. It will also set the test result to success if the type of the key cannot
354+
// assign to the type of the key of the map.
355+
//
356+
// assertion.NotMapHasKey(map[string]int{"a":1}, "b") // success
357+
// assertion.NotMapHasKey(map[string]int{"a":1}, 1) // success
358+
// assertion.NotMapHasKey(map[string]int{"a":1}, "a") // fail
359+
func NotMapHasKey(t *testing.T, m, key any, message ...any) error {
360+
t.Helper()
361+
362+
return tryNotMapHasKey(t, false, m, key, message...)
363+
}
364+
365+
// NotMapHasKeyNow tests whether the map contains the specified key or not, it will fail if the map
366+
// contain the key, and it will terminate the execution if the test fails. It will also set the
367+
// test result to success if the type of the key cannot assign to the type of the key of the map.
368+
//
369+
// assertion.NotMapHasKeyNow(map[string]int{"a":1}, "b") // success
370+
// assertion.NotMapHasKeyNow(map[string]int{"a":1}, 1) // success
371+
// assertion.NotMapHasKeyNow(map[string]int{"a":1}, "a") // fail and terminate
372+
// // never run
373+
func NotMapHasKeyNow(t *testing.T, m, key any, message ...any) error {
374+
t.Helper()
375+
376+
return tryNotMapHasKey(t, true, m, key, message...)
377+
}
378+
379+
// MapHasValue tests whether the map contains the specified value or not, it will fail if the map
380+
// does not contain the value, or the type of the value cannot assign to the type of the values of
381+
// the map.
382+
//
383+
// assertion.MapHasValue(map[string]int{"a":1}, 1) // success
384+
// assertion.MapHasValue(map[string]int{"a":1}, 2) // fail
385+
// assertion.MapHasValue(map[string]int{"a":1}, "a") // fail
386+
func MapHasValue(t *testing.T, m, value any, message ...any) error {
387+
t.Helper()
388+
389+
return tryMapHasValue(t, false, m, value, message...)
390+
}
391+
392+
// MapHasValueNow tests whether the map contains the specified value or not, and it will terminate
393+
// the execution if the test fails. It will fail if the map does not contain the value, or the type
394+
// of the value cannot assign to the type of the value of the map.
395+
//
396+
// assertion.MapHasValueNow(map[string]int{"a":1}, 1) // success
397+
// assertion.MapHasValueNow(map[string]int{"a":1}, 2) // fail and terminate
398+
// // never run
399+
func MapHasValueNow(t *testing.T, m, value any, message ...any) error {
400+
t.Helper()
401+
402+
return tryMapHasValue(t, true, m, value, message...)
403+
}
404+
405+
// NotMapHasValue tests whether the map contains the specified value or not, it will fail if the
406+
// map contain the value. It will also set the test result to success if the type of the value
407+
// cannot assign to the type of the value of the map.
408+
//
409+
// assertion.NotMapHasValue(map[string]int{"a":1}, 2) // success
410+
// assertion.NotMapHasValue(map[string]int{"a":1}, "a") // success
411+
// assertion.NotMapHasValue(map[string]int{"a":1}, 1) // fail
412+
func NotMapHasValue(t *testing.T, m, value any, message ...any) error {
413+
t.Helper()
414+
415+
return tryNotMapHasValue(t, false, m, value, message...)
416+
}
417+
418+
// NotMapHasValueNow tests whether the map contains the specified value or not, it will fail if the
419+
// map contain the value, and it will terminate the execution if the test fails. It will also set
420+
// the test result to success if the type of the value cannot assign to the type of the value of
421+
// the map.
422+
//
423+
// assertion.NotMapHasValueNow(map[string]int{"a":1}, 2) // success
424+
// assertion.NotMapHasValueNow(map[string]int{"a":1}, "a") // success
425+
// assertion.NotMapHasValueNow(map[string]int{"a":1}, 1) // fail and terminate
426+
// // never run
427+
func NotMapHasValueNow(t *testing.T, m, value any, message ...any) error {
428+
t.Helper()
429+
430+
return tryNotMapHasValue(t, true, m, value, message...)
431+
}
432+
327433
// Match tests whether the string matches the regular expression or not.
328434
//
329435
// pattern := regexp.MustCompile(`^https?:\/\/`)

error.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const (
2121
defaultErrMessageNotPanic string = "got unwanted error: %v"
2222
defaultErrMessageTrue string = "the expression evaluated to a falsy value"
2323
defaultErrMessageNotTrue string = "the expression evaluated to a truthy value"
24+
defaultErrMessageMapHasKey string = "expect map has key %v"
25+
defaultErrMessageNotMapHasKey string = "expect map has no key %v"
26+
defaultErrMessageMapHasValue string = "expect map has value %v"
27+
defaultErrMessageNotMapHasValue string = "expect map has no value %v"
2428
)
2529

2630
// AssertionError indicates the failure of an assertion.

map.go

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package assert
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
// MapHasKey tests whether the map contains the specified key or not, it will fail if the map does
9+
// not contain the key, or the type of the key cannot assign to the type of the key of the map.
10+
//
11+
// assertion.MapHasKey(map[string]int{"a":1}, "a") // success
12+
// assertion.MapHasKey(map[string]int{"a":1}, "b") // fail
13+
// assertion.MapHasKey(map[string]int{"a":1}, 1) // fail
14+
func (a *Assertion) MapHasKey(m, key any, message ...any) error {
15+
a.Helper()
16+
17+
return tryMapHasKey(a.T, false, m, key, message...)
18+
}
19+
20+
// MapHasKeyNow tests whether the map contains the specified key or not, and it will terminate the
21+
// execution if the test fails. It will fail if the map does not contain the key, or the type of
22+
// the key cannot assign to the type of the key of the map.
23+
//
24+
// assertion.MapHasKeyNow(map[string]int{"a":1}, "a") // success
25+
// assertion.MapHasKeyNow(map[string]int{"a":1}, "b") // fail and terminate
26+
// // never run
27+
func (a *Assertion) MapHasKeyNow(m, key any, message ...any) error {
28+
a.Helper()
29+
30+
return tryMapHasKey(a.T, true, m, key, message...)
31+
}
32+
33+
// NotMapHasKey tests whether the map contains the specified key or not, it will fail if the map
34+
// contain the key. It will also set the test result to success if the type of the key cannot
35+
// assign to the type of the key of the map.
36+
//
37+
// assertion.NotMapHasKey(map[string]int{"a":1}, "b") // success
38+
// assertion.NotMapHasKey(map[string]int{"a":1}, 1) // success
39+
// assertion.NotMapHasKey(map[string]int{"a":1}, "a") // fail
40+
func (a *Assertion) NotMapHasKey(m, key any, message ...any) error {
41+
a.Helper()
42+
43+
return tryNotMapHasKey(a.T, false, m, key, message...)
44+
}
45+
46+
// NotMapHasKeyNow tests whether the map contains the specified key or not, it will fail if the map
47+
// contain the key, and it will terminate the execution if the test fails. It will also set the
48+
// test result to success if the type of the key cannot assign to the type of the key of the map.
49+
//
50+
// assertion.NotMapHasKeyNow(map[string]int{"a":1}, "b") // success
51+
// assertion.NotMapHasKeyNow(map[string]int{"a":1}, 1) // success
52+
// assertion.NotMapHasKeyNow(map[string]int{"a":1}, "a") // fail and terminate
53+
// // never run
54+
func (a *Assertion) NotMapHasKeyNow(m, key any, message ...any) error {
55+
a.Helper()
56+
57+
return tryNotMapHasKey(a.T, true, m, key, message...)
58+
}
59+
60+
// tryMapHasKey tries to test whether the map contains the specified key or not, and it'll fail if
61+
// the map does not contains the specified key.
62+
func tryMapHasKey(
63+
t *testing.T,
64+
failedNow bool,
65+
m, key any,
66+
message ...any,
67+
) error {
68+
t.Helper()
69+
70+
return test(
71+
t,
72+
func() bool { return isMapHasKey(m, key) },
73+
failedNow,
74+
fmt.Sprintf(defaultErrMessageMapHasKey, key),
75+
message...,
76+
)
77+
}
78+
79+
// tryNotMapHasKey tries to test whether the map contains the specified key or not, and it'll fail
80+
// if the map contains the specified key.
81+
func tryNotMapHasKey(
82+
t *testing.T,
83+
failedNow bool,
84+
m, key any,
85+
message ...any,
86+
) error {
87+
t.Helper()
88+
89+
return test(
90+
t,
91+
func() bool { return !isMapHasKey(m, key) },
92+
failedNow,
93+
fmt.Sprintf(defaultErrMessageNotMapHasKey, key),
94+
message...,
95+
)
96+
}
97+
98+
// MapHasValue tests whether the map contains the specified value or not, it will fail if the map
99+
// does not contain the value, or the type of the value cannot assign to the type of the values of
100+
// the map.
101+
//
102+
// assertion.MapHasValue(map[string]int{"a":1}, 1) // success
103+
// assertion.MapHasValue(map[string]int{"a":1}, 2) // fail
104+
// assertion.MapHasValue(map[string]int{"a":1}, "a") // fail
105+
func (a *Assertion) MapHasValue(m, value any, message ...any) error {
106+
a.Helper()
107+
108+
return tryMapHasValue(a.T, false, m, value, message...)
109+
}
110+
111+
// MapHasValueNow tests whether the map contains the specified value or not, and it will terminate
112+
// the execution if the test fails. It will fail if the map does not contain the value, or the type
113+
// of the value cannot assign to the type of the value of the map.
114+
//
115+
// assertion.MapHasValueNow(map[string]int{"a":1}, 1) // success
116+
// assertion.MapHasValueNow(map[string]int{"a":1}, 2) // fail and terminate
117+
// // never run
118+
func (a *Assertion) MapHasValueNow(m, value any, message ...any) error {
119+
a.Helper()
120+
121+
return tryMapHasValue(a.T, true, m, value, message...)
122+
}
123+
124+
// NotMapHasValue tests whether the map contains the specified value or not, it will fail if the
125+
// map contain the value. It will also set the test result to success if the type of the value
126+
// cannot assign to the type of the value of the map.
127+
//
128+
// assertion.NotMapHasValue(map[string]int{"a":1}, 2) // success
129+
// assertion.NotMapHasValue(map[string]int{"a":1}, "a") // success
130+
// assertion.NotMapHasValue(map[string]int{"a":1}, 1) // fail
131+
func (a *Assertion) NotMapHasValue(m, value any, message ...any) error {
132+
a.Helper()
133+
134+
return tryNotMapHasValue(a.T, false, m, value, message...)
135+
}
136+
137+
// NotMapHasValueNow tests whether the map contains the specified value or not, it will fail if the
138+
// map contain the value, and it will terminate the execution if the test fails. It will also set
139+
// the test result to success if the type of the value cannot assign to the type of the value of
140+
// the map.
141+
//
142+
// assertion.NotMapHasValueNow(map[string]int{"a":1}, 2) // success
143+
// assertion.NotMapHasValueNow(map[string]int{"a":1}, "a") // success
144+
// assertion.NotMapHasValueNow(map[string]int{"a":1}, 1) // fail and terminate
145+
// // never run
146+
func (a *Assertion) NotMapHasValueNow(m, value any, message ...any) error {
147+
a.Helper()
148+
149+
return tryNotMapHasValue(a.T, true, m, value, message...)
150+
}
151+
152+
// tryMapHasValue tries to test whether the map contains the specified value or not, and it'll fail
153+
// if the map does not contains the specified value.
154+
func tryMapHasValue(
155+
t *testing.T,
156+
failedNow bool,
157+
m, value any,
158+
message ...any,
159+
) error {
160+
t.Helper()
161+
162+
return test(
163+
t,
164+
func() bool { return isMapHasValue(m, value) },
165+
failedNow,
166+
fmt.Sprintf(defaultErrMessageMapHasValue, value),
167+
message...,
168+
)
169+
}
170+
171+
// tryNotMapHasValue tries to test whether the map contains the specified value or not, and it'll
172+
// fail if the map contains the specified value.
173+
func tryNotMapHasValue(
174+
t *testing.T,
175+
failedNow bool,
176+
m, value any,
177+
message ...any,
178+
) error {
179+
t.Helper()
180+
181+
return test(
182+
t,
183+
func() bool { return !isMapHasValue(m, value) },
184+
failedNow,
185+
fmt.Sprintf(defaultErrMessageNotMapHasValue, value),
186+
message...,
187+
)
188+
}

0 commit comments

Comments
 (0)