forked from peco/peco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
124 lines (110 loc) · 2.89 KB
/
config_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package peco
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/nsf/termbox-go"
)
func TestReadRC(t *testing.T) {
txt := `
{
"Keymap": {
"C-j": "peco.Finish",
"C-x,C-c": "peco.Finish"
},
"Style": {
"Basic": ["on_default", "default"],
"Selected": ["underline", "on_cyan", "black"],
"Query": ["yellow", "bold"],
"Matched": ["cyan", "bold", "on_red"]
},
"Prompt": "[peco]"
}
`
cfg := NewConfig()
if err := json.Unmarshal([]byte(txt), cfg); err != nil {
t.Fatalf("Error unmarshaling json: %s", err)
}
t.Logf("%#q", cfg)
}
type stringsToStyleTest struct {
strings []string
style *Style
}
func TestStringsToStyle(t *testing.T) {
tests := []stringsToStyleTest{
stringsToStyleTest{
strings: []string{"on_default", "default"},
style: &Style{fg: termbox.ColorDefault, bg: termbox.ColorDefault},
},
stringsToStyleTest{
strings: []string{"bold", "on_blue", "yellow"},
style: &Style{fg: termbox.ColorYellow | termbox.AttrBold, bg: termbox.ColorBlue},
},
stringsToStyleTest{
strings: []string{"underline", "on_cyan", "black"},
style: &Style{fg: termbox.ColorBlack | termbox.AttrUnderline, bg: termbox.ColorCyan},
},
stringsToStyleTest{
strings: []string{"reverse", "on_red", "white"},
style: &Style{fg: termbox.ColorWhite | termbox.AttrReverse, bg: termbox.ColorRed},
},
stringsToStyleTest{
strings: []string{"on_bold", "on_magenta", "green"},
style: &Style{fg: termbox.ColorGreen, bg: termbox.ColorMagenta | termbox.AttrBold},
},
}
t.Logf("Checking strings -> color mapping...")
for _, test := range tests {
t.Logf(" checking %s...", test.strings)
if a := stringsToStyle(test.strings); *a != *test.style {
t.Errorf("Expected '%s' to be '%#v', but got '%#v'", test.strings, test.style, a)
}
}
}
func TestLocateRcfile(t *testing.T) {
dir, err := ioutil.TempDir("", "peco-")
if err != nil {
t.Fatalf("Failed to create temporary directory: %s", err)
}
homedirFunc = func() (string, error) {
return dir, nil
}
expected := []string{
filepath.Join(dir, "peco"),
filepath.Join(dir, "1", "peco"),
filepath.Join(dir, "2", "peco"),
filepath.Join(dir, "3", "peco"),
filepath.Join(dir, ".peco"),
}
i := 0
_locateRcfileIn = func(dir string) (string, error) {
t.Logf("looking for file in %s", dir)
if i > len(expected)-1 {
t.Fatalf("Got %d directories, only have %d", i+1, len(expected))
}
if expected[i] != dir {
t.Errorf("Expected %s, got %s", expected[i], dir)
}
i++
return "", fmt.Errorf("error: Not found")
}
os.Setenv("XDG_CONFIG_HOME", dir)
os.Setenv("XDG_CONFIG_DIRS", strings.Join(
[]string{
filepath.Join(dir, "1"),
filepath.Join(dir, "2"),
filepath.Join(dir, "3"),
},
fmt.Sprintf("%c", filepath.ListSeparator),
))
LocateRcfile()
expected[0] = filepath.Join(dir, ".config", "peco")
os.Setenv("XDG_CONFIG_HOME", "")
i = 0
LocateRcfile()
}