Add print tests

This commit is contained in:
Anton Medvedev
2018-08-13 09:09:05 +03:00
parent 3092ecff8d
commit 577c4e9113
+33 -5
View File
@@ -2,6 +2,7 @@ package expr
import (
"fmt"
"reflect"
"testing"
)
@@ -28,7 +29,7 @@ var printTests = []printTest{
"call([1, not true].foo)",
},
{
builtinNode{"len", []Node{identifierNode{"array"}}},
builtinNode{"len", []Node{nameNode{"array"}}},
"len(array)",
},
{
@@ -43,10 +44,6 @@ var printTests = []printTest{
binaryNode{"and", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
"((a or b) and c)",
},
{
matchesNode{left: nameNode{"foo"}, right: textNode{"foobar"}},
"(foo matches \"foobar\")",
},
{
conditionalNode{nameNode{"a"}, nameNode{"a"}, nameNode{"b"}},
"a ? a : b",
@@ -59,5 +56,36 @@ func TestPrint(t *testing.T) {
if actual != test.expected {
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", test.expected, actual, test.expected)
}
// Parse again and check if ast same as before.
ast, err := Parse(actual)
if err != nil {
t.Errorf("%s: can't parse printed expression", actual)
}
if !reflect.DeepEqual(ast, test.input) {
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", test.expected, ast, test.input)
}
}
}
func TestPrint_matches(t *testing.T) {
input := matchesNode{left: nameNode{"foo"}, right: textNode{"foobar"}}
expected := "(foo matches \"foobar\")"
actual := fmt.Sprintf("%v", input)
if actual != expected {
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", expected, actual, expected)
}
// Parse again and check if ast same as before.
ast, err := Parse(actual)
if err != nil {
t.Errorf("%s: can't parse printed expression", actual)
}
// Clear parsed regexp.
m := ast.(matchesNode)
m.r = nil
if !reflect.DeepEqual(m, input) {
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", expected, m, input)
}
}