Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 129 additions & 21 deletions internal/diags/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func TestLineRangeString(t *testing.T) {
testCases := []testCaseT{
{lr: LineRange{First: 1, Last: 1}, expected: "1"},
{lr: LineRange{First: 1, Last: 2}, expected: "1-2"},
{lr: LineRange{First: 5, Last: 10}, expected: "5-10"},
}

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

func TestLineRangeExpand(t *testing.T) {
lr := LineRange{First: 1, Last: 3}
require.Equal(t, []int{1, 2, 3}, lr.Expand())
type testCaseT struct {
name string
expected []int
lr LineRange
}

testCases := []testCaseT{
{name: "basic range", lr: LineRange{First: 1, Last: 3}, expected: []int{1, 2, 3}},
{name: "single line", lr: LineRange{First: 5, Last: 5}, expected: []int{5}},
{name: "large range", lr: LineRange{First: 10, Last: 15}, expected: []int{10, 11, 12, 13, 14, 15}},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, tc.lr.Expand())
})
}
}

func TestPositionRangesLines(t *testing.T) {
prs := PositionRanges{
{Line: 2},
{Line: 5},
{Line: 3},
}
lr := prs.Lines()
require.Equal(t, 2, lr.First)
require.Equal(t, 5, lr.Last)
type testCaseT struct {
name string
prs PositionRanges
expectFirst int
expectLast int
}

testCases := []testCaseT{
{
name: "multiple positions",
prs: PositionRanges{{Line: 2}, {Line: 5}, {Line: 3}},
expectFirst: 2,
expectLast: 5,
},
{
name: "single position",
prs: PositionRanges{{Line: 42}},
expectFirst: 42,
expectLast: 42,
},
{
name: "empty",
prs: PositionRanges{},
expectFirst: 0,
expectLast: 0,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
lr := tc.prs.Lines()
require.Equal(t, tc.expectFirst, lr.First)
require.Equal(t, tc.expectLast, lr.Last)
})
}
}

func TestPositionRangesAddOffset(t *testing.T) {
prs := PositionRanges{
{Line: 1, FirstColumn: 2, LastColumn: 3},
{Line: 2, FirstColumn: 3, LastColumn: 4},
}
prs.AddOffset(10, 20)
require.Equal(t, 11, prs[0].Line)
require.Equal(t, 22, prs[0].FirstColumn)
require.Equal(t, 23, prs[0].LastColumn)
require.Equal(t, 12, prs[1].Line)
require.Equal(t, 23, prs[1].FirstColumn)
require.Equal(t, 24, prs[1].LastColumn)
type testCaseT struct {
name string
prs PositionRanges
expected PositionRanges
lineOffset int
columnOffset int
}

testCases := []testCaseT{
{
name: "multiple positions with offset",
prs: PositionRanges{
{Line: 1, FirstColumn: 2, LastColumn: 3},
{Line: 2, FirstColumn: 3, LastColumn: 4},
},
lineOffset: 10,
columnOffset: 20,
expected: PositionRanges{
{Line: 11, FirstColumn: 22, LastColumn: 23},
{Line: 12, FirstColumn: 23, LastColumn: 24},
},
},
{
name: "zero offset",
prs: PositionRanges{
{Line: 5, FirstColumn: 10, LastColumn: 20},
},
lineOffset: 0,
columnOffset: 0,
expected: PositionRanges{
{Line: 5, FirstColumn: 10, LastColumn: 20},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.prs.AddOffset(tc.lineOffset, tc.columnOffset)
require.Equal(t, tc.expected, tc.prs)
})
}
}

func TestPositionRangesLen(t *testing.T) {
type testCaseT struct {
name string
prs PositionRanges
expected int
}

testCases := []testCaseT{
{
name: "multiple ranges",
prs: PositionRanges{
{Line: 1, FirstColumn: 1, LastColumn: 5},
{Line: 2, FirstColumn: 1, LastColumn: 3},
},
expected: 8,
},
{
name: "empty",
prs: PositionRanges{},
expected: 0,
},
{
name: "single column",
prs: PositionRanges{{Line: 1, FirstColumn: 5, LastColumn: 5}},
expected: 1,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, tc.prs.Len())
})
}
}
74 changes: 67 additions & 7 deletions internal/diags/problems_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,77 @@ expr: |
}

func TestCountDigits(t *testing.T) {
require.Equal(t, 1, countDigits(1))
require.Equal(t, 2, countDigits(10))
require.Equal(t, 3, countDigits(100))
type testCaseT struct {
name string
input int
expected int
}

testCases := []testCaseT{
{name: "single digit", input: 1, expected: 1},
{name: "two digits", input: 10, expected: 2},
{name: "three digits", input: 100, expected: 3},
{name: "zero", input: 0, expected: 0},
{name: "four digits", input: 1000, expected: 4},
{name: "five digits", input: 99999, expected: 5},
{name: "six digits", input: 123456, expected: 6},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, countDigits(tc.input))
})
}
}

func TestLineCoverage(t *testing.T) {
diags := []Diagnostic{
{Pos: PositionRanges{{Line: 1}, {Line: 2}}},
{Pos: PositionRanges{{Line: 2}, {Line: 3}}},
type testCaseT struct {
name string
diags []Diagnostic
expected []int
}

testCases := []testCaseT{
{
name: "multiple lines",
diags: []Diagnostic{
{Pos: PositionRanges{{Line: 1}, {Line: 2}}},
{Pos: PositionRanges{{Line: 2}, {Line: 3}}},
},
expected: []int{1, 2, 3},
},
{
name: "empty",
diags: []Diagnostic{},
expected: nil,
},
{
name: "single line",
diags: []Diagnostic{
{Pos: PositionRanges{{Line: 5}}},
},
expected: []int{5},
},
{
name: "duplicates",
diags: []Diagnostic{
{Pos: PositionRanges{{Line: 3}, {Line: 3}}},
{Pos: PositionRanges{{Line: 3}, {Line: 5}}},
},
expected: []int{3, 5},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := lineCoverage(tc.diags)
if tc.expected == nil {
require.Empty(t, result)
} else {
require.Equal(t, tc.expected, result)
}
})
}
require.Equal(t, []int{1, 2, 3}, lineCoverage(diags))
}

func TestInjectDiagnosticsKind(t *testing.T) {
Expand Down
113 changes: 113 additions & 0 deletions internal/output/humanize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ func TestHumanizeDuration(t *testing.T) {
input: (time.Hour * (24*7*14 + 24*6 + 3)) + time.Minute*33 + time.Second*3 + time.Millisecond + 999 + time.Microsecond*5,
output: "14w6d3h33m3s1ms",
},
{
input: time.Minute*3 + time.Second*45,
output: "3m45s",
},
{
input: time.Second*30 + time.Millisecond*250,
output: "30s250ms",
},
{
input: time.Hour*24*7 + time.Millisecond*1,
output: "1w1ms",
},
{
input: time.Millisecond,
output: "1ms",
},
{
input: time.Hour,
output: "1h",
},
{
input: time.Minute,
output: "1m",
},
{
input: time.Second,
output: "1s",
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -106,6 +134,22 @@ func TestHumanizeBytes(t *testing.T) {
input: 1024 * 1024 * 1024 * 1024,
output: "1.0TiB",
},
{
input: 1024 * 1024 * 1024 * 1024 * 1024,
output: "1.0PiB",
},
{
input: 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
output: "1.0EiB",
},
{
input: 512,
output: "512B",
},
{
input: 1536,
output: "1.5KiB",
},
}

for _, tc := range testCases {
Expand All @@ -115,3 +159,72 @@ func TestHumanizeBytes(t *testing.T) {
})
}
}

func TestMaybeColor(t *testing.T) {
type testCaseT struct {
name string
input string
output string
color output.Color
disabled bool
}

testCases := []testCaseT{
{
name: "color disabled",
color: output.Red,
disabled: true,
input: "test string",
output: "test string",
},
{
name: "color enabled with red",
color: output.Red,
disabled: false,
input: "error",
output: "\033[91merror\033[0m",
},
{
name: "color enabled with yellow",
color: output.Yellow,
disabled: false,
input: "warning",
output: "\033[93mwarning\033[0m",
},
{
name: "color enabled with blue",
color: output.Blue,
disabled: false,
input: "info",
output: "\033[94minfo\033[0m",
},
{
name: "color enabled with bold",
color: output.Bold,
disabled: false,
input: "bold text",
output: "\033[1mbold text\033[0m",
},
{
name: "color enabled with none",
color: output.None,
disabled: false,
input: "no color",
output: "\033[0mno color\033[0m",
},
{
name: "empty string with color",
color: output.Cyan,
disabled: false,
input: "",
output: "\033[96m\033[0m",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := output.MaybeColor(tc.color, tc.disabled, tc.input)
require.Equal(t, tc.output, result)
})
}
}
Loading
Loading