@@ -101,10 +101,6 @@ var parseTests = []parseTest{
101101 "a ?: b" ,
102102 conditionalNode {nameNode {"a" }, nameNode {"a" }, nameNode {"b" }},
103103 },
104- {
105- `"foo" matches "/foo/"` ,
106- binaryNode {"matches" , textNode {"foo" }, textNode {"/foo/" }},
107- },
108104 {
109105 "foo.bar().foo().baz[33]" ,
110106 propertyNode {propertyNode {methodNode {methodNode {nameNode {"foo" }, identifierNode {"bar" }, []Node {}}, identifierNode {"foo" }, []Node {}}, identifierNode {"baz" }}, numberNode {33 }},
@@ -169,6 +165,10 @@ var parseErrorTests = []parseErrorTest{
169165 "{-}" ,
170166 "a map key must be a" ,
171167 },
168+ {
169+ "a matches 'a)(b'" ,
170+ "error parsing regexp: unexpected )" ,
171+ },
172172}
173173
174174func TestParse (t * testing.T ) {
@@ -196,14 +196,42 @@ func TestParseError(t *testing.T) {
196196 }
197197}
198198
199- func TestParseErrorPosition (t * testing.T ) {
200- _ , err := Parse ("foo() + bar(**)" )
201- if err == nil {
202- err = fmt .Errorf ("<nil>" )
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 )
203232 }
204233
205- expected := "unexpected token operator(**)\n foo() + bar(**)\n ------------^"
206- if err .Error () != expected {
207- t .Errorf ("\n got\n \t %+v\n expected\n \t %v" , err .Error (), expected )
234+ if m .r != nil {
235+ t .Fatal ("regexp should not be compiled" )
208236 }
209237}
0 commit comments