mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Add print tests
This commit is contained in:
+33
-5
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user