Skip to content

Commit e7ee9aa

Browse files
committed
Line matcher should return an error instead of panic
1 parent 26ec2b5 commit e7ee9aa

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

pkg/runtime/validator.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,25 @@ func validateExpectedLines(got string, expected ExpectedOut) matcher.MatcherResu
137137
actualLines := strings.Split(got, getLineBreak())
138138
result := matcher.MatcherResult{Success: true}
139139

140-
for k, expL := range expected.Lines {
141-
if (k-1 > len(actualLines)) || (k-1 < 0) {
142-
panic(fmt.Sprintf("Invalid line number given %d", k))
140+
for key, expectedLine := range expected.Lines {
141+
// line number 0 or below 0
142+
if key <= 0 {
143+
panic(fmt.Sprintf("Invalid line number given %d", key))
143144
}
144145

145-
if result = m.Match(actualLines[k-1], expL); !result.Success {
146+
// line number exceeds result set
147+
if key > len(actualLines) {
148+
return matcher.MatcherResult{
149+
Success: false,
150+
Diff: fmt.Sprintf(
151+
"Line number %d does not exists in result: \n\n%s",
152+
key-1,
153+
strings.Join(actualLines, "\n"),
154+
),
155+
}
156+
}
157+
158+
if result = m.Match(actualLines[key-1], expectedLine); !result.Success {
146159
return result
147160
}
148161
}

pkg/runtime/validator_test.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,31 @@ output`
9494
assert.Empty(t, got.Diff)
9595
}
9696

97+
func Test_ValidateExpectedOut_MatchLines_ExpectedLineDoesNotExists(t *testing.T) {
98+
value := `test`
99+
100+
got := validateExpectedOut(value, ExpectedOut{Lines: map[int]string{2: "my", 3: "line"}})
101+
102+
assert.False(t, got.Success)
103+
diff := `Line number 1 does not exists in result:
104+
105+
test`
106+
assert.Equal(t, diff, got.Diff)
107+
}
108+
97109
func Test_ValidateExpectedOut_MatchLines_Fails(t *testing.T) {
98-
value := ``
110+
value := `test
111+
line 2
112+
line 3`
99113

100-
got := validateExpectedOut(value, ExpectedOut{Lines: map[int]string{1: "my", 3: "line"}})
114+
got := validateExpectedOut(value, ExpectedOut{Lines: map[int]string{2: "line 3"}})
101115

102116
assert.False(t, got.Success)
103117
diff := `--- Got
104118
+++ Expected
105119
@@ -1 +1 @@
106-
-
107-
+my
120+
-line 2
121+
+line 3
108122
`
109123
assert.Equal(t, diff, got.Diff)
110124
}
@@ -142,23 +156,6 @@ contains
142156
assert.Equal(t, diff, got.Diff)
143157
}
144158

145-
func Test_ValidateExpectedOut_PanicIfLineDoesNotExist_TooHigh(t *testing.T) {
146-
defer func() {
147-
r := recover()
148-
if r != nil {
149-
assert.Equal(t, "Invalid line number given 99", r)
150-
}
151-
assert.NotNil(t, r)
152-
}()
153-
154-
value := `my
155-
multi
156-
line
157-
output`
158-
159-
_ = validateExpectedOut(value, ExpectedOut{Lines: map[int]string{99: "my"}})
160-
}
161-
162159
func Test_ValidateExpectedOut_PanicIfLineDoesNotExist(t *testing.T) {
163160
defer func() {
164161
r := recover()

0 commit comments

Comments
 (0)