From 19cedd04b691eac491e5166f5dcb745ee2452cf1 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 5 Mar 2024 23:47:56 +0100 Subject: [PATCH] generate must variant --- must/must.go | 36 ++++++++++++++++++++++++++++ must/must_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/must/must.go b/must/must.go index 4488e96..7fbcbfc 100644 --- a/must/must.go +++ b/must/must.go @@ -498,6 +498,42 @@ func MapNotContainsValuesEqual[M ~map[K]V, K comparable, V interfaces.EqualFunc[ invoke(t, assertions.MapNotContainsValuesEqual(m, vals), settings...) } +// MapContainsValue asserts m contains val. +func MapContainsValue[M ~map[K]V, K comparable, V any](t T, m M, val V, settings ...Setting) { + t.Helper() + invoke(t, assertions.MapContainsValue(m, val, options(settings...)), settings...) +} + +// MapNotContainsValue asserts m does not contain val. +func MapNotContainsValue[M ~map[K]V, K comparable, V any](t T, m M, val V, settings ...Setting) { + t.Helper() + invoke(t, assertions.MapNotContainsValue(m, val, options(settings...)), settings...) +} + +// MapContainsValueFunc asserts m contains val using the eq function. +func MapContainsValueFunc[M ~map[K]V, K comparable, V any](t T, m M, val V, eq func(V, V) bool, settings ...Setting) { + t.Helper() + invoke(t, assertions.MapContainsValueFunc(m, val, eq), settings...) +} + +// MapNotContainsValueFunc asserts m does not contain val using the eq function. +func MapNotContainsValueFunc[M ~map[K]V, K comparable, V any](t T, m M, val V, eq func(V, V) bool, settings ...Setting) { + t.Helper() + invoke(t, assertions.MapNotContainsValueFunc(m, val, eq), settings...) +} + +// MapContainsValueEqual asserts m contains val using the V.Equal method. +func MapContainsValueEqual[M ~map[K]V, K comparable, V interfaces.EqualFunc[V]](t T, m M, val V, settings ...Setting) { + t.Helper() + invoke(t, assertions.MapContainsValueEqual(m, val), settings...) +} + +// MapNotContainsValueEqual asserts m does not contain val using the V.Equal method. +func MapNotContainsValueEqual[M ~map[K]V, K comparable, V interfaces.EqualFunc[V]](t T, m M, val V, settings ...Setting) { + t.Helper() + invoke(t, assertions.MapNotContainsValueEqual(m, val), settings...) +} + // FileExistsFS asserts file exists on the fs.FS filesystem. // // Example, diff --git a/must/must_test.go b/must/must_test.go index 9d940bb..dbabe38 100644 --- a/must/must_test.go +++ b/must/must_test.go @@ -1295,6 +1295,66 @@ func TestMapNotContainsValuesEqual(t *testing.T) { }) } +func TestMapContainsValue(t *testing.T) { + tc := newCase(t, `expected map to contain value`) + t.Cleanup(tc.assert) + + m := map[string]int{"a": 1, "b": 2, "c": 3, "d": 4} + MapContainsValue(tc, m, 5) +} + +func TestMapNotContainsValue(t *testing.T) { + tc := newCase(t, `expected map to not contain value`) + t.Cleanup(tc.assert) + + m := map[string]int{"a": 1, "b": 2, "c": 3, "d": 4} + MapNotContainsValue(tc, m, 1) +} + +func TestMapContainsValueFunc(t *testing.T) { + tc := newCase(t, `expected map to contain value`) + t.Cleanup(tc.assert) + + m := map[string]int{"a": 1, "b": 2, "c": 3, "d": 4} + MapContainsValueFunc(tc, m, 6, func(a, b int) bool { + return a == b + }) +} + +func TestMapNotContainsValueFunc(t *testing.T) { + tc := newCase(t, `expected map to not contain value`) + t.Cleanup(tc.assert) + + m := map[string]int{"a": 1, "b": 2, "c": 3, "d": 4} + MapNotContainsValueFunc(tc, m, 1, func(a, b int) bool { + return a == b + }) +} + +func TestMapContainsValueEqual(t *testing.T) { + tc := newCase(t, `expected map to contain value`) + t.Cleanup(tc.assert) + + m := map[int]*Person{ + 1: {ID: 100, Name: "Alice"}, + 2: {ID: 200, Name: "Bob"}, + 3: {ID: 300, Name: "Carl"}, + } + MapContainsValueEqual(tc, m, &Person{ID: 201, Name: "Bob"}) +} + +func TestMapNotContainsValueEqual(t *testing.T) { + tc := newCase(t, `expected map to not contain value`) + t.Cleanup(tc.assert) + + m := map[int]*Person{ + 1: {ID: 100, Name: "Alice"}, + 2: {ID: 200, Name: "Bob"}, + 3: {ID: 300, Name: "Carl"}, + } + MapNotContainsValueEqual(tc, m, &Person{ID: 200, Name: "Daisy"}) +} + func TestFileExistsFS(t *testing.T) { tc := newCase(t, `expected file to exist`) t.Cleanup(tc.assert)