-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings_test.go
More file actions
159 lines (140 loc) · 3.88 KB
/
strings_test.go
File metadata and controls
159 lines (140 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package attest_test
import (
"strings"
"testing"
"github.com/cboone/attest"
)
func TestContains(t *testing.T) {
t.Run("substring present passes", func(t *testing.T) {
mt := newMockT(t)
attest.Contains(mt, "hello world", "world")
if mt.failed {
t.Error("Contains should pass when substring is present")
}
})
t.Run("substring absent fails", func(t *testing.T) {
mt := newMockT(t)
attest.Contains(mt, "hello world", "xyz")
if !mt.failed {
t.Error("Contains should fail when substring is absent")
}
})
t.Run("empty substring passes", func(t *testing.T) {
mt := newMockT(t)
attest.Contains(mt, "hello", "")
if mt.failed {
t.Error("Contains with empty needle should pass")
}
})
t.Run("empty haystack fails for non-empty needle", func(t *testing.T) {
mt := newMockT(t)
attest.Contains(mt, "", "x")
if !mt.failed {
t.Error("Contains with empty haystack should fail for non-empty needle")
}
})
}
func TestNotContains(t *testing.T) {
t.Run("substring absent passes", func(t *testing.T) {
mt := newMockT(t)
attest.NotContains(mt, "hello world", "xyz")
if mt.failed {
t.Error("NotContains should pass when substring is absent")
}
})
t.Run("substring present fails", func(t *testing.T) {
mt := newMockT(t)
attest.NotContains(mt, "hello world", "world")
if !mt.failed {
t.Error("NotContains should fail when substring is present")
}
})
}
func TestHasPrefix(t *testing.T) {
t.Run("correct prefix passes", func(t *testing.T) {
mt := newMockT(t)
attest.HasPrefix(mt, "hello world", "hello")
if mt.failed {
t.Error("HasPrefix should pass for correct prefix")
}
})
t.Run("wrong prefix fails", func(t *testing.T) {
mt := newMockT(t)
attest.HasPrefix(mt, "hello world", "world")
if !mt.failed {
t.Error("HasPrefix should fail for wrong prefix")
}
})
}
func TestHasSuffix(t *testing.T) {
t.Run("correct suffix passes", func(t *testing.T) {
mt := newMockT(t)
attest.HasSuffix(mt, "hello world", "world")
if mt.failed {
t.Error("HasSuffix should pass for correct suffix")
}
})
t.Run("wrong suffix fails", func(t *testing.T) {
mt := newMockT(t)
attest.HasSuffix(mt, "hello world", "hello")
if !mt.failed {
t.Error("HasSuffix should fail for wrong suffix")
}
})
}
func TestEqualFold(t *testing.T) {
t.Run("case-insensitive match passes", func(t *testing.T) {
mt := newMockT(t)
attest.EqualFold(mt, "Hello", "hello")
if mt.failed {
t.Error("EqualFold should pass for case-insensitive match")
}
})
t.Run("different strings fail", func(t *testing.T) {
mt := newMockT(t)
attest.EqualFold(mt, "hello", "world")
if !mt.failed {
t.Error("EqualFold should fail for different strings")
}
})
}
func TestMatches(t *testing.T) {
t.Run("valid pattern matches passes", func(t *testing.T) {
mt := newMockT(t)
attest.Matches(mt, "hello123", `^hello\d+$`)
if mt.failed {
t.Error("Matches should pass for matching pattern")
}
})
t.Run("valid pattern no match fails", func(t *testing.T) {
mt := newMockT(t)
attest.Matches(mt, "hello", `^\d+$`)
if !mt.failed {
t.Error("Matches should fail for non-matching pattern")
}
})
t.Run("invalid pattern fails with clear message", func(t *testing.T) {
mt := newMockT(t)
attest.Matches(mt, "hello", `[invalid`)
if !mt.failed {
t.Error("Matches should fail for invalid pattern")
}
if len(mt.messages) > 0 && !strings.Contains(mt.messages[0], "invalid regexp pattern") {
t.Errorf("expected 'invalid regexp pattern' in message, got: %s", mt.messages[0])
}
})
t.Run("empty pattern matches everything", func(t *testing.T) {
mt := newMockT(t)
attest.Matches(mt, "anything", "")
if mt.failed {
t.Error("empty pattern should match any string")
}
})
t.Run("empty string with empty pattern passes", func(t *testing.T) {
mt := newMockT(t)
attest.Matches(mt, "", "")
if mt.failed {
t.Error("empty string with empty pattern should pass")
}
})
}