From 37a7eda095280d1cc6b83e50cb14884ff7a72e4e Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Mon, 12 Sep 2016 19:19:47 +0300 Subject: [PATCH] add missing files --- eval_test.go | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++ parse_test.go | 23 ++++++ 2 files changed, 224 insertions(+) create mode 100644 eval_test.go create mode 100644 parse_test.go diff --git a/eval_test.go b/eval_test.go new file mode 100644 index 0000000..b6c1997 --- /dev/null +++ b/eval_test.go @@ -0,0 +1,201 @@ +package main + +// These inputs are used in scan and parse tests. + +var gTests = []struct { + inp string + toks []string + exprs []Expr +}{ + { + "", + []string{}, + nil, + }, + + { + "# comments start with '#'", + []string{}, + nil, + }, + + { + "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", ""}}, + }, + + { + "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"}}}}, + }, + + { + ":set ratios 1:2:3;", + []string{":", "set", "ratios", "1:2:3", ";", "\n"}, + []Expr{&ListExpr{[]Expr{&SetExpr{"ratios", "1:2:3"}}}}, + }, + + { + "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"}}}, + }, + + { + "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}}}}}, + }, + + { + "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{"/"}}}}, + }, + + { + `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" + `}}}, + }, +} diff --git a/parse_test.go b/parse_test.go new file mode 100644 index 0000000..0efa91d --- /dev/null +++ b/parse_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "reflect" + "strings" + "testing" +) + +func TestParse(t *testing.T) { + for _, test := range gTests { + p := newParser(strings.NewReader(test.inp)) + + for _, expr := range test.exprs { + if p.parse(); !reflect.DeepEqual(p.expr, expr) { + t.Errorf("at input '%s' expected '%s' but parsed '%s'", test.inp, expr, p.expr) + } + } + + if p.parse(); p.expr != nil { + t.Errorf("at input '%s' unexpected '%s'", test.inp, p.expr) + } + } +}