Skip to content

Commit 5f295ff

Browse files
authored
Merge pull request #1623 from cloudflare/tests
Add more tests
2 parents b4b1bf5 + 05e66c7 commit 5f295ff

File tree

4 files changed

+329
-28
lines changed

4 files changed

+329
-28
lines changed

internal/diags/position_test.go

Lines changed: 129 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func TestLineRangeString(t *testing.T) {
205205
testCases := []testCaseT{
206206
{lr: LineRange{First: 1, Last: 1}, expected: "1"},
207207
{lr: LineRange{First: 1, Last: 2}, expected: "1-2"},
208+
{lr: LineRange{First: 5, Last: 10}, expected: "5-10"},
208209
}
209210

210211
for _, tc := range testCases {
@@ -215,31 +216,138 @@ func TestLineRangeString(t *testing.T) {
215216
}
216217

217218
func TestLineRangeExpand(t *testing.T) {
218-
lr := LineRange{First: 1, Last: 3}
219-
require.Equal(t, []int{1, 2, 3}, lr.Expand())
219+
type testCaseT struct {
220+
name string
221+
expected []int
222+
lr LineRange
223+
}
224+
225+
testCases := []testCaseT{
226+
{name: "basic range", lr: LineRange{First: 1, Last: 3}, expected: []int{1, 2, 3}},
227+
{name: "single line", lr: LineRange{First: 5, Last: 5}, expected: []int{5}},
228+
{name: "large range", lr: LineRange{First: 10, Last: 15}, expected: []int{10, 11, 12, 13, 14, 15}},
229+
}
230+
231+
for _, tc := range testCases {
232+
t.Run(tc.name, func(t *testing.T) {
233+
require.Equal(t, tc.expected, tc.lr.Expand())
234+
})
235+
}
220236
}
221237

222238
func TestPositionRangesLines(t *testing.T) {
223-
prs := PositionRanges{
224-
{Line: 2},
225-
{Line: 5},
226-
{Line: 3},
227-
}
228-
lr := prs.Lines()
229-
require.Equal(t, 2, lr.First)
230-
require.Equal(t, 5, lr.Last)
239+
type testCaseT struct {
240+
name string
241+
prs PositionRanges
242+
expectFirst int
243+
expectLast int
244+
}
245+
246+
testCases := []testCaseT{
247+
{
248+
name: "multiple positions",
249+
prs: PositionRanges{{Line: 2}, {Line: 5}, {Line: 3}},
250+
expectFirst: 2,
251+
expectLast: 5,
252+
},
253+
{
254+
name: "single position",
255+
prs: PositionRanges{{Line: 42}},
256+
expectFirst: 42,
257+
expectLast: 42,
258+
},
259+
{
260+
name: "empty",
261+
prs: PositionRanges{},
262+
expectFirst: 0,
263+
expectLast: 0,
264+
},
265+
}
266+
267+
for _, tc := range testCases {
268+
t.Run(tc.name, func(t *testing.T) {
269+
lr := tc.prs.Lines()
270+
require.Equal(t, tc.expectFirst, lr.First)
271+
require.Equal(t, tc.expectLast, lr.Last)
272+
})
273+
}
231274
}
232275

233276
func TestPositionRangesAddOffset(t *testing.T) {
234-
prs := PositionRanges{
235-
{Line: 1, FirstColumn: 2, LastColumn: 3},
236-
{Line: 2, FirstColumn: 3, LastColumn: 4},
237-
}
238-
prs.AddOffset(10, 20)
239-
require.Equal(t, 11, prs[0].Line)
240-
require.Equal(t, 22, prs[0].FirstColumn)
241-
require.Equal(t, 23, prs[0].LastColumn)
242-
require.Equal(t, 12, prs[1].Line)
243-
require.Equal(t, 23, prs[1].FirstColumn)
244-
require.Equal(t, 24, prs[1].LastColumn)
277+
type testCaseT struct {
278+
name string
279+
prs PositionRanges
280+
expected PositionRanges
281+
lineOffset int
282+
columnOffset int
283+
}
284+
285+
testCases := []testCaseT{
286+
{
287+
name: "multiple positions with offset",
288+
prs: PositionRanges{
289+
{Line: 1, FirstColumn: 2, LastColumn: 3},
290+
{Line: 2, FirstColumn: 3, LastColumn: 4},
291+
},
292+
lineOffset: 10,
293+
columnOffset: 20,
294+
expected: PositionRanges{
295+
{Line: 11, FirstColumn: 22, LastColumn: 23},
296+
{Line: 12, FirstColumn: 23, LastColumn: 24},
297+
},
298+
},
299+
{
300+
name: "zero offset",
301+
prs: PositionRanges{
302+
{Line: 5, FirstColumn: 10, LastColumn: 20},
303+
},
304+
lineOffset: 0,
305+
columnOffset: 0,
306+
expected: PositionRanges{
307+
{Line: 5, FirstColumn: 10, LastColumn: 20},
308+
},
309+
},
310+
}
311+
312+
for _, tc := range testCases {
313+
t.Run(tc.name, func(t *testing.T) {
314+
tc.prs.AddOffset(tc.lineOffset, tc.columnOffset)
315+
require.Equal(t, tc.expected, tc.prs)
316+
})
317+
}
318+
}
319+
320+
func TestPositionRangesLen(t *testing.T) {
321+
type testCaseT struct {
322+
name string
323+
prs PositionRanges
324+
expected int
325+
}
326+
327+
testCases := []testCaseT{
328+
{
329+
name: "multiple ranges",
330+
prs: PositionRanges{
331+
{Line: 1, FirstColumn: 1, LastColumn: 5},
332+
{Line: 2, FirstColumn: 1, LastColumn: 3},
333+
},
334+
expected: 8,
335+
},
336+
{
337+
name: "empty",
338+
prs: PositionRanges{},
339+
expected: 0,
340+
},
341+
{
342+
name: "single column",
343+
prs: PositionRanges{{Line: 1, FirstColumn: 5, LastColumn: 5}},
344+
expected: 1,
345+
},
346+
}
347+
348+
for _, tc := range testCases {
349+
t.Run(tc.name, func(t *testing.T) {
350+
require.Equal(t, tc.expected, tc.prs.Len())
351+
})
352+
}
245353
}

internal/diags/problems_test.go

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,77 @@ expr: |
160160
}
161161

162162
func TestCountDigits(t *testing.T) {
163-
require.Equal(t, 1, countDigits(1))
164-
require.Equal(t, 2, countDigits(10))
165-
require.Equal(t, 3, countDigits(100))
163+
type testCaseT struct {
164+
name string
165+
input int
166+
expected int
167+
}
168+
169+
testCases := []testCaseT{
170+
{name: "single digit", input: 1, expected: 1},
171+
{name: "two digits", input: 10, expected: 2},
172+
{name: "three digits", input: 100, expected: 3},
173+
{name: "zero", input: 0, expected: 0},
174+
{name: "four digits", input: 1000, expected: 4},
175+
{name: "five digits", input: 99999, expected: 5},
176+
{name: "six digits", input: 123456, expected: 6},
177+
}
178+
179+
for _, tc := range testCases {
180+
t.Run(tc.name, func(t *testing.T) {
181+
require.Equal(t, tc.expected, countDigits(tc.input))
182+
})
183+
}
166184
}
167185

168186
func TestLineCoverage(t *testing.T) {
169-
diags := []Diagnostic{
170-
{Pos: PositionRanges{{Line: 1}, {Line: 2}}},
171-
{Pos: PositionRanges{{Line: 2}, {Line: 3}}},
187+
type testCaseT struct {
188+
name string
189+
diags []Diagnostic
190+
expected []int
191+
}
192+
193+
testCases := []testCaseT{
194+
{
195+
name: "multiple lines",
196+
diags: []Diagnostic{
197+
{Pos: PositionRanges{{Line: 1}, {Line: 2}}},
198+
{Pos: PositionRanges{{Line: 2}, {Line: 3}}},
199+
},
200+
expected: []int{1, 2, 3},
201+
},
202+
{
203+
name: "empty",
204+
diags: []Diagnostic{},
205+
expected: nil,
206+
},
207+
{
208+
name: "single line",
209+
diags: []Diagnostic{
210+
{Pos: PositionRanges{{Line: 5}}},
211+
},
212+
expected: []int{5},
213+
},
214+
{
215+
name: "duplicates",
216+
diags: []Diagnostic{
217+
{Pos: PositionRanges{{Line: 3}, {Line: 3}}},
218+
{Pos: PositionRanges{{Line: 3}, {Line: 5}}},
219+
},
220+
expected: []int{3, 5},
221+
},
222+
}
223+
224+
for _, tc := range testCases {
225+
t.Run(tc.name, func(t *testing.T) {
226+
result := lineCoverage(tc.diags)
227+
if tc.expected == nil {
228+
require.Empty(t, result)
229+
} else {
230+
require.Equal(t, tc.expected, result)
231+
}
232+
})
172233
}
173-
require.Equal(t, []int{1, 2, 3}, lineCoverage(diags))
174234
}
175235

176236
func TestInjectDiagnosticsKind(t *testing.T) {

internal/output/humanize_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ func TestHumanizeDuration(t *testing.T) {
5757
input: (time.Hour * (24*7*14 + 24*6 + 3)) + time.Minute*33 + time.Second*3 + time.Millisecond + 999 + time.Microsecond*5,
5858
output: "14w6d3h33m3s1ms",
5959
},
60+
{
61+
input: time.Minute*3 + time.Second*45,
62+
output: "3m45s",
63+
},
64+
{
65+
input: time.Second*30 + time.Millisecond*250,
66+
output: "30s250ms",
67+
},
68+
{
69+
input: time.Hour*24*7 + time.Millisecond*1,
70+
output: "1w1ms",
71+
},
72+
{
73+
input: time.Millisecond,
74+
output: "1ms",
75+
},
76+
{
77+
input: time.Hour,
78+
output: "1h",
79+
},
80+
{
81+
input: time.Minute,
82+
output: "1m",
83+
},
84+
{
85+
input: time.Second,
86+
output: "1s",
87+
},
6088
}
6189

6290
for _, tc := range testCases {
@@ -106,6 +134,22 @@ func TestHumanizeBytes(t *testing.T) {
106134
input: 1024 * 1024 * 1024 * 1024,
107135
output: "1.0TiB",
108136
},
137+
{
138+
input: 1024 * 1024 * 1024 * 1024 * 1024,
139+
output: "1.0PiB",
140+
},
141+
{
142+
input: 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
143+
output: "1.0EiB",
144+
},
145+
{
146+
input: 512,
147+
output: "512B",
148+
},
149+
{
150+
input: 1536,
151+
output: "1.5KiB",
152+
},
109153
}
110154

111155
for _, tc := range testCases {
@@ -115,3 +159,72 @@ func TestHumanizeBytes(t *testing.T) {
115159
})
116160
}
117161
}
162+
163+
func TestMaybeColor(t *testing.T) {
164+
type testCaseT struct {
165+
name string
166+
input string
167+
output string
168+
color output.Color
169+
disabled bool
170+
}
171+
172+
testCases := []testCaseT{
173+
{
174+
name: "color disabled",
175+
color: output.Red,
176+
disabled: true,
177+
input: "test string",
178+
output: "test string",
179+
},
180+
{
181+
name: "color enabled with red",
182+
color: output.Red,
183+
disabled: false,
184+
input: "error",
185+
output: "\033[91merror\033[0m",
186+
},
187+
{
188+
name: "color enabled with yellow",
189+
color: output.Yellow,
190+
disabled: false,
191+
input: "warning",
192+
output: "\033[93mwarning\033[0m",
193+
},
194+
{
195+
name: "color enabled with blue",
196+
color: output.Blue,
197+
disabled: false,
198+
input: "info",
199+
output: "\033[94minfo\033[0m",
200+
},
201+
{
202+
name: "color enabled with bold",
203+
color: output.Bold,
204+
disabled: false,
205+
input: "bold text",
206+
output: "\033[1mbold text\033[0m",
207+
},
208+
{
209+
name: "color enabled with none",
210+
color: output.None,
211+
disabled: false,
212+
input: "no color",
213+
output: "\033[0mno color\033[0m",
214+
},
215+
{
216+
name: "empty string with color",
217+
color: output.Cyan,
218+
disabled: false,
219+
input: "",
220+
output: "\033[96m\033[0m",
221+
},
222+
}
223+
224+
for _, tc := range testCases {
225+
t.Run(tc.name, func(t *testing.T) {
226+
result := output.MaybeColor(tc.color, tc.disabled, tc.input)
227+
require.Equal(t, tc.output, result)
228+
})
229+
}
230+
}

0 commit comments

Comments
 (0)