1
1
mirror of https://github.com/walles/moar.git synced 2024-11-09 17:40:09 +03:00

Fix off-by-one error in the _Reader

This commit is contained in:
Johan Walles 2019-06-14 06:49:27 +02:00
parent da30eaf956
commit 0abaf16da5
2 changed files with 32 additions and 7 deletions

View File

@ -77,7 +77,7 @@ func (r *_Reader) GetLines(firstLineOneBased int, wantedLineCount int) *Lines {
lastLineZeroBased := firstLineZeroBased + wantedLineCount - 1
if lastLineZeroBased > len(r.lines) {
lastLineZeroBased = len(r.lines)
lastLineZeroBased = len(r.lines) - 1
}
// Prevent reading past the end of the available lines

View File

@ -1,6 +1,11 @@
package m
import "testing"
import (
"testing"
"path"
"runtime"
"io/ioutil"
)
func _TestGetLines(t *testing.T, reader *_Reader) {
t.Logf("Testing file: %s...", reader.name)
@ -19,19 +24,39 @@ func _TestGetLines(t *testing.T, reader *_Reader) {
lineCount := reader.LineCount()
lines = reader.GetLines(lineCount, 10)
if len(lines.lines) != 10 {
t.Errorf("Asked for 10 lines, got too few: %d", len(lines.lines))
t.Errorf("Asked for 10 lines but got %d", len(lines.lines))
return
}
if lines.firstLineOneBased != lineCount - 9 {
t.Errorf("Expected last line to be %d, was %d", lineCount - 9, lines.firstLineOneBased)
t.Errorf("Expected first line to be %d, was %d", lineCount - 9, lines.firstLineOneBased)
return
}
}
func TestGetLines(t *testing.T) {
// FIXME: List all files in ../sample-files
files := []string {"../sample-files/empty.txt"}
func _GetTestFiles() []string {
// From: https://coderwall.com/p/_fmbug/go-get-path-to-current-file
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("Getting current filename failed")
}
samplesDir := path.Join(path.Dir(filename), "../sample-files")
files, err := ioutil.ReadDir(samplesDir)
if err != nil {
panic(err)
}
var filenames []string
for _, file := range files {
filenames = append(filenames, "../sample-files/" + file.Name())
}
return filenames
}
func TestGetLines(t *testing.T) {
for _, file := range _GetTestFiles() {
reader, e := NewReaderFromFilename(file)
if e != nil {
t.Errorf("Error opening file <%s>: %s", file, e.Error())