-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_map.go
More file actions
103 lines (83 loc) · 2.24 KB
/
Copy pathassert_map.go
File metadata and controls
103 lines (83 loc) · 2.24 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
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
package goassert
import "testing"
/*
Asserts that the given map is empty. The assertion will fail if the given map is nil
*/
func EmptyMap[K comparable, V any](t testing.TB, m map[K]V) {
t.Helper()
if m == nil {
t.Error("Expected empty map but got nil")
return
}
length := len(m)
if length != 0 {
t.Errorf("Expected empty map but got map with length of %d", length)
}
}
/*
Asserts that the given map is not nil or empty
*/
func NotEmptyMap[K comparable, V any](t testing.TB, m map[K]V) {
t.Helper()
if m == nil {
t.Error("Expected non-empty map but got nil")
return
}
if len(m) == 0 {
t.Error("Expected non-empty map but got empty map")
}
}
/*
Asserts that the given map has length equal to the specified length
*/
func MapLength[K comparable, V any](t testing.TB, m map[K]V, expectedLength int) {
t.Helper()
length := len(m)
if length != expectedLength {
t.Errorf("Expected map to have length of %d but got %d", expectedLength, length)
}
}
/*
Asserts that the given map contains the given key. The key must be [comparable]
*/
func MapContainsKey[K comparable, V any](t testing.TB, m map[K]V, k K) {
t.Helper()
_, found := m[k]
if !found {
t.Errorf("The given map was expected to contain key %v but did not", k)
}
}
/*
Asserts that the given map does not contain the given key. The key must be [comparable]
*/
func MapNotContainsKey[K comparable, V any](t testing.TB, m map[K]V, k K) {
t.Helper()
_, found := m[k]
if found {
t.Errorf("The given map was expected to not contain key %v but did", k)
}
}
/*
Asserts that the given map contains the given key-value pair. The key and value must be [comparable]
*/
func MapContains[K, V comparable](t testing.TB, m map[K]V, k K, v V) {
t.Helper()
actualValue, found := m[k]
if !found {
t.Errorf("Key %v was not found in the map", k)
return
}
if v != actualValue {
t.Errorf("Expected %v for key %v in the map but got %v", v, k, actualValue)
}
}
/*
Asserts that the given map does not contain the given key-value pair. The key and value must be [comparable]
*/
func MapNotContains[K, V comparable](t testing.TB, m map[K]V, k K, v V) {
t.Helper()
value, found := m[k]
if found && v == value {
t.Errorf("Key %v and value %v was not expected to be found in the map", k, v)
}
}