114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
package errs
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestContainsSubstring(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
s string
|
|
substr string
|
|
want bool
|
|
}{
|
|
{"exact match", "hello", "hello", true},
|
|
{"substring at start", "hello world", "hello", true},
|
|
{"substring at end", "hello world", "world", true},
|
|
{"substring in middle", "hello world foo", "world", true},
|
|
{"no match", "hello", "world", false},
|
|
{"empty string", "", "", true},
|
|
{"empty substring", "hello", "", true},
|
|
{"substr longer than s", "hi", "hello world", false},
|
|
{"partial match only", "hello", "hello world", false},
|
|
{"case sensitive", "Hello", "hello", false},
|
|
{"unicode substring", "你好世界", "世界", true},
|
|
{"unicode no match", "你好世界", "hello", false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := containsSubstring(tt.s, tt.substr)
|
|
if got != tt.want {
|
|
t.Errorf("containsSubstring(%q, %q) = %v, want %v", tt.s, tt.substr, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestContainsAt(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
s string
|
|
substr string
|
|
start int
|
|
want bool
|
|
}{
|
|
{"find at start", "hello world", "hello", 0, true},
|
|
{"find at offset", "hello world", "world", 6, true},
|
|
{"find with start inside", "hello world hello", "hello", 6, true},
|
|
{"not found after offset", "hello world", "hello", 6, false},
|
|
{"start beyond string", "hello", "lo", 10, false},
|
|
{"empty substr at start", "hello", "", 0, true},
|
|
{"empty substr at end", "hello", "", 5, true},
|
|
{"start at exact position", "hello world", "world", 6, true},
|
|
{"start_just_before", "hello world", "world", 5, true}, // "world" starts at index 6, so start=5 is within range
|
|
{"multiple occurrences", "ababab", "ab", 2, true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := containsAt(tt.s, tt.substr, tt.start)
|
|
if got != tt.want {
|
|
t.Errorf("containsAt(%q, %q, %d) = %v, want %v", tt.s, tt.substr, tt.start, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAssertErrorContains_Success(t *testing.T) {
|
|
// This should pass - error contains substring
|
|
// Just verify it doesn't panic/fail
|
|
err := &testError{msg: "connection refused: something went wrong"}
|
|
AssertErrorContains(t, err, "connection refused")
|
|
}
|
|
|
|
func TestAssertErrorContains_EmptySubstring(t *testing.T) {
|
|
// Empty substring should pass with any error
|
|
err := &testError{msg: "any error"}
|
|
AssertErrorContains(t, err, "")
|
|
}
|
|
|
|
// testError is a simple error implementation for testing
|
|
type testError struct {
|
|
msg string
|
|
}
|
|
|
|
func (e *testError) Error() string {
|
|
return e.msg
|
|
}
|
|
|
|
// TestContainsSubstring_StandardLibrary verifies our implementation matches strings.Contains
|
|
func TestContainsSubstring_StandardLibrary(t *testing.T) {
|
|
testCases := []struct {
|
|
s string
|
|
substr string
|
|
}{
|
|
{"hello world", "world"},
|
|
{"", ""},
|
|
{"hello", ""},
|
|
{"", "x"},
|
|
{"hello", "world"},
|
|
{"ababab", "ab"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
ourResult := containsSubstring(tc.s, tc.substr)
|
|
stdResult := strings.Contains(tc.s, tc.substr)
|
|
if ourResult != stdResult {
|
|
t.Errorf("containsSubstring(%q, %q) = %v, strings.Contains = %v",
|
|
tc.s, tc.substr, ourResult, stdResult)
|
|
}
|
|
}
|
|
}
|