-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
47 lines (42 loc) · 1.34 KB
/
map.go
File metadata and controls
47 lines (42 loc) · 1.34 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
package assert
import (
"testing"
)
// MapContainsKeyAny asserts that the key is in the map, and returns the value if it passes.
// For use with maps with any as the key type.
func MapContainsKeyAny[V any](t *testing.T, key any, testedMap map[any]V) V {
t.Helper()
value, ok := testedMap[key]
if !ok {
t.Fatalf("Map does not contain key '%v'", key)
}
return value
}
// MapNotContainsKeyAny asserts that the key is not in the map.
// For use with maps with any as the key type.
func MapNotContainsKeyAny[V any](t *testing.T, key any, testedMap map[any]V) {
t.Helper()
_, ok := testedMap[key]
if ok {
t.Fatalf("Map contains key '%v'", key)
}
}
// MapContainsKey asserts that the key is in the map, and returns the value if it passes.
// For maps that do not have any as their key type, and instead have a comparable type.
func MapContainsKey[K comparable, V any](t *testing.T, key K, testedMap map[K]V) V {
t.Helper()
value, ok := testedMap[key]
if !ok {
t.Fatalf("Map does not contain key '%v'", key)
}
return value
}
// MapNotContainsKey asserts that the key is not in the map.
// For maps that do not have any as their key type, and instead have a comparable type.
func MapNotContainsKey[K comparable, V any](t *testing.T, key K, testedMap map[K]V) {
t.Helper()
_, ok := testedMap[key]
if ok {
t.Fatalf("Map contains key '%v'", key)
}
}