1
1
mirror of https://github.com/walles/moar.git synced 2024-11-22 21:50:43 +03:00

Make some more tests pass

This commit is contained in:
Johan Walles 2024-01-07 23:17:34 +01:00
parent d2b3fad36b
commit 1c58d83bd3
4 changed files with 22 additions and 11 deletions

View File

@ -96,7 +96,7 @@ func (l LineNumber) Format() string {
// If both lines are the same this method will return 1.
func (l LineNumber) CountLinesTo(next LineNumber) int {
if l.number > next.number {
panic(fmt.Errorf("line numbers must be ordered, got %d-%d", l.number, next.number))
panic(fmt.Errorf("line numbers must be ordered, got %s-%s", l.Format(), next.Format()))
}
return 1 + next.AsZeroBased() - l.AsZeroBased()

View File

@ -45,3 +45,15 @@ func TestLineNumberEquality(t *testing.T) {
assert.Equal(t, LineNumberFromZeroBased(1), LineNumberFromOneBased(2),
"Two different ways of representing the same line number should be equal")
}
func TestLineNumberCountLinesTo(t *testing.T) {
assert.Equal(t,
LineNumberFromZeroBased(0).CountLinesTo(LineNumberFromZeroBased(0)),
1, // Count is inclusive, so countint from 0 to 0 is 1
)
assert.Equal(t,
LineNumberFromZeroBased(0).CountLinesTo(LineNumberFromZeroBased(1)),
2, // Count is inclusive, so countint from 0 to 1 is 2
)
}

View File

@ -638,17 +638,16 @@ func (reader *Reader) getLinesUnlocked(firstLine linenumbers.LineNumber, wantedL
lastLine := firstLine.NonWrappingAdd(wantedLineCount - 1)
if lastLine.AsZeroBased() >= len(reader.lines) {
lastLine = *linenumbers.LineNumberFromLength(len(reader.lines))
}
// Prevent reading past the end of the available lines
if firstLine.CountLinesTo(lastLine) < wantedLineCount && !firstLine.IsZero() {
// 5 would mean we've gone 5 lines too far
overshoot := wantedLineCount - firstLine.CountLinesTo(lastLine)
firstLine = firstLine.NonWrappingAdd(-overshoot)
maxLineNumber := *linenumbers.LineNumberFromLength(len(reader.lines))
if lastLine.IsAfter(maxLineNumber) {
lastLine = maxLineNumber
return reader.getLinesUnlocked(firstLine, wantedLineCount)
// If one line was requested, then first and last should be exactly the
// same, and we would get there by adding zero.
firstLine = lastLine.NonWrappingAdd(1 - wantedLineCount)
return reader.getLinesUnlocked(firstLine, firstLine.CountLinesTo(lastLine))
}
returnLines := reader.lines[firstLine.AsZeroBased() : lastLine.AsZeroBased()+1]

View File

@ -36,7 +36,7 @@ func testCanonicalize1000(t *testing.T, withStatusBar bool, currentStartLine lin
},
}
assert.Equal(t, lastVisiblePosition.lineNumber(&pager), lastVisibleLine)
assert.Equal(t, *lastVisiblePosition.lineNumber(&pager), lastVisibleLine)
}
func TestCanonicalize1000WithStatusBar(t *testing.T) {