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

Commit 1198f4d

Browse files
committed
Begin map assertions
1 parent 1727b3a commit 1198f4d

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

CHANGELOG.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The main additions are the new assertions for
6161
- `Panics`
6262
- `NotPanic`
6363
- Add `Slice` function which provides following assertions,
64-
in addition to `Any`, via `FluentSlice` types:
64+
in addition to `Any`, via `FluentSlice` type:
6565
- `Empty`
6666
- `NotEmpty`
6767
- `Equivalent`
@@ -71,6 +71,19 @@ The main additions are the new assertions for
7171
- `Any`
7272
- `All`
7373
- `None`
74+
- Add `Map` function which provides following assertions,
75+
in addition to `Any`, via `FlientMap` type:
76+
- `Empty`
77+
- `NotEmpty`
78+
- `Contain`
79+
- `NotContain`
80+
- `ContainKey`
81+
- `NotContainKey`
82+
- `ContainPair`
83+
- `NotContainPair`
84+
- `Any`
85+
- `All`
86+
- `None`
7487
- Add `FailureMessage.Prefix` method together with `And` and `Or` functions
7588
to facilitate creating complex assertions.
7689

map.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package verify
22

3+
import "fmt"
4+
35
// FluentMap encapsulates assertions for a map.
46
type FluentMap[K comparable, V any] struct {
57
FluentAny[map[K]V]
@@ -10,15 +12,21 @@ func Map[K comparable, V any](got map[K]V) FluentMap[K, V] {
1012
return FluentMap[K, V]{FluentAny[map[K]V]{got}}
1113
}
1214

13-
// TODO: Empty() FailureMessage
14-
15-
// TODO: NotEmpty() FailureMessage
16-
17-
// TODO: Len(n int) FailureMessage
18-
19-
// TODO: Equal(elements map[K]V) FailureMessage
15+
// Empty tests if the slice is empty.
16+
func (x FluentMap[K, V]) Empty() FailureMessage {
17+
if len(x.Got) == 0 {
18+
return ""
19+
}
20+
return FailureMessage(fmt.Sprintf("not an empty map\ngot: %+v", x.Got))
21+
}
2022

21-
// TODO: NotEqual(elements map[K]V) FailureMessage
23+
// NotEmpty tests if the slice is not empty.
24+
func (x FluentMap[K, V]) NotEmpty() FailureMessage {
25+
if len(x.Got) > 0 {
26+
return ""
27+
}
28+
return FailureMessage(fmt.Sprintf("an empty map\ngot: %+v", x.Got))
29+
}
2230

2331
// TODO: Contain(elements map[K]V) FailureMessage
2432

@@ -35,3 +43,5 @@ func Map[K comparable, V any](got map[K]V) FluentMap[K, V] {
3543
// TODO: Any(func(K,V) bool) FailureMessage
3644

3745
// TODO: All(func(K,V) bool) FailureMessage
46+
47+
// TODO: None(func(K,V) bool) FailureMessage

map_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,29 @@ func TestMap(t *testing.T) {
2020
got := verify.Map(want).FluentAny.Got // type embedding done properly
2121
assertEqual(t, got, want)
2222
})
23+
24+
dict := map[string]A{
25+
"a": {Str: "text", Bool: true, Slice: []int{1, 2, 3}},
26+
"b": {Slice: []int{9, 8, 7}},
27+
}
28+
t.Run("Empty", func(t *testing.T) {
29+
t.Run("Passed", func(t *testing.T) {
30+
got := verify.Map(map[string]A{}).Empty()
31+
assertPassed(t, got)
32+
})
33+
t.Run("Failed", func(t *testing.T) {
34+
got := verify.Map(dict).Empty()
35+
assertFailed(t, got, "not an empty map")
36+
})
37+
})
38+
t.Run("NotEmpty", func(t *testing.T) {
39+
t.Run("Passed", func(t *testing.T) {
40+
got := verify.Map(dict).NotEmpty()
41+
assertPassed(t, got)
42+
})
43+
t.Run("Failed", func(t *testing.T) {
44+
got := verify.Map(map[string]A{}).NotEmpty()
45+
assertFailed(t, got, "an empty map")
46+
})
47+
})
2348
}

0 commit comments

Comments
 (0)