diff options
Diffstat (limited to 'check/check_test.go')
| -rw-r--r-- | check/check_test.go | 83 |
1 files changed, 75 insertions, 8 deletions
diff --git a/check/check_test.go b/check/check_test.go index a5ab96c..3293ba4 100644 --- a/check/check_test.go +++ b/check/check_test.go @@ -111,17 +111,17 @@ func wrappedCheckNotEqual(t checkmate.TestingT, args []any) bool { func wrappedCheckContains(t checkmate.TestingT, args []any) bool { if len(args) > 2 { - return Contains(t, args[0].(string), args[1].(string), args[2:]...) + return Contains(t, args[0], args[1], args[2:]...) } else { - return Contains(t, args[0].(string), args[1].(string)) + return Contains(t, args[0], args[1]) } } func wrappedCheckNotContains(t checkmate.TestingT, args []any) bool { if len(args) > 2 { - return NotContains(t, args[0].(string), args[1].(string), args[2:]...) + return NotContains(t, args[0], args[1], args[2:]...) } else { - return NotContains(t, args[0].(string), args[1].(string)) + return NotContains(t, args[0], args[1]) } } @@ -142,8 +142,17 @@ var passingTestFns = []struct { {"CheckNotDeepEqual", wrappedCheckNotDeepEqual, []any{5, 6}}, {"CheckEqual", wrappedCheckEqual, []any{5, 5}}, {"CheckNotEqual", wrappedCheckNotEqual, []any{5, 10}}, - {"CheckContains", wrappedCheckContains, []any{"hello world", "world"}}, - {"CheckNotContains", wrappedCheckNotContains, []any{"hello world", "goodbye"}}, + {"CheckContains - Substr", wrappedCheckContains, []any{"hello world", "world"}}, + {"CheckContains - Slice", wrappedCheckContains, []any{[]string{"hello", "world"}, "world"}}, + {"CheckContains - Array", wrappedCheckContains, []any{[2]string{"hello", "world"}, "world"}}, + {"CheckContains - Map", wrappedCheckContains, []any{map[string]int{"hello": 1, "world": 2}, "world"}}, + {"CheckContains - Pointer to String", wrappedCheckContains, []any{func() *string { s := "hello world"; return &s }(), "world"}}, + {"CheckContains - Pointer to Slice", wrappedCheckContains, []any{func() *[]string { s := []string{"hello", "world"}; return &s }(), "world"}}, + {"CheckNotContains - Substr", wrappedCheckNotContains, []any{"hello world", "goodbye"}}, + {"CheckNotContains - Slice", wrappedCheckNotContains, []any{[]string{"hello", "world"}, "goodbye"}}, + {"CheckNotContains - Array", wrappedCheckNotContains, []any{[2]string{"hello", "world"}, "goodbye"}}, + {"CheckNotContains - Map", wrappedCheckNotContains, []any{map[string]int{"hello": 1, "world": 2}, "goodbye"}}, + {"CheckNotContains - Pointer to String", wrappedCheckNotContains, []any{func() *string { s := "hello world"; return &s }(), "goodbye"}}, } var failingTestFns = []struct { @@ -163,8 +172,14 @@ var failingTestFns = []struct { {"CheckNotDeepEqual", wrappedCheckNotDeepEqual, []any{5, 5}}, {"CheckEqual", wrappedCheckEqual, []any{5, 10}}, {"CheckNotEqual", wrappedCheckNotEqual, []any{5, 5}}, - {"CheckContains", wrappedCheckContains, []any{"hello world", "goodbye"}}, - {"CheckNotContains", wrappedCheckNotContains, []any{"hello world", "world"}}, + {"CheckContains - Substr not found", wrappedCheckContains, []any{"hello world", "goodbye"}}, + {"CheckContains - Slice item not found", wrappedCheckContains, []any{[]string{"hello", "world"}, "goodbye"}}, + {"CheckContains - Array item not found", wrappedCheckContains, []any{[2]string{"hello", "world"}, "goodbye"}}, + {"CheckContains - Map key not found", wrappedCheckContains, []any{map[string]int{"hello": 1}, "goodbye"}}, + {"CheckNotContains - Substr found", wrappedCheckNotContains, []any{"hello world", "world"}}, + {"CheckNotContains - Slice item found", wrappedCheckNotContains, []any{[]string{"hello", "world"}, "world"}}, + {"CheckNotContains - Array item found", wrappedCheckNotContains, []any{[2]string{"hello", "world"}, "world"}}, + {"CheckNotContains - Map key found", wrappedCheckNotContains, []any{map[string]int{"hello": 1, "world": 2}, "world"}}, } func TestOptionalMessageAndArgs(t *testing.T) { @@ -374,3 +389,55 @@ func TestCheckNilForNilPointer(t *testing.T) { t.Fatal("check.Nil should have returned true for nil pointer") } } + +func TestCheckContainsErrorCases(t *testing.T) { + t.Run("String container with non-string item", func(t *testing.T) { + mockT := &cmtest.MockT{} + passed := Contains(mockT, "hello world", 123) + + if passed { + t.Error("Contains should fail when string container has non-string item") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "item must be string") { + t.Errorf("Expected error message about item type, got: %v", mockT.Logs) + } + }) + + t.Run("Unsupported container type", func(t *testing.T) { + mockT := &cmtest.MockT{} + passed := Contains(mockT, 123, 1) + + if passed { + t.Error("Contains should fail for unsupported container type") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "unsupported container type") { + t.Errorf("Expected error message about unsupported type, got: %v", mockT.Logs) + } + }) +} + +func TestCheckNotContainsErrorCases(t *testing.T) { + t.Run("String container with non-string item", func(t *testing.T) { + mockT := &cmtest.MockT{} + passed := NotContains(mockT, "hello world", 123) + + if passed { + t.Error("NotContains should fail when string container has non-string item") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "item must be string") { + t.Errorf("Expected error message about item type, got: %v", mockT.Logs) + } + }) + + t.Run("Unsupported container type", func(t *testing.T) { + mockT := &cmtest.MockT{} + passed := NotContains(mockT, 123, 1) + + if passed { + t.Error("NotContains should fail for unsupported container type") + } + if len(mockT.Logs) == 0 || !strings.Contains(mockT.Logs[0], "unsupported container type") { + t.Errorf("Expected error message about unsupported type, got: %v", mockT.Logs) + } + }) +} |