-
Notifications
You must be signed in to change notification settings - Fork 0
/
tail_test.go
40 lines (35 loc) · 1.01 KB
/
tail_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"runtime"
"strings"
"testing"
)
func TestTail(t *testing.T) {
tests := []struct {
input string
n int
expected string
}{
{"line1\nline2\nline3\nline4\nline5\n", 2, "line4\nline5"},
{"line1\nline2\nline3\nline4\nline5\n", 0, ""},
{"line1\nline2\nline3\nline4\nline5\n", 5, "line1\nline2\nline3\nline4\nline5"},
{"line1\nline2\nline3\nline4\nline5\n", 10, "line1\nline2\nline3\nline4\nline5"},
{"line1\nline2\nline3\nline4\nline5", 3, "line3\nline4\nline5"},
{"", 3, ""},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
// Determine the expected line ending based on the operating system
lineEnding := "\n"
if runtime.GOOS == "windows" {
lineEnding = "\r\n"
}
// Replace expected line endings with the appropriate ones for the OS
expected := strings.ReplaceAll(tt.expected, "\n", lineEnding)
result := tail(tt.input, tt.n)
if result != expected {
t.Errorf("tail(%d) = %q; want %q", tt.n, result, expected)
}
})
}
}