peco.peco/config_test.go
2014-06-17 10:51:52 +09:00

122 lines
2.7 KiB
Go

package peco
import (
"encoding/json"
"path/filepath"
"fmt"
"io/ioutil"
"os"
"os/user"
"strings"
"testing"
"github.com/nsf/termbox-go"
)
func TestReadRC(t *testing.T) {
txt := `
{
"Keymap": {
"C-j": "peco.Finish"
},
"Style": {
"Basic": ["on_default", "default"],
"Selected": ["underline", "on_cyan", "black"],
"Query": ["yellow", "bold"]
}
}
`
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{"blink", "on_red", "white"},
style: &Style{fg: termbox.ColorWhite | termbox.AttrReverse, bg: termbox.ColorRed},
},
}
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)
}
currentUser = func() (*user.User, error) {
return &user.User{
HomeDir: 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("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()
}