1
1
mirror of https://github.com/walles/moar.git synced 2024-11-26 13:46:16 +03:00

Add a test

This commit is contained in:
Johan Walles 2024-07-18 15:31:29 +02:00
parent 25c79625d5
commit 3a08bf62bb

View File

@ -416,9 +416,49 @@ func TestReadUpdatingFile_InitiallyEmpty(t *testing.T) {
assert.Equal(t, allLines.lines[0].Plain(nil), "Text")
}
// NOTE: Consider testing file tailing with the first part not ending in a
// linefeed. In this case, the last line should become longer when new bytes
// show up.
// If people keep appending to the currently opened file we should display those
// changes.
//
// This test verifies it with the initial contents not ending with a linefeed.
func TestReadUpdatingFile_HalfLine(t *testing.T) {
// Make a temp file containing one line of text, ending with a newline
file, err := os.CreateTemp("", "moar-TestReadUpdatingFile-*.txt")
assert.NilError(t, err)
defer os.Remove(file.Name())
_, err = file.WriteString("Start")
assert.NilError(t, err)
// Start a reader on that file
testMe, err := NewReaderFromFilename(file.Name(), *styles.Get("native"), formatters.TTY16m, nil)
assert.NilError(t, err)
// Wait for the reader to finish reading
assert.NilError(t, testMe._wait())
assert.Equal(t, int(testMe.bytesCount), len([]byte("Start")))
// Append the rest of the line
const secondLineString = ", end\n"
_, err = file.WriteString(secondLineString)
assert.NilError(t, err)
// Give the reader some time to react
for i := 0; i < 20; i++ {
allLines, _ := testMe.GetLines(linenumbers.LineNumber{}, 10)
if len(allLines.lines) == 2 {
break
}
time.Sleep(100 * time.Millisecond)
}
// Verify we got the two lines
allLines, _ := testMe.GetLines(linenumbers.LineNumber{}, 10)
assert.Equal(t, len(allLines.lines), 1, "Still expecting one line, got %d", len(allLines.lines))
assert.Equal(t, testMe.GetLineCount(), 1)
assert.Equal(t, allLines.lines[0].Plain(nil), "Start, end")
assert.Equal(t, int(testMe.bytesCount), len([]byte("Start, end\n")))
}
// NOTE: Consider testing file tailing with the first part ending ending in the
// middle of an UTF-8 character. In this case, the new bytes should complete the