Skip to content

Commit 72cf3ef

Browse files
committed
Add test demonstrating problem with ForEachLineInFile
The function drops the last line if it doesn't end with a line feed.
1 parent ae610dc commit 72cf3ef

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

pkg/utils/io_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package utils
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Test_forEachLineInStream(t *testing.T) {
11+
scenarios := []struct {
12+
name string
13+
input string
14+
expectedLines []string
15+
}{
16+
{
17+
name: "empty input",
18+
input: "",
19+
expectedLines: []string{},
20+
},
21+
{
22+
name: "single line",
23+
input: "abc\n",
24+
expectedLines: []string{"abc\n"},
25+
},
26+
{
27+
name: "single line without line feed",
28+
input: "abc",
29+
/* EXPECTED:
30+
expectedLines: []string{"abc"},
31+
ACTUAL: */
32+
expectedLines: []string{},
33+
},
34+
{
35+
name: "multiple lines",
36+
input: "abc\ndef\n",
37+
expectedLines: []string{"abc\n", "def\n"},
38+
},
39+
{
40+
name: "multiple lines including empty lines",
41+
input: "abc\n\ndef\n",
42+
expectedLines: []string{"abc\n", "\n", "def\n"},
43+
},
44+
{
45+
name: "multiple lines without linefeed at end of file",
46+
input: "abc\ndef\nghi",
47+
/* EXPECTED:
48+
expectedLines: []string{"abc\n", "def\n", "ghi"},
49+
ACTUAL: */
50+
expectedLines: []string{"abc\n", "def\n"},
51+
},
52+
}
53+
54+
for _, s := range scenarios {
55+
t.Run(s.name, func(t *testing.T) {
56+
lines := []string{}
57+
forEachLineInStream(strings.NewReader(s.input), func(line string, i int) {
58+
lines = append(lines, line)
59+
})
60+
assert.EqualValues(t, s.expectedLines, lines)
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)