This commit is contained in:
Gokcehan 2016-09-02 23:40:13 +03:00
parent f81146ae1e
commit f304f863b9

View File

@ -5,82 +5,59 @@ import (
"testing" "testing"
) )
// TODO: use a slice and merge with outputs 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"}},
var inp0 = "" // multiline commands
var inp1 = "# comments start with '#'" {"cmd gohome :{{\n\tcd ~\n\tset hidden\n}}",
var inp2 = "set hidden # trailing comments are allowed" []string{"cmd", "gohome", ":", "{{",
var inp3 = "set hidden; set preview" "cd", "~", "\n",
var inp4 = "set ratios 1:2:3" "set", "hidden", "\n",
var inp5 = "set ratios 1:2:3;" "}}", "\n"}},
var inp6 = ":set ratios 1:2:3"
var inp7 = ":set ratios 1:2:3;"
var inp8 = "map gh cd ~"
var inp9 = "map gh cd ~;"
var inp10 = "map gh :cd ~"
var inp11 = "map gh :cd ~;"
var inp12 = "cmd usage $du -h . | less"
var inp13 = "map u usage"
var inp14 = "map u usage;"
var inp15 = "map u :usage"
var inp16 = "map u :usage;"
var inp17 = "map u $du -h . | less"
var inp18 = `cmd usage $du -h "$1" | less`
var inp19 = "map u usage /"
var inp20 = `cmd gohome :{{ {"map gh :{{\n\tcd ~\n\tset hidden\n}}",
cd ~ []string{"map", "gh", ":", "{{",
set hidden "cd", "~", "\n",
}}` "set", "hidden", "\n",
"}}", "\n"}},
var inp21 = `map gh :{{ {"map c ${{\n\tmkdir foo\n\tIFS=':'; cp ${fs} foo\n\ttar -czvf \"foo.tar.gz\" foo\n\trm -rf foo\n}}",
cd ~ []string{"map", "c", "$", "{{",
set hidden "\n\tmkdir foo\n\tIFS=':'; cp ${fs} foo\n\ttar -czvf \"foo.tar.gz\" foo\n\trm -rf foo\n",
}}` "}}", "\n"}},
var inp22 = `map c ${{ {"cmd compress ${{\n\tmkdir \"$1\"\n\tIFS=':'; cp ${fs} \"$1\"\n\ttar -czvf \"$1.tar.gz\" \"$1\"\n\trm -rf \"$1\"\n}}",
mkdir foo []string{"cmd", "compress", "$", "{{",
IFS=':'; cp ${fs} foo "\n\tmkdir \"$1\"\n\tIFS=':'; cp ${fs} \"$1\"\n\ttar -czvf \"$1.tar.gz\" \"$1\"\n\trm -rf \"$1\"\n",
tar -czvf "foo.tar.gz" foo "}}", "\n"}},
rm -rf foo
}}`
var inp23 = `cmd compress ${{ // unfinished command
mkdir "$1" {"cmd compress ${{\n\tmkdir \"$1\"",
IFS=':'; cp ${fs} "$1" []string{"cmd", "compress", "$", "{{"}},
tar -czvf "$1.tar.gz" "$1" }
rm -rf "$1"
}}`
// unfinished command
var inp24 = `cmd compress ${{
mkdir "$1"`
var out0 = []string{}
var out1 = []string{}
var out2 = []string{"set", "hidden", "\n"}
var out3 = []string{"set", "hidden", ";", "set", "preview", "\n"}
var out4 = []string{"set", "ratios", "1:2:3", "\n"}
var out5 = []string{"set", "ratios", "1:2:3", ";"}
var out6 = []string{":", "set", "ratios", "1:2:3", "\n", "\n"}
var out7 = []string{":", "set", "ratios", "1:2:3", ";", "\n"}
var out8 = []string{"map", "gh", "cd", "~", "\n"}
var out9 = []string{"map", "gh", "cd", "~", ";"}
var out10 = []string{"map", "gh", ":", "cd", "~", "\n", "\n"}
var out11 = []string{"map", "gh", ":", "cd", "~", ";", "\n"}
var out12 = []string{"cmd", "usage", "$", "du -h . | less", "\n"}
var out13 = []string{"map", "u", "usage", "\n"}
var out14 = []string{"map", "u", "usage", ";"}
var out15 = []string{"map", "u", ":", "usage", "\n", "\n"}
var out16 = []string{"map", "u", ":", "usage", ";", "\n"}
var out17 = []string{"map", "u", "$", "du -h . | less", "\n"}
var out18 = []string{"cmd", "usage", "$", `du -h "$1" | less`, "\n"}
var out19 = []string{"map", "u", "usage", "/", "\n"}
var out20 = []string{"cmd", "gohome", ":", "{{", "cd", "~", "\n", "set", "hidden", "\n", "}}", "\n"}
var out21 = []string{"map", "gh", ":", "{{", "cd", "~", "\n", "set", "hidden", "\n", "}}", "\n"}
var out22 = []string{"map", "c", "$", "{{", "\n\tmkdir foo\n\tIFS=':'; cp ${fs} foo\n\ttar -czvf \"foo.tar.gz\" foo\n\trm -rf foo\n", "}}", "\n"}
var out23 = []string{"cmd", "compress", "$", "{{", "\n\tmkdir \"$1\"\n\tIFS=':'; cp ${fs} \"$1\"\n\ttar -czvf \"$1.tar.gz\" \"$1\"\n\trm -rf \"$1\"\n", "}}", "\n"}
var out24 = []string{"cmd", "compress", "$", "{{"}
func compare(t *testing.T, inp string, out []string) { func compare(t *testing.T, inp string, out []string) {
s := newScanner(strings.NewReader(inp)) s := newScanner(strings.NewReader(inp))
@ -97,29 +74,7 @@ func compare(t *testing.T, inp string, out []string) {
} }
func TestScan(t *testing.T) { func TestScan(t *testing.T) {
compare(t, inp0, out0) for _, inp := range inps {
compare(t, inp1, out1) compare(t, inp.s, inp.toks)
compare(t, inp2, out2) }
compare(t, inp3, out3)
compare(t, inp4, out4)
compare(t, inp5, out5)
compare(t, inp6, out6)
compare(t, inp7, out7)
compare(t, inp8, out8)
compare(t, inp9, out9)
compare(t, inp10, out10)
compare(t, inp11, out11)
compare(t, inp12, out12)
compare(t, inp13, out13)
compare(t, inp14, out14)
compare(t, inp15, out15)
compare(t, inp16, out16)
compare(t, inp17, out17)
compare(t, inp18, out18)
compare(t, inp19, out19)
compare(t, inp20, out20)
compare(t, inp21, out21)
compare(t, inp22, out22)
compare(t, inp23, out23)
compare(t, inp24, out24)
} }