-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
269 lines (245 loc) · 7.33 KB
/
errors_test.go
File metadata and controls
269 lines (245 loc) · 7.33 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package gotmx
import (
"errors"
"strings"
"testing"
)
func TestLevenshteinDistance(t *testing.T) {
tests := []struct {
a, b string
expected int
}{
{"", "", 0},
{"a", "", 1},
{"", "a", 1},
{"abc", "abc", 0},
{"abc", "abd", 1},
{"button", "buttn", 1},
{"button", "buton", 1},
{"kitten", "sitting", 3},
{"saturday", "sunday", 3},
}
for _, tt := range tests {
t.Run(tt.a+"_"+tt.b, func(t *testing.T) {
result := levenshteinDistance(tt.a, tt.b)
if result != tt.expected {
t.Errorf("levenshteinDistance(%q, %q) = %d, want %d", tt.a, tt.b, result, tt.expected)
}
})
}
}
func TestFindClosestMatch(t *testing.T) {
candidates := []TemplateName{"button", "card", "header", "footer", "navigation"}
tests := []struct {
target TemplateName
expected TemplateName
}{
{"buttn", "button"}, // 1 edit away
{"buton", "button"}, // 1 edit away
{"botton", "button"}, // 1 edit away
{"card", "card"}, // exact match
{"cardd", "card"}, // 1 edit away
{"xyz", ""}, // too far from any candidate
{"completely", ""}, // too far from any candidate
{"heater", "header"}, // 2 edits away
{"footr", "footer"}, // 1 edit away
{"nav", ""}, // too short to match navigation reliably
}
for _, tt := range tests {
t.Run(string(tt.target), func(t *testing.T) {
result := findClosestMatch(tt.target, candidates)
if result != tt.expected {
t.Errorf("findClosestMatch(%q) = %q, want %q", tt.target, result, tt.expected)
}
})
}
}
func TestTemplateNotFoundError(t *testing.T) {
t.Run("basic error", func(t *testing.T) {
err := &TemplateNotFoundError{
Name: "mytemplate",
}
if !strings.Contains(err.Error(), "mytemplate") {
t.Errorf("error should contain template name, got: %s", err.Error())
}
if !strings.Contains(err.Error(), "not found") {
t.Errorf("error should contain 'not found', got: %s", err.Error())
}
})
t.Run("with namespace", func(t *testing.T) {
err := &TemplateNotFoundError{
Name: "mytemplate",
Namespace: "frontend/components",
}
if !strings.Contains(err.Error(), "frontend/components") {
t.Errorf("error should contain namespace, got: %s", err.Error())
}
})
t.Run("with did you mean", func(t *testing.T) {
err := &TemplateNotFoundError{
Name: "buttn",
DidYouMean: "button",
}
if !strings.Contains(err.Error(), "did you mean") {
t.Errorf("error should contain 'did you mean', got: %s", err.Error())
}
if !strings.Contains(err.Error(), "button") {
t.Errorf("error should contain suggestion, got: %s", err.Error())
}
})
t.Run("with available templates", func(t *testing.T) {
err := &TemplateNotFoundError{
Name: "unknown",
Available: []TemplateName{"button", "card", "header"},
}
if !strings.Contains(err.Error(), "available") {
t.Errorf("error should contain 'available', got: %s", err.Error())
}
if !strings.Contains(err.Error(), "button") {
t.Errorf("error should list available templates, got: %s", err.Error())
}
})
t.Run("many available templates", func(t *testing.T) {
available := make([]TemplateName, 15)
for i := range available {
available[i] = TemplateName("template" + string(rune('a'+i)))
}
err := &TemplateNotFoundError{
Name: "unknown",
Available: available,
}
// Should show count instead of listing all
if !strings.Contains(err.Error(), "15 templates available") {
t.Errorf("error should show count for many templates, got: %s", err.Error())
}
})
}
func TestAmbiguousTemplateError(t *testing.T) {
err := &AmbiguousTemplateError{
Name: "button",
Namespaces: []Namespace{"frontend/components", "admin/components"},
}
errStr := err.Error()
if !strings.Contains(errStr, "button") {
t.Errorf("error should contain template name, got: %s", errStr)
}
if !strings.Contains(errStr, "ambiguous") {
t.Errorf("error should contain 'ambiguous', got: %s", errStr)
}
if !strings.Contains(errStr, "frontend/components") {
t.Errorf("error should list namespaces, got: %s", errStr)
}
if !strings.Contains(errStr, "namespace#button") {
t.Errorf("error should suggest qualified name format, got: %s", errStr)
}
}
func TestComponentNotFoundError(t *testing.T) {
t.Run("basic error", func(t *testing.T) {
err := &ComponentNotFoundError{
ComponentRef: "my-button",
}
if !strings.Contains(err.Error(), "my-button") {
t.Errorf("error should contain component ref, got: %s", err.Error())
}
})
t.Run("with cause", func(t *testing.T) {
cause := &TemplateNotFoundError{Name: "button"}
err := &ComponentNotFoundError{
ComponentRef: "my-button",
Cause: cause,
}
if !strings.Contains(err.Error(), "my-button") {
t.Errorf("error should contain component ref, got: %s", err.Error())
}
// Test Unwrap
unwrapped := errors.Unwrap(err)
if unwrapped != cause {
t.Errorf("Unwrap should return cause, got: %v", unwrapped)
}
})
}
func TestRenderError(t *testing.T) {
t.Run("full context", func(t *testing.T) {
cause := errors.New("value is nil")
err := &RenderError{
Template: "page.html",
Element: "div",
Attribute: "g-if",
Cause: cause,
}
errStr := err.Error()
if !strings.Contains(errStr, "page.html") {
t.Errorf("error should contain template, got: %s", errStr)
}
if !strings.Contains(errStr, "<div>") {
t.Errorf("error should contain element in brackets, got: %s", errStr)
}
if !strings.Contains(errStr, "[g-if]") {
t.Errorf("error should contain attribute in brackets, got: %s", errStr)
}
if !strings.Contains(errStr, "value is nil") {
t.Errorf("error should contain cause, got: %s", errStr)
}
})
t.Run("element only", func(t *testing.T) {
err := &RenderError{
Element: "span",
Cause: errors.New("failed"),
}
errStr := err.Error()
if !strings.Contains(errStr, "<span>") {
t.Errorf("error should contain element, got: %s", errStr)
}
if strings.Contains(errStr, "template") {
t.Errorf("error should not mention template when empty, got: %s", errStr)
}
})
t.Run("unwrap", func(t *testing.T) {
cause := errors.New("underlying error")
err := &RenderError{
Element: "div",
Cause: cause,
}
unwrapped := errors.Unwrap(err)
if unwrapped != cause {
t.Errorf("Unwrap should return cause, got: %v", unwrapped)
}
})
}
func TestWrapRenderError(t *testing.T) {
t.Run("wraps regular error", func(t *testing.T) {
cause := errors.New("something went wrong")
wrapped := wrapRenderError(cause, "div", "g-if")
var renderErr *RenderError
if !errors.As(wrapped, &renderErr) {
t.Fatal("wrapped error should be RenderError")
}
if renderErr.Element != "div" {
t.Errorf("Element = %q, want %q", renderErr.Element, "div")
}
if renderErr.Attribute != "g-if" {
t.Errorf("Attribute = %q, want %q", renderErr.Attribute, "g-if")
}
if renderErr.Cause != cause {
t.Errorf("Cause should be original error")
}
})
t.Run("does not double wrap", func(t *testing.T) {
original := &RenderError{
Element: "span",
Attribute: "g-with",
Cause: errors.New("inner error"),
}
wrapped := wrapRenderError(original, "div", "g-if")
// Should return the original error unchanged
if wrapped != original {
t.Error("should not double-wrap RenderError")
}
})
t.Run("returns nil for nil error", func(t *testing.T) {
result := wrapRenderError(nil, "div", "g-if")
if result != nil {
t.Error("should return nil for nil error")
}
})
}