File: error_test.go

package info (click to toggle)
golang-github-alecthomas-participle 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 564 kB
  • sloc: makefile: 2
file content (37 lines) | stat: -rw-r--r-- 912 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package participle

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestErrorReporting(t *testing.T) {
	type cls struct {
		Visibility string `@"public"?`
		Class      string `"class" @Ident`
	}
	type union struct {
		Visibility string `@"public"?`
		Union      string `"union" @Ident`
	}
	type decl struct {
		Class *cls   `(  @@`
		Union *union ` | @@ )`
	}
	type grammar struct {
		Decls []*decl `( @@ ";" )*`
	}
	p := mustTestParser(t, &grammar{}, UseLookahead(5))

	var err error
	ast := &grammar{}
	err = p.ParseString(`public class A;`, ast)
	assert.NoError(t, err)
	err = p.ParseString(`public union A;`, ast)
	assert.NoError(t, err)
	err = p.ParseString(`public struct Bar;`, ast)
	assert.EqualError(t, err, `1:8: unexpected token "struct" (expected "union")`)
	err = p.ParseString(`public class 1;`, ast)
	assert.EqualError(t, err, `1:14: unexpected token "1" (expected <ident>)`)
}