-
Notifications
You must be signed in to change notification settings - Fork 0
/
head_test.go
40 lines (35 loc) · 1.01 KB
/
head_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 TestHead(t *testing.T) {
tests := []struct {
input string
n int
expected string
}{
{"line1\nline2\nline3\nline4\nline5\n", 2, "line1\nline2"},
{"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, "line1\nline2\nline3"},
{"", 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 := head(tt.input, tt.n)
if result != expected {
t.Errorf("head(%d) = %q; want %q", tt.n, result, expected)
}
})
}
}