Skip to content

Commit

Permalink
clean up logic; add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Banning committed Jan 17, 2017
1 parent 622caf7 commit 580465b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 1 addition & 6 deletions reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,12 @@ func Tail(file string, n int) ([]string, error) {
for i = n - 1; i >= 0; i-- {
lines[i], err = fh.ReadLine()
if err == io.EOF {
i++ // lines[i] is ""; we'll strip it
lines = lines[i+1:] // back it out
break
} else if err != nil {
return lines[i:], err
}
}

// See if number of lines < 'n'.
if i > 0 {
lines = lines[i:]
}

return lines, nil
}
2 changes: 2 additions & 0 deletions small.file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a test file
With just 2 lines.
14 changes: 14 additions & 0 deletions tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ func TestTail(t *testing.T) {
}
}

func TestTailSmallFile(t *testing.T) {
fmt.Println("\n---------- TestTailSmallFile")
tail, err := Tail("small.file", 4)
if err != nil {
t.Fatal(err)
}
if len(tail) != 2 {
t.Fatal("tail has len:", len(tail))
}
for i, s := range tail {
fmt.Println(i, ":", s)
}
}

0 comments on commit 580465b

Please sign in to comment.