-
Notifications
You must be signed in to change notification settings - Fork 2
/
map.go
130 lines (115 loc) · 3.6 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
127
128
129
130
package verify
import (
"fmt"
"github.com/google/go-cmp/cmp"
)
// FluentMap encapsulates assertions for a map.
type FluentMap[K comparable, V any] struct {
FluentAny[map[K]V]
}
// Map is used for testing a map.
func Map[K comparable, V any](got map[K]V) FluentMap[K, V] {
return FluentMap[K, V]{FluentAny[map[K]V]{got}}
}
// Empty tests if the slice is empty.
func (x FluentMap[K, V]) Empty() FailureMessage {
if len(x.Got) == 0 {
return ""
}
return FailureMessage(fmt.Sprintf("not an empty map\ngot: %+v", x.Got))
}
// NotEmpty tests if the slice is not empty.
func (x FluentMap[K, V]) NotEmpty() FailureMessage {
if len(x.Got) > 0 {
return ""
}
return FailureMessage(fmt.Sprintf("an empty map\ngot: %+v", x.Got))
}
// Contain tests if the map contains all pairs from want.
func (x FluentMap[K, V]) Contain(want map[K]V, opts ...cmp.Option) FailureMessage {
missing := x.miss(want, opts)
if len(missing) == 0 {
return ""
}
return FailureMessage(fmt.Sprintf("not contains all pairs\ngot: %+v\nwant: %+v\nmissing: %+v", x.Got, want, missing))
}
// NotContain tests if the map does not contains all pairs from want.
func (x FluentMap[K, V]) NotContain(want map[K]V, opts ...cmp.Option) FailureMessage {
missing := x.miss(want, opts)
if len(missing) > 0 {
return ""
}
return FailureMessage(fmt.Sprintf("contains all pairs\ngot: %+v\nwant: %+v", x.Got, want))
}
func (x FluentMap[K, V]) miss(want map[K]V, opts []cmp.Option) map[K]V {
missing := map[K]V{}
for k, v := range want {
got, ok := x.Got[k]
if !ok {
missing[k] = v
continue
}
if !cmp.Equal(v, got, opts...) {
missing[k] = v
continue
}
}
return missing
}
// ContainPair tests if the map contains the pair.
func (x FluentMap[K, V]) ContainPair(k K, v V, opts ...cmp.Option) FailureMessage {
got, ok := x.Got[k]
if !ok {
return FailureMessage(fmt.Sprintf("has no value under key\ngot: %+v\nkey: %+v\nvalue: %+v", x.Got, k, v))
}
if !cmp.Equal(v, got, opts...) {
return FailureMessage(fmt.Sprintf("has different value under key\nkey: %+v\ngot: %+v\nwant: %+v", k, got, v))
}
return ""
}
// NotContainPair tests if the map does not contain the pair.
func (x FluentMap[K, V]) NotContainPair(k K, v V, opts ...cmp.Option) FailureMessage {
got, ok := x.Got[k]
if !ok {
return ""
}
if !cmp.Equal(v, got, opts...) {
return ""
}
return FailureMessage(fmt.Sprintf("contains the pair\ngot: %+v\nkey: %+v\nvalue: %+v", x.Got, k, v))
}
// Any tests if any of the map's pairs meets the predicate criteria.
func (x FluentMap[K, V]) Any(predicate func(K, V) bool) FailureMessage {
for k, v := range x.Got {
if predicate(k, v) {
return ""
}
}
return FailureMessage(fmt.Sprintf("none pair does meet the predicate criteria\ngot: %+v", x.Got))
}
// All tests if all of the map's pairs meet the predicate criteria.
func (x FluentMap[K, V]) All(predicate func(K, V) bool) FailureMessage {
for k, v := range x.Got {
if !predicate(k, v) {
return FailureMessage(fmt.Sprintf("a pair does not meet the predicate criteria\ngot: %+v\npair: %+v", x.Got, v))
}
}
return ""
}
// None tests if none of the map's pairs meets the predicate criteria.
func (x FluentMap[K, V]) None(predicate func(K, V) bool) FailureMessage {
for k, v := range x.Got {
if predicate(k, v) {
return FailureMessage(fmt.Sprintf("a pair meets the predicate criteria\ngot: %+v\npair: %+v", x.Got, v))
}
}
return ""
}
// Len tests the length of the map.
func (x FluentMap[K, V]) Len(want int) FailureMessage {
gotLen := len(x.Got)
if gotLen != want {
return FailureMessage(fmt.Sprintf("has different length\ngot: %+v\nlen: %v\nwant: %v", x.Got, gotLen, want))
}
return ""
}