From f304f863b978c5075f3eba7531d7e816442aed81 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Fri, 2 Sep 2016 23:40:13 +0300 Subject: [PATCH] cleanup --- scan_test.go | 147 ++++++++++++++++++--------------------------------- 1 file changed, 51 insertions(+), 96 deletions(-) diff --git a/scan_test.go b/scan_test.go index 859ecbc..de9c30e 100644 --- a/scan_test.go +++ b/scan_test.go @@ -5,82 +5,59 @@ import ( "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 = "" -var inp1 = "# comments start with '#'" -var inp2 = "set hidden # trailing comments are allowed" -var inp3 = "set hidden; set preview" -var inp4 = "set ratios 1:2:3" -var inp5 = "set ratios 1:2:3;" -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 /" + // multiline commands + {"cmd gohome :{{\n\tcd ~\n\tset hidden\n}}", + []string{"cmd", "gohome", ":", "{{", + "cd", "~", "\n", + "set", "hidden", "\n", + "}}", "\n"}}, -var inp20 = `cmd gohome :{{ - cd ~ - set hidden -}}` + {"map gh :{{\n\tcd ~\n\tset hidden\n}}", + []string{"map", "gh", ":", "{{", + "cd", "~", "\n", + "set", "hidden", "\n", + "}}", "\n"}}, -var inp21 = `map gh :{{ - cd ~ - set hidden -}}` + {"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"}}, -var inp22 = `map c ${{ - mkdir foo - IFS=':'; cp ${fs} foo - tar -czvf "foo.tar.gz" foo - rm -rf foo -}}` + {"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"}}, -var inp23 = `cmd compress ${{ - mkdir "$1" - IFS=':'; cp ${fs} "$1" - 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", "$", "{{"} + // unfinished command + {"cmd compress ${{\n\tmkdir \"$1\"", + []string{"cmd", "compress", "$", "{{"}}, +} func compare(t *testing.T, inp string, out []string) { s := newScanner(strings.NewReader(inp)) @@ -97,29 +74,7 @@ func compare(t *testing.T, inp string, out []string) { } func TestScan(t *testing.T) { - compare(t, inp0, out0) - compare(t, inp1, out1) - 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) + for _, inp := range inps { + compare(t, inp.s, inp.toks) + } }