lf/scan_test.go

81 lines
2.8 KiB
Go
Raw Normal View History

2016-08-13 12:49:04 +00:00
package main
import (
"strings"
"testing"
)
2016-09-02 20:40:13 +00:00
var inps = []struct {
s string
toks []string
}{
// single line commands
{"", []string{}},
{"# comments start with '#'", []string{}},
{"set hidden # trailing comments are allowed", []string{"set", "hidden", "\n"}},
{"set hidden; set preview", []string{"set", "hidden", ";", "set", "preview", "\n"}},
{"set ratios 1:2:3", []string{"set", "ratios", "1:2:3", "\n"}},
{"set ratios 1:2:3;", []string{"set", "ratios", "1:2:3", ";"}},
{":set ratios 1:2:3", []string{":", "set", "ratios", "1:2:3", "\n", "\n"}},
{":set ratios 1:2:3;", []string{":", "set", "ratios", "1:2:3", ";", "\n"}},
{"map gh cd ~", []string{"map", "gh", "cd", "~", "\n"}},
{"map gh cd ~;", []string{"map", "gh", "cd", "~", ";"}},
{"map gh :cd ~", []string{"map", "gh", ":", "cd", "~", "\n", "\n"}},
{"map gh :cd ~;", []string{"map", "gh", ":", "cd", "~", ";", "\n"}},
{"cmd usage $du -h . | less", []string{"cmd", "usage", "$", "du -h . | less", "\n"}},
{"map u usage", []string{"map", "u", "usage", "\n"}},
{"map u usage;", []string{"map", "u", "usage", ";"}},
{"map u :usage", []string{"map", "u", ":", "usage", "\n", "\n"}},
{"map u :usage;", []string{"map", "u", ":", "usage", ";", "\n"}},
{"map u $du -h . | less", []string{"map", "u", "$", "du -h . | less", "\n"}},
{"cmd usage $du -h \"$1\" | less", []string{"cmd", "usage", "$", `du -h "$1" | less`, "\n"}},
{"map u usage /", []string{"map", "u", "usage", "/", "\n"}},
2016-08-13 12:49:04 +00:00
2016-09-02 20:40:13 +00:00
// multiline commands
{"cmd gohome :{{\n\tcd ~\n\tset hidden\n}}",
[]string{"cmd", "gohome", ":", "{{",
"cd", "~", "\n",
"set", "hidden", "\n",
"}}", "\n"}},
2016-08-13 12:49:04 +00:00
2016-09-02 20:40:13 +00:00
{"map gh :{{\n\tcd ~\n\tset hidden\n}}",
[]string{"map", "gh", ":", "{{",
"cd", "~", "\n",
"set", "hidden", "\n",
"}}", "\n"}},
2016-08-13 12:49:04 +00:00
2016-09-02 20:40:13 +00:00
{"map c ${{\n\tmkdir foo\n\tIFS=':'; cp ${fs} foo\n\ttar -czvf \"foo.tar.gz\" foo\n\trm -rf foo\n}}",
[]string{"map", "c", "$", "{{",
"\n\tmkdir foo\n\tIFS=':'; cp ${fs} foo\n\ttar -czvf \"foo.tar.gz\" foo\n\trm -rf foo\n",
"}}", "\n"}},
2016-08-13 12:49:04 +00:00
2016-09-02 20:40:13 +00:00
{"cmd compress ${{\n\tmkdir \"$1\"\n\tIFS=':'; cp ${fs} \"$1\"\n\ttar -czvf \"$1.tar.gz\" \"$1\"\n\trm -rf \"$1\"\n}}",
[]string{"cmd", "compress", "$", "{{",
"\n\tmkdir \"$1\"\n\tIFS=':'; cp ${fs} \"$1\"\n\ttar -czvf \"$1.tar.gz\" \"$1\"\n\trm -rf \"$1\"\n",
"}}", "\n"}},
2016-08-13 12:49:04 +00:00
2016-09-02 20:40:13 +00:00
// unfinished command
{"cmd compress ${{\n\tmkdir \"$1\"",
[]string{"cmd", "compress", "$", "{{"}},
}
2016-08-13 12:49:04 +00:00
func compare(t *testing.T, inp string, out []string) {
s := newScanner(strings.NewReader(inp))
for _, tok := range out {
if s.scan(); s.tok != tok {
t.Errorf("at input '%s' expected '%s' but scanned '%s'", inp, tok, s.tok)
}
}
if s.scan() {
t.Errorf("at input '%s' unexpected '%s'", inp, s.tok)
}
}
func TestScan(t *testing.T) {
2016-09-02 20:40:13 +00:00
for _, inp := range inps {
compare(t, inp.s, inp.toks)
}
2016-08-13 12:49:04 +00:00
}