add parse tests

This commit is contained in:
Gokcehan 2016-09-12 19:18:13 +03:00
parent ee0f931db8
commit 1fcd70afa0
2 changed files with 10 additions and 71 deletions

View File

@ -31,7 +31,6 @@ package main
import (
"fmt"
"io"
"log"
)
type Expr interface {
@ -229,8 +228,6 @@ func (p *Parser) parseExpr() Expr {
// TODO: handle error
}
log.Println("parsed:", result)
return result
}

View File

@ -5,76 +5,18 @@ import (
"testing"
)
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"}},
func TestScan(t *testing.T) {
for _, test := range gTests {
s := newScanner(strings.NewReader(test.inp))
// multiline commands
{"cmd gohome :{{\n\tcd ~\n\tset hidden\n}}",
[]string{"cmd", "gohome", ":", "{{",
"cd", "~", "\n",
"set", "hidden", "\n",
"}}", "\n"}},
{"map gh :{{\n\tcd ~\n\tset hidden\n}}",
[]string{"map", "gh", ":", "{{",
"cd", "~", "\n",
"set", "hidden", "\n",
"}}", "\n"}},
{"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"}},
{"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"}},
// unfinished command
{"cmd compress ${{\n\tmkdir \"$1\"",
[]string{"cmd", "compress", "$", "{{"}},
}
func compare(t *testing.T, inp string, out []string) {
s := newScanner(strings.NewReader(inp))
for _, tok := range out {
for _, tok := range test.toks {
if s.scan(); s.tok != tok {
t.Errorf("at input '%s' expected '%s' but scanned '%s'", inp, tok, s.tok)
t.Errorf("at input '%s' expected '%s' but scanned '%s'", test.inp, tok, s.tok)
}
}
if s.scan() {
t.Errorf("at input '%s' unexpected '%s'", inp, s.tok)
}
}
func TestScan(t *testing.T) {
for _, inp := range inps {
compare(t, inp.s, inp.toks)
t.Errorf("at input '%s' unexpected '%s'", test.inp, s.tok)
}
}
}