-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse_test.go
76 lines (66 loc) · 1.48 KB
/
reverse_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// reverse_test.go - read a file line-by-line backwards
package rfile
import (
"io"
"os"
"reflect"
"testing"
)
func TestRfile(t *testing.T) {
tests := map[string]struct {
content string
wantLines []string
}{
"empty file": {
content: "",
wantLines: nil,
},
"one-line file": {
content: "1st line",
wantLines: []string{"1st line"},
},
"multi-line file": {
content: "1st line\n2nd line\n",
wantLines: []string{"2nd line", "1st line"},
},
"multi-line file with empty lines": {
content: "1st line\n2nd line\n3rd line\n\n4th line\n\n",
wantLines: []string{"", "4th line", "", "3rd line", "2nd line", "1st line"},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
filename := createTempFile(t, test.content)
defer os.Remove(filename)
rf, err := Open(filename)
if err != nil {
t.Fatal(err)
}
defer rf.Close()
var lines []string
for {
line, err := rf.ReadLine()
if err != nil {
if err != io.EOF {
t.Fatal(err)
}
break
}
lines = append(lines, line)
}
if !reflect.DeepEqual(test.wantLines, lines) {
t.Errorf("not equal: want '%v' got '%v'", test.wantLines, lines)
}
})
}
}
func createTempFile(t *testing.T, content string) string {
t.Helper()
file, err := os.CreateTemp("", "rfile-test-ReadLine")
if err != nil {
t.Fatalf("create a temp file: %v", err)
}
defer func() { _ = file.Close() }()
_, _ = file.WriteString(content)
return file.Name()
}