-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
126 lines (118 loc) · 2.39 KB
/
map.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package assert
import (
"fmt"
"maps"
"testing"
)
// MapNil asserts that m is nil.
//
//nolint:thelper // It's called below.
func MapNil[M ~map[K]V, K comparable, V any](tb testing.TB, m M, opts ...Option) bool {
ok := m == nil
if !ok {
tb.Helper()
Fail(
tb,
"map_nil",
"not nil:\nm = "+ValueStringer(m),
opts...,
)
}
return ok
}
// MapNotNil asserts that m is not nil.
//
//nolint:thelper // It's called below.
func MapNotNil[M ~map[K]V, K comparable, V any](tb testing.TB, m M, opts ...Option) bool {
ok := m != nil
if !ok {
tb.Helper()
Fail(
tb,
"map_not_nil",
"nil",
opts...,
)
}
return ok
}
// MapEmpty asserts that m is empty.
//
//nolint:thelper // It's called below.
func MapEmpty[M ~map[K]V, K comparable, V any](tb testing.TB, m M, opts ...Option) bool {
ok := len(m) == 0
if !ok {
tb.Helper()
Fail(
tb,
"map_empty",
fmt.Sprintf("not empty:\nlength = %d\nm = %s", len(m), ValueStringer(m)),
opts...,
)
}
return ok
}
// MapNotEmpty asserts that m is not empty.
//
//nolint:thelper // It's called below.
func MapNotEmpty[M ~map[K]V, K comparable, V any](tb testing.TB, m M, opts ...Option) bool {
ok := len(m) != 0
if !ok {
tb.Helper()
Fail(
tb,
"map_not_empty",
"empty",
opts...,
)
}
return ok
}
// MapLen asserts that m has length l.
//
//nolint:thelper // It's called below.
func MapLen[M ~map[K]V, K comparable, V any](tb testing.TB, m M, l int, opts ...Option) bool {
ok := len(m) == l
if !ok {
tb.Helper()
Fail(
tb,
"map_len",
fmt.Sprintf("unexpected length:\nexpected = %d\nactual = %d", l, len(m)),
opts...,
)
}
return ok
}
// MapEqual asserts that m1 and m2 are equal.
//
//nolint:thelper // It's called below.
func MapEqual[M1, M2 ~map[K]V, K, V comparable](tb testing.TB, m1 M1, m2 M2, opts ...Option) bool {
ok := maps.Equal(m1, m2)
if !ok {
tb.Helper()
Fail(
tb,
"map_equal",
fmt.Sprintf("not equal:\nm1 = %s\nm2 = %s", ValueStringer(m1), ValueStringer(m2)),
opts...,
)
}
return ok
}
// MapNotEqual asserts that m1 and m2 are not equal.
//
//nolint:thelper // It's called below.
func MapNotEqual[M1, M2 ~map[K]V, K, V comparable](tb testing.TB, m1 M1, m2 M2, opts ...Option) bool {
ok := !maps.Equal(m1, m2)
if !ok {
tb.Helper()
Fail(
tb,
"map_not_equal",
fmt.Sprintf("equal:\nm1 = %s\nm2 = %s", ValueStringer(m1), ValueStringer(m2)),
opts...,
)
}
return ok
}