Skip to content

Commit

Permalink
test: added MapEqOp
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck authored and shoenig committed Sep 4, 2024
1 parent 18682f8 commit fe99a71
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,19 @@ func ExampleMapEqual() {
// Output:
}

func ExampleMapEqOp() {
m1 := map[int]string{
1: "one",
2: "two",
}
m2 := map[int]string{
1: "one",
2: "two",
}
MapEqOp(t, m1, m2)
// Output:
}

func ExampleMapLen() {
m := map[int]string{
1: "one",
Expand Down
28 changes: 28 additions & 0 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,34 @@ func MapEqual[M interfaces.MapEqualFunc[K, V], K comparable, V interfaces.EqualF
return
}

func MapEqOp[M interfaces.Map[K, V], K, V comparable](exp, val M) (s string) {
lenA, lenB := len(exp), len(val)

if lenA != lenB {
s = "expected maps of same length\n"
s += bullet("len(exp): %d\n", lenA)
s += bullet("len(val): %d\n", lenB)
return
}

for key, valA := range exp {
valB, exists := val[key]
if !exists {
s = "expected maps of same keys\n"
s += diff(exp, val, nil)
return
}

if valA != valB {
s = "expected maps of same values via ==\n"
s += diff(exp, val, nil)
return
}
}

return
}

func MapLen[M ~map[K]V, K comparable, V any](n int, m M) (s string) {
if l := len(m); l != n {
s = "expected map to be different length\n"
Expand Down
13 changes: 13 additions & 0 deletions must/examples_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions must/must.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ func MapEqual[M interfaces.MapEqualFunc[K, V], K comparable, V interfaces.EqualF
invoke(t, assertions.MapEqual(exp, val), settings...)
}

// MapEqOp asserts maps exp and val contain the same key/val pairs, using == to
// compare vals.
func MapEqOp[M interfaces.Map[K, V], K, V comparable](t T, exp M, val M, settings ...Setting) {
t.Helper()
invoke(t, assertions.MapEqOp(exp, val), settings...)
}

// MapLen asserts map is of size n.
func MapLen[M ~map[K]V, K comparable, V any](t T, n int, m M, settings ...Setting) {
t.Helper()
Expand Down
52 changes: 52 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,58 @@ func TestMapEqual(t *testing.T) {
})
}

func TestMapEqOp(t *testing.T) {
t.Run("different values", func(t *testing.T) {
tc := newCase(t, `expected maps of same values via ==`)
t.Cleanup(tc.assert)

a := map[int]string{
0: "zero",
1: "one",
}

b := map[int]string{
0: "zero",
1: "eins",
}

MapEqOp(tc, a, b)
})
t.Run("different lengths", func(t *testing.T) {
tc := newCase(t, `expected maps of same length`)
t.Cleanup(tc.assert)

a := map[int]string{
0: "zero",
1: "one",
}

b := map[int]string{
0: "zero",
1: "one",
2: "two",
}

MapEqOp(tc, a, b)
})
t.Run("different keys", func(t *testing.T) {
tc := newCase(t, `expected maps of same keys`)
t.Cleanup(tc.assert)

a := map[int]string{
0: "zero",
1: "one",
}

b := map[int]string{
0: "zero",
2: "one",
}

MapEqOp(tc, a, b)
})
}

func TestMapLen(t *testing.T) {
tc := newCase(t, `expected map to be different length`)
t.Cleanup(tc.assert)
Expand Down

0 comments on commit fe99a71

Please sign in to comment.