Debian Codegraph @

src/history_test.go

raw
1 package fzf
2
3 import (
4 "os"
5 "runtime"
6 "testing"
7 )
8
9 func TestHistory(t *testing.T) {
10 maxHistory := 50
11
12 // Invalid arguments
13 var paths []string
14 if runtime.GOOS == "windows" {
15 // GOPATH should exist, so we shouldn't be able to override it
16 paths = []string{os.Getenv("GOPATH")}
17 } else {
18 paths = []string{"/etc", "/proc"}
19 }
20
21 for _, path := range paths {
22 if _, e := NewHistory(path, maxHistory); e == nil {
23 t.Error("Error expected for: " + path)
24 }
25 }
26
27 f, _ := os.CreateTemp("", "fzf-history")
28 f.Close()
29
30 { // Append lines
31 h, _ := NewHistory(f.Name(), maxHistory)
32 for i := 0; i < maxHistory+10; i++ {
33 h.append("foobar")
34 }
35 }
36 { // Read lines
37 h, _ := NewHistory(f.Name(), maxHistory)
38 if len(h.lines) != maxHistory+1 {
39 t.Errorf("Expected: %d, actual: %d\n", maxHistory+1, len(h.lines))
40 }
41 for i := range maxHistory {
42 if h.lines[i] != "foobar" {
43 t.Error("Expected: foobar, actual: " + h.lines[i])
44 }
45 }
46 }
47 { // Append lines
48 h, _ := NewHistory(f.Name(), maxHistory)
49 h.append("barfoo")
50 h.append("")
51 h.append("foobarbaz")
52 }
53 { // Read lines again
54 h, _ := NewHistory(f.Name(), maxHistory)
55 if len(h.lines) != maxHistory+1 {
56 t.Errorf("Expected: %d, actual: %d\n", maxHistory+1, len(h.lines))
57 }
58 compare := func(idx int, exp string) {
59 if h.lines[idx] != exp {
60 t.Errorf("Expected: %s, actual: %s\n", exp, h.lines[idx])
61 }
62 }
63 compare(maxHistory-3, "foobar")
64 compare(maxHistory-2, "barfoo")
65 compare(maxHistory-1, "foobarbaz")
66 }
67 }