Skip to content

Commit 95fa24f

Browse files
committed
Refactor parser error tests
1 parent b2422fc commit 95fa24f

File tree

2 files changed

+55
-57
lines changed

2 files changed

+55
-57
lines changed

parser/parser.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,10 @@ func (p *parser) parsePrimary() Node {
217217
return p.parsePostfixExpression(expr)
218218
}
219219

220-
if token.Is(Operator, "#") {
221-
p.next()
222-
node := &PointerNode{Base: Loc(token.Location)}
223-
return p.parsePostfixExpression(node)
224-
}
225-
226-
if token.Is(Operator, ".") {
220+
if token.Is(Operator, "#") || token.Is(Operator, ".") {
221+
if token.Is(Operator, "#") {
222+
p.next()
223+
}
227224
node := &PointerNode{Base: Loc(token.Location)}
228225
return p.parsePostfixExpression(node)
229226
}

parser/parser_test.go

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -213,59 +213,60 @@ func TestParse(t *testing.T) {
213213
}
214214
}
215215

216+
const errorTests = `
217+
foo.
218+
unexpected end of expression (1:4)
219+
| foo.
220+
| ...^
221+
222+
a+
223+
unexpected token EOF (1:2)
224+
| a+
225+
| .^
226+
227+
a ? (1+2) c
228+
unexpected token Identifier("c") (1:11)
229+
| a ? (1+2) c
230+
| ..........^
231+
232+
[a b]
233+
unexpected token Identifier("b") (1:4)
234+
| [a b]
235+
| ...^
236+
237+
foo.bar(a b)
238+
unexpected token Identifier("b") (1:11)
239+
| foo.bar(a b)
240+
| ..........^
241+
242+
{-}
243+
a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token Operator("-")) (1:2)
244+
| {-}
245+
| .^
246+
247+
a matches 'a:)b'
248+
error parsing regexp: unexpected ): ` + "`a:)b`" + ` (1:16)
249+
| a matches 'a:)b'
250+
| ...............^
251+
252+
foo({.bar})
253+
a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token Operator(".")) (1:6)
254+
| foo({.bar})
255+
| .....^
256+
`
257+
216258
func TestParse_error(t *testing.T) {
217-
var parseErrorTests = []struct {
218-
input string
219-
err string
220-
}{
221-
{
222-
"foo.",
223-
"syntax error: missing Identifier at",
224-
},
225-
{
226-
"a+",
227-
"syntax error: mismatched input '<EOF>'",
228-
},
229-
{
230-
"a ? (1+2) c",
231-
"syntax error: missing ':' at 'c'",
232-
},
233-
{
234-
"[a b]",
235-
"syntax error: extraneous input 'b' expecting {']', ','}",
236-
},
237-
{
238-
"foo.bar(a b)",
239-
"syntax error: extraneous input 'b' expecting ')'",
240-
},
241-
{
242-
"{-}",
243-
"syntax error: no viable alternative at input '{-'",
244-
},
245-
{
246-
"a matches 'a)(b'",
247-
"error parsing regexp: unexpected )",
248-
},
249-
{
250-
`a matches "*"`,
251-
"error parsing regexp: missing argument to repetition operator: `*` (1:11)\n | a matches \"*\"\n | ..........^",
252-
},
253-
{
254-
`.foo`,
255-
"parse error: dot property accessor can be only inside closure",
256-
},
257-
{
258-
`foo({.bar})`,
259-
"syntax error: no viable alternative at input '{.'",
260-
},
261-
}
262-
for _, test := range parseErrorTests {
263-
_, err := parser.Parse(test.input)
259+
tests := strings.Split(strings.Trim(errorTests, "\n"), "\n\n")
260+
for _, test := range tests {
261+
input := strings.SplitN(test, "\n", 2)
262+
if len(input) != 2 {
263+
t.Errorf("syntax error in test: %q", test)
264+
break
265+
}
266+
_, err := parser.Parse(input[0])
264267
if err == nil {
265268
err = fmt.Errorf("<nil>")
266269
}
267-
if !strings.Contains(err.Error(), test.err) || test.err == "" {
268-
assert.Equal(t, test.err, err.Error(), test.input)
269-
}
270+
assert.Equal(t, input[1], err.Error(), input[0])
270271
}
271272
}

0 commit comments

Comments
 (0)