forked from alecthomas/participle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
49 lines (43 loc) · 1.32 KB
/
error_test.go
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
38
39
40
41
42
43
44
45
46
47
48
49
package participle_test
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
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{}, participle.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" <ident>)`)
err = p.ParseString("", `public class 1;`, ast)
assert.EqualError(t, err, `1:14: unexpected token "1" (expected <ident>)`)
}
func TestErrorWrap(t *testing.T) {
expected := errors.New("badbad")
err := participle.Wrapf(lexer.Position{Line: 1, Column: 1}, expected, "bad: %d", 10)
require.Equal(t, expected, errors.Unwrap(err))
require.Equal(t, "1:1: bad: 10: badbad", err.Error())
}