2019-06-13 21:04:51 +03:00
|
|
|
package m
|
|
|
|
|
2019-06-14 07:49:27 +03:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
2019-06-13 21:04:51 +03:00
|
|
|
|
|
|
|
func _TestGetLines(t *testing.T, reader *_Reader) {
|
|
|
|
t.Logf("Testing file: %s...", reader.name)
|
|
|
|
|
|
|
|
lines := reader.GetLines(1, 10)
|
|
|
|
if len(lines.lines) > 10 {
|
|
|
|
t.Errorf("Asked for 10 lines, got too many: %d", len(lines.lines))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lines.lines) < 10 {
|
|
|
|
// No good plan for how to test short files more
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test clipping at the end
|
|
|
|
lineCount := reader.LineCount()
|
|
|
|
lines = reader.GetLines(lineCount, 10)
|
|
|
|
if len(lines.lines) != 10 {
|
2019-06-14 07:49:27 +03:00
|
|
|
t.Errorf("Asked for 10 lines but got %d", len(lines.lines))
|
2019-06-13 21:04:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if lines.firstLineOneBased != lineCount - 9 {
|
2019-06-14 07:49:27 +03:00
|
|
|
t.Errorf("Expected first line to be %d, was %d", lineCount - 9, lines.firstLineOneBased)
|
2019-06-13 21:04:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 07:49:27 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:04:51 +03:00
|
|
|
func TestGetLines(t *testing.T) {
|
2019-06-14 07:49:27 +03:00
|
|
|
for _, file := range _GetTestFiles() {
|
2019-06-13 21:04:51 +03:00
|
|
|
reader, e := NewReaderFromFilename(file)
|
|
|
|
if e != nil {
|
|
|
|
t.Errorf("Error opening file <%s>: %s", file, e.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_TestGetLines(t, reader)
|
|
|
|
}
|
|
|
|
}
|