Skip to content

Commit c224b52

Browse files
committed
Add print tests
1 parent 653a058 commit c224b52

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

print_test.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package expr
22

33
import (
44
"fmt"
5+
"reflect"
56
"testing"
67
)
78

@@ -28,7 +29,7 @@ var printTests = []printTest{
2829
"call([1, not true].foo)",
2930
},
3031
{
31-
builtinNode{"len", []Node{identifierNode{"array"}}},
32+
builtinNode{"len", []Node{nameNode{"array"}}},
3233
"len(array)",
3334
},
3435
{
@@ -43,10 +44,6 @@ var printTests = []printTest{
4344
binaryNode{"and", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
4445
"((a or b) and c)",
4546
},
46-
{
47-
matchesNode{left: nameNode{"foo"}, right: textNode{"foobar"}},
48-
"(foo matches \"foobar\")",
49-
},
5047
{
5148
conditionalNode{nameNode{"a"}, nameNode{"a"}, nameNode{"b"}},
5249
"a ? a : b",
@@ -59,5 +56,36 @@ func TestPrint(t *testing.T) {
5956
if actual != test.expected {
6057
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", test.expected, actual, test.expected)
6158
}
59+
// Parse again and check if ast same as before.
60+
ast, err := Parse(actual)
61+
if err != nil {
62+
t.Errorf("%s: can't parse printed expression", actual)
63+
}
64+
if !reflect.DeepEqual(ast, test.input) {
65+
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", test.expected, ast, test.input)
66+
}
67+
}
68+
}
69+
70+
func TestPrint_matches(t *testing.T) {
71+
input := matchesNode{left: nameNode{"foo"}, right: textNode{"foobar"}}
72+
expected := "(foo matches \"foobar\")"
73+
74+
actual := fmt.Sprintf("%v", input)
75+
if actual != expected {
76+
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", expected, actual, expected)
77+
}
78+
// Parse again and check if ast same as before.
79+
ast, err := Parse(actual)
80+
if err != nil {
81+
t.Errorf("%s: can't parse printed expression", actual)
82+
}
83+
84+
// Clear parsed regexp.
85+
m := ast.(matchesNode)
86+
m.r = nil
87+
88+
if !reflect.DeepEqual(m, input) {
89+
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", expected, m, input)
6290
}
6391
}

0 commit comments

Comments
 (0)