2016-09-12 16:19:47 +00:00
|
|
|
package main
|
|
|
|
|
2016-09-18 16:21:24 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
2016-09-12 16:19:47 +00:00
|
|
|
|
2016-09-18 16:21:24 +00:00
|
|
|
// These inputs are used in scan and parse tests.
|
2016-09-12 16:19:47 +00:00
|
|
|
var gTests = []struct {
|
|
|
|
inp string
|
|
|
|
toks []string
|
|
|
|
exprs []Expr
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
[]string{},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"# comments start with '#'",
|
|
|
|
[]string{},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
2016-11-22 18:31:20 +00:00
|
|
|
{
|
|
|
|
"echo hello",
|
|
|
|
[]string{"echo", "hello", "\n"},
|
|
|
|
[]Expr{&CallExpr{"echo", []string{"hello"}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"echo hello world",
|
|
|
|
[]string{"echo", "hello", "world", "\n"},
|
|
|
|
[]Expr{&CallExpr{"echo", []string{"hello", "world"}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"echo 'hello world'",
|
|
|
|
[]string{"echo", "hello world", "\n"},
|
|
|
|
[]Expr{&CallExpr{"echo", []string{"hello world"}}},
|
|
|
|
},
|
|
|
|
|
2016-09-12 16:19:47 +00:00
|
|
|
{
|
|
|
|
"set hidden # trailing comments are allowed",
|
|
|
|
[]string{"set", "hidden", "\n"},
|
|
|
|
[]Expr{&SetExpr{"hidden", ""}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"set hidden; set preview",
|
|
|
|
[]string{"set", "hidden", ";", "set", "preview", "\n"},
|
|
|
|
[]Expr{&SetExpr{"hidden", ""}, &SetExpr{"preview", ""}},
|
|
|
|
},
|
|
|
|
|
2016-10-15 19:53:07 +00:00
|
|
|
{
|
|
|
|
"set hidden\nset preview",
|
|
|
|
[]string{"set", "hidden", "\n", "set", "preview", "\n"},
|
|
|
|
[]Expr{&SetExpr{"hidden", ""}, &SetExpr{"preview", ""}},
|
|
|
|
},
|
|
|
|
|
2016-09-12 16:19:47 +00:00
|
|
|
{
|
|
|
|
"set ratios 1:2:3",
|
|
|
|
[]string{"set", "ratios", "1:2:3", "\n"},
|
|
|
|
[]Expr{&SetExpr{"ratios", "1:2:3"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"set ratios 1:2:3;",
|
|
|
|
[]string{"set", "ratios", "1:2:3", ";"},
|
|
|
|
[]Expr{&SetExpr{"ratios", "1:2:3"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
":set ratios 1:2:3",
|
|
|
|
[]string{":", "set", "ratios", "1:2:3", "\n", "\n"},
|
|
|
|
[]Expr{&ListExpr{[]Expr{&SetExpr{"ratios", "1:2:3"}}}},
|
|
|
|
},
|
|
|
|
|
2016-10-15 19:53:07 +00:00
|
|
|
{
|
|
|
|
":set ratios 1:2:3\nset hidden",
|
|
|
|
[]string{":", "set", "ratios", "1:2:3", "\n", "\n", "set", "hidden", "\n"},
|
|
|
|
[]Expr{&ListExpr{[]Expr{&SetExpr{"ratios", "1:2:3"}}}, &SetExpr{"hidden", ""}},
|
|
|
|
},
|
|
|
|
|
2016-09-12 16:19:47 +00:00
|
|
|
{
|
|
|
|
":set ratios 1:2:3;",
|
|
|
|
[]string{":", "set", "ratios", "1:2:3", ";", "\n"},
|
|
|
|
[]Expr{&ListExpr{[]Expr{&SetExpr{"ratios", "1:2:3"}}}},
|
|
|
|
},
|
|
|
|
|
2016-10-15 19:53:07 +00:00
|
|
|
{
|
|
|
|
":set ratios 1:2:3;\nset hidden",
|
|
|
|
[]string{":", "set", "ratios", "1:2:3", ";", "\n", "set", "hidden", "\n"},
|
|
|
|
[]Expr{&ListExpr{[]Expr{&SetExpr{"ratios", "1:2:3"}}}, &SetExpr{"hidden", ""}},
|
|
|
|
},
|
|
|
|
|
2016-09-12 16:19:47 +00:00
|
|
|
{
|
|
|
|
"map gh cd ~",
|
|
|
|
[]string{"map", "gh", "cd", "~", "\n"},
|
|
|
|
[]Expr{&MapExpr{"gh", &CallExpr{"cd", []string{"~"}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map gh cd ~;",
|
|
|
|
[]string{"map", "gh", "cd", "~", ";"},
|
|
|
|
[]Expr{&MapExpr{"gh", &CallExpr{"cd", []string{"~"}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map gh :cd ~",
|
|
|
|
[]string{"map", "gh", ":", "cd", "~", "\n", "\n"},
|
|
|
|
[]Expr{&MapExpr{"gh", &ListExpr{[]Expr{&CallExpr{"cd", []string{"~"}}}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map gh :cd ~;",
|
|
|
|
[]string{"map", "gh", ":", "cd", "~", ";", "\n"},
|
|
|
|
[]Expr{&MapExpr{"gh", &ListExpr{[]Expr{&CallExpr{"cd", []string{"~"}}}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"cmd usage $du -h . | less",
|
|
|
|
[]string{"cmd", "usage", "$", "du -h . | less", "\n"},
|
|
|
|
[]Expr{&CmdExpr{"usage", &ExecExpr{"$", "du -h . | less"}}},
|
|
|
|
},
|
|
|
|
|
2016-11-18 19:45:49 +00:00
|
|
|
{
|
|
|
|
"cmd 世界 $echo 世界",
|
|
|
|
[]string{"cmd", "世界", "$", "echo 世界", "\n"},
|
|
|
|
[]Expr{&CmdExpr{"世界", &ExecExpr{"$", "echo 世界"}}},
|
|
|
|
},
|
|
|
|
|
2016-09-12 16:19:47 +00:00
|
|
|
{
|
|
|
|
"map u usage",
|
|
|
|
[]string{"map", "u", "usage", "\n"},
|
|
|
|
[]Expr{&MapExpr{"u", &CallExpr{"usage", nil}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map u usage;",
|
|
|
|
[]string{"map", "u", "usage", ";"},
|
|
|
|
[]Expr{&MapExpr{"u", &CallExpr{"usage", nil}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map u :usage",
|
|
|
|
[]string{"map", "u", ":", "usage", "\n", "\n"},
|
|
|
|
[]Expr{&MapExpr{"u", &ListExpr{[]Expr{&CallExpr{"usage", nil}}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map u :usage;",
|
|
|
|
[]string{"map", "u", ":", "usage", ";", "\n"},
|
|
|
|
[]Expr{&MapExpr{"u", &ListExpr{[]Expr{&CallExpr{"usage", nil}}}}},
|
|
|
|
},
|
|
|
|
|
2016-10-15 18:46:31 +00:00
|
|
|
{
|
|
|
|
"map r push :rename<space>",
|
|
|
|
[]string{"map", "r", "push", ":rename<space>", "\n"},
|
|
|
|
[]Expr{&MapExpr{"r", &CallExpr{"push", []string{":rename<space>"}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map r push :rename<space>;",
|
|
|
|
[]string{"map", "r", "push", ":rename<space>;", "\n"},
|
|
|
|
[]Expr{&MapExpr{"r", &CallExpr{"push", []string{":rename<space>;"}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2016-10-15 19:53:07 +00:00
|
|
|
"map r push :rename<space> # trailing comments are allowed after a space",
|
2016-10-15 18:46:31 +00:00
|
|
|
[]string{"map", "r", "push", ":rename<space>", "\n"},
|
|
|
|
[]Expr{&MapExpr{"r", &CallExpr{"push", []string{":rename<space>"}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map r :push :rename<space>",
|
|
|
|
[]string{"map", "r", ":", "push", ":rename<space>", "\n", "\n"},
|
|
|
|
[]Expr{&MapExpr{"r", &ListExpr{[]Expr{&CallExpr{"push", []string{":rename<space>"}}}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map r :push :rename<space> ; set hidden",
|
|
|
|
[]string{"map", "r", ":", "push", ":rename<space>", ";", "set", "hidden", "\n", "\n"},
|
|
|
|
[]Expr{&MapExpr{"r", &ListExpr{[]Expr{&CallExpr{"push", []string{":rename<space>"}}, &SetExpr{"hidden", ""}}}}},
|
|
|
|
},
|
|
|
|
|
2016-09-12 16:19:47 +00:00
|
|
|
{
|
|
|
|
"map u $du -h . | less",
|
|
|
|
[]string{"map", "u", "$", "du -h . | less", "\n"},
|
|
|
|
[]Expr{&MapExpr{"u", &ExecExpr{"$", "du -h . | less"}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"cmd usage $du -h \"$1\" | less",
|
|
|
|
[]string{"cmd", "usage", "$", `du -h "$1" | less`, "\n"},
|
|
|
|
[]Expr{&CmdExpr{"usage", &ExecExpr{"$", `du -h "$1" | less`}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map u usage /",
|
|
|
|
[]string{"map", "u", "usage", "/", "\n"},
|
|
|
|
[]Expr{&MapExpr{"u", &CallExpr{"usage", []string{"/"}}}},
|
|
|
|
},
|
|
|
|
|
2016-10-15 12:56:53 +00:00
|
|
|
{
|
|
|
|
"map ss :set sortby size; set showinfo size",
|
|
|
|
[]string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "showinfo", "size", "\n", "\n"},
|
|
|
|
[]Expr{&MapExpr{"ss", &ListExpr{[]Expr{&SetExpr{"sortby", "size"}, &SetExpr{"showinfo", "size"}}}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"map ss :set sortby size; set showinfo size;",
|
|
|
|
[]string{"map", "ss", ":", "set", "sortby", "size", ";", "set", "showinfo", "size", ";", "\n"},
|
|
|
|
[]Expr{&MapExpr{"ss", &ListExpr{[]Expr{&SetExpr{"sortby", "size"}, &SetExpr{"showinfo", "size"}}}}},
|
|
|
|
},
|
|
|
|
|
2016-09-12 16:19:47 +00:00
|
|
|
{
|
|
|
|
`cmd gohome :{{
|
|
|
|
cd ~
|
|
|
|
set hidden
|
|
|
|
}}`,
|
|
|
|
[]string{"cmd", "gohome", ":", "{{",
|
|
|
|
"cd", "~", "\n",
|
|
|
|
"set", "hidden", "\n",
|
|
|
|
"}}", "\n"},
|
|
|
|
[]Expr{&CmdExpr{"gohome", &ListExpr{[]Expr{
|
|
|
|
&CallExpr{"cd", []string{"~"}},
|
|
|
|
&SetExpr{"hidden", ""}}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`map gh :{{
|
|
|
|
cd ~
|
|
|
|
set hidden
|
|
|
|
}}`,
|
|
|
|
[]string{"map", "gh", ":", "{{",
|
|
|
|
"cd", "~", "\n",
|
|
|
|
"set", "hidden", "\n",
|
|
|
|
"}}", "\n"},
|
|
|
|
[]Expr{&MapExpr{"gh", &ListExpr{[]Expr{
|
|
|
|
&CallExpr{"cd", []string{"~"}},
|
|
|
|
&SetExpr{"hidden", ""}}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`map c ${{
|
|
|
|
mkdir foo
|
|
|
|
IFS=':'; cp ${fs} foo
|
|
|
|
tar -czvf "foo.tar.gz" foo
|
|
|
|
rm -rf foo
|
|
|
|
}}`,
|
|
|
|
[]string{"map", "c", "$", "{{", `
|
|
|
|
mkdir foo
|
|
|
|
IFS=':'; cp ${fs} foo
|
|
|
|
tar -czvf "foo.tar.gz" foo
|
|
|
|
rm -rf foo
|
|
|
|
`, "}}", "\n"},
|
|
|
|
[]Expr{&MapExpr{"c", &ExecExpr{"$", `
|
|
|
|
mkdir foo
|
|
|
|
IFS=':'; cp ${fs} foo
|
|
|
|
tar -czvf "foo.tar.gz" foo
|
|
|
|
rm -rf foo
|
|
|
|
`}}},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`cmd compress ${{
|
|
|
|
mkdir "$1"
|
|
|
|
IFS=':'; cp ${fs} "$1"
|
|
|
|
tar -czvf "$1.tar.gz" "$1"
|
|
|
|
rm -rf "$1"
|
|
|
|
}}`,
|
|
|
|
[]string{"cmd", "compress", "$", "{{", `
|
|
|
|
mkdir "$1"
|
|
|
|
IFS=':'; cp ${fs} "$1"
|
|
|
|
tar -czvf "$1.tar.gz" "$1"
|
|
|
|
rm -rf "$1"
|
|
|
|
`, "}}", "\n"},
|
|
|
|
[]Expr{&CmdExpr{"compress", &ExecExpr{"$", `
|
|
|
|
mkdir "$1"
|
|
|
|
IFS=':'; cp ${fs} "$1"
|
|
|
|
tar -czvf "$1.tar.gz" "$1"
|
|
|
|
rm -rf "$1"
|
|
|
|
`}}},
|
|
|
|
},
|
|
|
|
}
|
2016-09-18 16:21:24 +00:00
|
|
|
|
|
|
|
func TestSplitKeys(t *testing.T) {
|
|
|
|
inps := []struct {
|
|
|
|
s string
|
|
|
|
keys []string
|
|
|
|
}{
|
|
|
|
{"", nil},
|
|
|
|
{"j", []string{"j"}},
|
|
|
|
{"jk", []string{"j", "k"}},
|
|
|
|
{"1j", []string{"1", "j"}},
|
|
|
|
{"42j", []string{"4", "2", "j"}},
|
|
|
|
{"<space>", []string{"<space>"}},
|
|
|
|
{"j<space>", []string{"j", "<space>"}},
|
|
|
|
{"j<space>k", []string{"j", "<space>", "k"}},
|
|
|
|
{"1j<space>k", []string{"1", "j", "<space>", "k"}},
|
|
|
|
{"1j<space>1k", []string{"1", "j", "<space>", "1", "k"}},
|
|
|
|
{"<>", []string{"<>"}},
|
|
|
|
{"<space", []string{"<space"}},
|
|
|
|
{"<space<", []string{"<space<"}},
|
|
|
|
{"<space<>", []string{"<space<>"}},
|
|
|
|
{"><space>", []string{">", "<space>"}},
|
|
|
|
{"><space>>", []string{">", "<space>", ">"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, inp := range inps {
|
|
|
|
if keys := splitKeys(inp.s); !reflect.DeepEqual(keys, inp.keys) {
|
|
|
|
t.Errorf("at input '%s' expected '%v' but got '%v'", inp.s, inp.keys, keys)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|