-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory_test.go
More file actions
77 lines (72 loc) · 1.56 KB
/
history_test.go
File metadata and controls
77 lines (72 loc) · 1.56 KB
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
77
package startprompt
import (
"os"
"testing"
)
func TestMemHistory(t *testing.T) {
mem := NewMemHistory()
if len(mem.GetAll()) != 0 {
t.Fatalf("want=0, but got=%d", len(mem.GetAll()))
}
tests := []string{
"hello world",
"hello world2",
"hello\nworld",
"hello\nworld2",
}
for i, want := range tests {
mem.Append(want)
gots := mem.GetAll()
if len(gots) != i+1 {
t.Fatalf("want=%d, but got=%d", i+1, len(gots))
}
if gots[i] != want {
t.Fatalf("want=%q, but got=%q", want, gots[i])
}
for j := 0; j < i; j++ {
if gots[j] != tests[j] {
t.Fatalf("want=%q, but got=%q", tests[j], gots[j])
}
}
}
}
func TestFileHistory(t *testing.T) {
tempFile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatalf("CreateTemp error: %v", err)
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Errorf("Error remove file: %v", err)
}
}(tempFile.Name())
history := NewFileHistory(tempFile.Name())
//history := NewFileHistory("example")
if len(history.GetAll()) != 0 {
t.Fatalf("want=0, but got=%d", len(history.GetAll()))
}
tests := []string{
"hello world",
"hello world2",
"hello\nworld",
"hello\nworld2",
"hello\nworld\n",
"hello\nworld\n3",
}
for i, want := range tests {
history.Append(want)
gots := history.GetAll()
if len(gots) != i+1 {
t.Fatalf("want=%d, but got=%d", i+1, len(gots))
}
if gots[i] != want {
t.Fatalf("want=%q, but got=%q", want, gots[i])
}
for j := 0; j < i; j++ {
if gots[j] != tests[j] {
t.Fatalf("want=%q, but got=%q", tests[j], gots[j])
}
}
}
}