Skip to content

Commit 40933a2

Browse files
committed
Simplify tests
1 parent 02b4800 commit 40933a2

File tree

2 files changed

+20
-63
lines changed

2 files changed

+20
-63
lines changed

parser_test.go

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ var parseTests = []parseTest{
133133
"len(foo)",
134134
builtinNode{"len", []Node{nameNode{"foo"}}},
135135
},
136+
{
137+
`foo matches "foo"`,
138+
matchesNode{left: nameNode{"foo"}, right: textNode{"foo"}},
139+
},
140+
{
141+
`foo matches regex`,
142+
matchesNode{left: nameNode{"foo"}, right: nameNode{"regex"}},
143+
},
136144
}
137145

138146
type parseErrorTest struct {
@@ -178,6 +186,10 @@ func TestParse(t *testing.T) {
178186
t.Errorf("%s:\n%v", test.input, err)
179187
continue
180188
}
189+
if m, ok := actual.(matchesNode); ok {
190+
m.r = nil
191+
actual = m
192+
}
181193
if !reflect.DeepEqual(actual, test.expected) {
182194
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", test.input, actual, test.expected)
183195
}
@@ -195,43 +207,3 @@ func TestParse_error(t *testing.T) {
195207
}
196208
}
197209
}
198-
199-
func TestParse_matches(t *testing.T) {
200-
node, err := Parse(`foo matches "foo"`)
201-
if err != nil {
202-
t.Fatal(err)
203-
}
204-
205-
m, ok := node.(matchesNode)
206-
if !ok {
207-
t.Fatalf("expected to me matchesNode, got %T", node)
208-
}
209-
210-
if !reflect.DeepEqual(m.left, nameNode{"foo"}) || !reflect.DeepEqual(m.right, textNode{"foo"}) {
211-
t.Fatalf("left or right side of matches operator invalid: %#v", m)
212-
}
213-
214-
if m.r == nil {
215-
t.Fatal("regexp should be compiled")
216-
}
217-
}
218-
219-
func TestParse_matches_dynamic(t *testing.T) {
220-
node, err := Parse(`foo matches regex`)
221-
if err != nil {
222-
t.Fatal(err)
223-
}
224-
225-
m, ok := node.(matchesNode)
226-
if !ok {
227-
t.Fatalf("expected to me matchesNode, got %T", node)
228-
}
229-
230-
if !reflect.DeepEqual(m.left, nameNode{"foo"}) || !reflect.DeepEqual(m.right, nameNode{"regex"}) {
231-
t.Fatalf("left or right side of matches operator invalid: %#v", m)
232-
}
233-
234-
if m.r != nil {
235-
t.Fatal("regexp should not be compiled")
236-
}
237-
}

print_test.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ var printTests = []printTest{
4848
conditionalNode{nameNode{"a"}, nameNode{"a"}, nameNode{"b"}},
4949
"a ? a : b",
5050
},
51+
{
52+
matchesNode{left: nameNode{"foo"}, right: textNode{"foobar"}},
53+
"(foo matches \"foobar\")",
54+
},
5155
}
5256

5357
func TestPrint(t *testing.T) {
@@ -61,31 +65,12 @@ func TestPrint(t *testing.T) {
6165
if err != nil {
6266
t.Errorf("%s: can't parse printed expression", actual)
6367
}
68+
if m, ok := ast.(matchesNode); ok {
69+
m.r = nil
70+
ast = m
71+
}
6472
if !reflect.DeepEqual(ast, test.input) {
6573
t.Errorf("%s:\ngot\n\t%#v\nexpected\n\t%#v", test.expected, ast, test.input)
6674
}
6775
}
6876
}
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)
90-
}
91-
}

0 commit comments

Comments
 (0)