Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 8b2f003

Browse files
committed
Map assertions
1 parent dbf04ff commit 8b2f003

File tree

5 files changed

+118
-20
lines changed

5 files changed

+118
-20
lines changed

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ The main additions are the new assertions for
7777
- `NotEmpty`
7878
- `Contain`
7979
- `NotContain`
80-
- `ContainKey`
81-
- `NotContainKey`
8280
- `ContainPair`
8381
- `NotContainPair`
8482
- `Any`

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ func TestSlice(t *testing.T) {
124124

125125
```sh
126126
$ go test
127-
TODO
127+
--- FAIL: TestSlice (0.00s)
128+
slice_test.go:12:
129+
not equivalent
130+
got: [3 1 2]
131+
want: [2 3 4]
132+
extra got: [1]
133+
extra want: [4]
128134
```
129135

130136
### Periodic polling

map.go

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (x FluentMap[K, V]) Contain(want map[K]V, opts ...cmp.Option) FailureMessag
4141
return FailureMessage(fmt.Sprintf("not contains all pairs\ngot: %+v\nwant: %+v\nmissing: %+v", x.Got, want, missing))
4242
}
4343

44-
// NotContain tests if the slice does not have the same element as want in any order.
44+
// NotContain tests if the map does not contains all pairs from want.
4545
func (x FluentMap[K, V]) NotContain(want map[K]V, opts ...cmp.Option) FailureMessage {
4646
missing := x.miss(want, opts)
4747
if len(missing) > 0 {
@@ -66,20 +66,56 @@ func (x FluentMap[K, V]) miss(want map[K]V, opts []cmp.Option) map[K]V {
6666
return missing
6767
}
6868

69-
// TODO: Contain(elements map[K]V) FailureMessage
70-
71-
// TODO: NotContain(elements map[K]V) FailureMessage
72-
73-
// TODO: ContainKey(keys ...K) FailureMessage
74-
75-
// TODO: NotContainKey(keys ...K) FailureMessage
76-
77-
// TODO: ContainPair(K,V) FailureMessage
69+
// ContainPair tests if the map contains the pair.
70+
func (x FluentMap[K, V]) ContainPair(k K, v V, opts ...cmp.Option) FailureMessage {
71+
got, ok := x.Got[k]
72+
if !ok {
73+
return FailureMessage(fmt.Sprintf("has no value under key\ngot: %+v\nkey: %+v\nvalue: %+v", x.Got, k, v))
74+
}
75+
if !cmp.Equal(v, got, opts...) {
76+
return FailureMessage(fmt.Sprintf("has different value under key\nkey: %+v\ngot: %+v\nwant: %+v", k, got, v))
77+
}
78+
return ""
79+
}
7880

79-
// TODO: NotContainPair(K,V) FailureMessage
81+
// NotContainPair tests if the map does not contain the pair.
82+
func (x FluentMap[K, V]) NotContainPair(k K, v V, opts ...cmp.Option) FailureMessage {
83+
got, ok := x.Got[k]
84+
if !ok {
85+
return ""
86+
}
87+
if !cmp.Equal(v, got, opts...) {
88+
return ""
89+
}
90+
return FailureMessage(fmt.Sprintf("contains the pair\ngot: %+v\nkey: %+v\nvalue: %+v", x.Got, k, v))
91+
}
8092

81-
// TODO: Any(func(K,V) bool) FailureMessage
93+
// Any tests if any of the slice's item meets the predicate criteria.
94+
func (x FluentMap[K, V]) Any(predicate func(K, V) bool) FailureMessage {
95+
for k, v := range x.Got {
96+
if predicate(k, v) {
97+
return ""
98+
}
99+
}
100+
return FailureMessage(fmt.Sprintf("none pair does meet the predicate criteria\ngot: %+v", x.Got))
101+
}
82102

83-
// TODO: All(func(K,V) bool) FailureMessage
103+
// All tests if all of the slice's items meets the predicate criteria.
104+
func (x FluentMap[K, V]) All(predicate func(K, V) bool) FailureMessage {
105+
for k, v := range x.Got {
106+
if !predicate(k, v) {
107+
return FailureMessage(fmt.Sprintf("a pair does not meet the predicate criteria\ngot: %+v\npair: %+v", x.Got, v))
108+
}
109+
}
110+
return ""
111+
}
84112

85-
// TODO: None(func(K,V) bool) FailureMessage
113+
// None tests if all of the slice's item does not meet the predicate criteria.
114+
func (x FluentMap[K, V]) None(predicate func(K, V) bool) FailureMessage {
115+
for k, v := range x.Got {
116+
if predicate(k, v) {
117+
return FailureMessage(fmt.Sprintf("a pair meets the predicate criteria\ngot: %+v\npair: %+v", x.Got, v))
118+
}
119+
}
120+
return ""
121+
}

map_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,64 @@ func TestMap(t *testing.T) {
7474
assertFailed(t, got, "contains all pairs")
7575
})
7676
})
77+
78+
t.Run("ContainPair", func(t *testing.T) {
79+
t.Run("Has", func(t *testing.T) {
80+
got := verify.Map(dict).ContainPair("b", A{Slice: []int{9, 8, 7}})
81+
assertPassed(t, got)
82+
})
83+
t.Run("DiffKey", func(t *testing.T) {
84+
got := verify.Map(dict).ContainPair("z", A{Slice: []int{9, 8, 7}})
85+
assertFailed(t, got, "has no value under key")
86+
})
87+
t.Run("DiffValue", func(t *testing.T) {
88+
got := verify.Map(dict).ContainPair("b", A{Slice: []int{1, 1, 1}})
89+
assertFailed(t, got, "has different value under key")
90+
})
91+
})
92+
t.Run("NotContainPair", func(t *testing.T) {
93+
t.Run("Has", func(t *testing.T) {
94+
got := verify.Map(dict).NotContainPair("b", A{Slice: []int{9, 8, 7}})
95+
assertFailed(t, got, "contains the pair")
96+
})
97+
t.Run("DiffKey", func(t *testing.T) {
98+
got := verify.Map(dict).NotContainPair("z", A{Slice: []int{9, 8, 7}})
99+
assertPassed(t, got)
100+
})
101+
t.Run("DiffValue", func(t *testing.T) {
102+
got := verify.Map(dict).NotContainPair("b", A{Slice: []int{1, 1, 1}})
103+
assertPassed(t, got)
104+
})
105+
})
106+
107+
t.Run("Any", func(t *testing.T) {
108+
t.Run("Passed", func(t *testing.T) {
109+
got := verify.Map(dict).Any(func(string, A) bool { return true })
110+
assertPassed(t, got)
111+
})
112+
t.Run("Failed", func(t *testing.T) {
113+
got := verify.Map(dict).Any(func(string, A) bool { return false })
114+
assertFailed(t, got, "none pair does meet the predicate criteria")
115+
})
116+
})
117+
t.Run("All", func(t *testing.T) {
118+
t.Run("Passed", func(t *testing.T) {
119+
got := verify.Map(dict).All(func(string, A) bool { return true })
120+
assertPassed(t, got)
121+
})
122+
t.Run("Failed", func(t *testing.T) {
123+
got := verify.Map(dict).All(func(string, A) bool { return false })
124+
assertFailed(t, got, "a pair does not meet the predicate criteria")
125+
})
126+
})
127+
t.Run("None", func(t *testing.T) {
128+
t.Run("Passed", func(t *testing.T) {
129+
got := verify.Map(dict).None(func(string, A) bool { return false })
130+
assertPassed(t, got)
131+
})
132+
t.Run("Failed", func(t *testing.T) {
133+
got := verify.Map(dict).None(func(string, A) bool { return true })
134+
assertFailed(t, got, "a pair meets the predicate criteria")
135+
})
136+
})
77137
}

slice.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (x FluentSlice[T]) Equivalent(want []T, opts ...cmp.Option) FailureMessage
3838
if len(extraGot) == 0 && len(extraWant) == 0 {
3939
return ""
4040
}
41-
return FailureMessage(fmt.Sprintf("not equivalent\nextra got: %+v\nextra want: %+v", extraGot, extraWant))
41+
return FailureMessage(fmt.Sprintf("not equivalent\ngot: %+v\nwant: %+v\nextra got: %+v\nextra want: %+v", x.Got, want, extraGot, extraWant))
4242
}
4343

4444
// NotEquivalent tests if the slice does not have the same items as want in any order.
@@ -83,7 +83,6 @@ func (x FluentSlice[T]) diff(want []T, opts []cmp.Option) (extraGot, extraWant [
8383
return
8484
}
8585

86-
// TODO: Rename to ContainItem and add Contain
8786
// Contain tests if the slice contains the item.
8887
func (x FluentSlice[T]) Contain(item T, opts ...cmp.Option) FailureMessage {
8988
for _, v := range x.Got {
@@ -94,7 +93,6 @@ func (x FluentSlice[T]) Contain(item T, opts ...cmp.Option) FailureMessage {
9493
return FailureMessage(fmt.Sprintf("slice does not contain the item\ngot: %+v\nitem: %+v", x.Got, item))
9594
}
9695

97-
// TODO: Rename to NotContainItem and add NotContain
9896
// NotContain tests if the slice does not contain the item.
9997
func (x FluentSlice[T]) NotContain(item T, opts ...cmp.Option) FailureMessage {
10098
for _, v := range x.Got {

0 commit comments

Comments
 (0)