mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
Add test demonstrating problem with ForEachLineInFile
The function drops the last line if it doesn't end with a line feed.
This commit is contained in:
63
pkg/utils/io_test.go
Normal file
63
pkg/utils/io_test.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_forEachLineInStream(t *testing.T) {
|
||||||
|
scenarios := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
expectedLines []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty input",
|
||||||
|
input: "",
|
||||||
|
expectedLines: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "single line",
|
||||||
|
input: "abc\n",
|
||||||
|
expectedLines: []string{"abc\n"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "single line without line feed",
|
||||||
|
input: "abc",
|
||||||
|
/* EXPECTED:
|
||||||
|
expectedLines: []string{"abc"},
|
||||||
|
ACTUAL: */
|
||||||
|
expectedLines: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple lines",
|
||||||
|
input: "abc\ndef\n",
|
||||||
|
expectedLines: []string{"abc\n", "def\n"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple lines including empty lines",
|
||||||
|
input: "abc\n\ndef\n",
|
||||||
|
expectedLines: []string{"abc\n", "\n", "def\n"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple lines without linefeed at end of file",
|
||||||
|
input: "abc\ndef\nghi",
|
||||||
|
/* EXPECTED:
|
||||||
|
expectedLines: []string{"abc\n", "def\n", "ghi"},
|
||||||
|
ACTUAL: */
|
||||||
|
expectedLines: []string{"abc\n", "def\n"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range scenarios {
|
||||||
|
t.Run(s.name, func(t *testing.T) {
|
||||||
|
lines := []string{}
|
||||||
|
forEachLineInStream(strings.NewReader(s.input), func(line string, i int) {
|
||||||
|
lines = append(lines, line)
|
||||||
|
})
|
||||||
|
assert.EqualValues(t, s.expectedLines, lines)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user