From da30eaf956a7807b8730d127a70e623dc5f92779 Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Thu, 13 Jun 2019 20:04:51 +0200 Subject: [PATCH] Handle displaying empty files And introduce a unit test! --- m/reader.go | 14 +++++++++++++- m/reader_test.go | 43 ++++++++++++++++++++++++++++++++++++++++++ moar.sh | 2 +- sample-files/empty.txt | 0 test.sh | 2 +- 5 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 m/reader_test.go create mode 100644 sample-files/empty.txt diff --git a/m/reader.go b/m/reader.go index 5cc5046..20f1b96 100644 --- a/m/reader.go +++ b/m/reader.go @@ -28,6 +28,7 @@ type Lines struct { // NewReaderFromStream creates a new stream reader func NewReaderFromStream(reader io.Reader) (*_Reader, error) { + // FIXME: Close the stream when done reading it? scanner := bufio.NewScanner(reader) var lines []string for scanner.Scan() { @@ -49,7 +50,9 @@ func NewReaderFromFilename(filename string) (*_Reader, error) { return nil, err } - return NewReaderFromStream(stream) + reader, err := NewReaderFromStream(stream) + reader.name = filename + return reader, err } func (r *_Reader) LineCount() int { @@ -61,6 +64,15 @@ func (r *_Reader) GetLines(firstLineOneBased int, wantedLineCount int) *Lines { firstLineOneBased = 1 } + if len(r.lines) == 0 { + return &Lines{ + lines: r.lines, + + // FIXME: What line number should we set here? + firstLineOneBased: firstLineOneBased, + } + } + firstLineZeroBased := firstLineOneBased - 1 lastLineZeroBased := firstLineZeroBased + wantedLineCount - 1 diff --git a/m/reader_test.go b/m/reader_test.go new file mode 100644 index 0000000..0d04132 --- /dev/null +++ b/m/reader_test.go @@ -0,0 +1,43 @@ +package m + +import "testing" + +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 { + t.Errorf("Asked for 10 lines, got too few: %d", len(lines.lines)) + return + } + if lines.firstLineOneBased != lineCount - 9 { + t.Errorf("Expected last 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"} + for _, file := range files { + reader, e := NewReaderFromFilename(file) + if e != nil { + t.Errorf("Error opening file <%s>: %s", file, e.Error()) + continue + } + + _TestGetLines(t, reader) + } +} \ No newline at end of file diff --git a/moar.sh b/moar.sh index 4d2420d..9c43912 100755 --- a/moar.sh +++ b/moar.sh @@ -4,7 +4,7 @@ set -e -o pipefail rm -f moar -go test 1>&2 +go test github.com/walles/moar/m 1>&2 go build 1>&2 diff --git a/sample-files/empty.txt b/sample-files/empty.txt new file mode 100644 index 0000000..e69de29 diff --git a/test.sh b/test.sh index 026a622..20655d5 100755 --- a/test.sh +++ b/test.sh @@ -7,7 +7,7 @@ GOOS=linux GOARCH=386 go build GOOS=darwin GOARCH=amd64 go build # Unit tests first... -go test +go test github.com/walles/moar/m # ... then Verify sending the output to a file go build