-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
struct_test.go
92 lines (80 loc) · 1.96 KB
/
struct_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package participle
import (
"reflect"
"testing"
"text/scanner"
require "github.com/alecthomas/assert/v2"
"github.com/alecthomas/participle/v2/lexer"
)
func TestStructLexerTokens(t *testing.T) {
type testScanner struct {
A string `12`
B string `34`
}
scan, err := lexStruct(reflect.TypeOf(testScanner{}))
require.NoError(t, err)
t12 := lexer.Token{Type: scanner.Int, Value: "12", Pos: lexer.Position{Filename: "testScanner", Line: 1, Column: 1}}
t34 := lexer.Token{Type: scanner.Int, Value: "34", Pos: lexer.Position{Filename: "B", Line: 2, Column: 1}}
require.Equal(t, t12, *mustPeek(scan))
require.Equal(t, 0, scan.field)
require.Equal(t, t12, *mustNext(scan))
require.Equal(t, t34, *mustPeek(scan))
require.Equal(t, 0, scan.field)
require.Equal(t, t34, *mustNext(scan))
require.Equal(t, 1, scan.field)
require.True(t, mustNext(scan).EOF())
}
func TestStructLexer(t *testing.T) {
g := struct {
A string `"a"|`
B string `"b"`
}{}
gt := reflect.TypeOf(g)
r, err := lexStruct(gt)
require.NoError(t, err)
f := []structLexerField{}
s := ""
for {
_, err := r.Peek()
require.NoError(t, err)
rn, err := r.Next()
require.NoError(t, err)
if rn.EOF() {
break
}
f = append(f, r.Field())
s += rn.String()
}
require.Equal(t, `a|b`, s)
f0 := r.GetField(0)
f1 := r.GetField(1)
require.Equal(t, []structLexerField{f0, f0, f1}, f)
}
type testEmbeddedIndexes struct {
A string `@String`
B string `@String`
}
func TestCollectFieldIndexes(t *testing.T) {
var grammar struct {
testEmbeddedIndexes
C string `@String`
}
typ := reflect.TypeOf(grammar)
indexes, err := collectFieldIndexes(typ)
require.NoError(t, err)
require.Equal(t, [][]int{{0, 0}, {0, 1}, {1}}, indexes)
}
func mustPeek(scan *structLexer) *lexer.Token {
token, err := scan.Peek()
if err != nil {
panic(err)
}
return token
}
func mustNext(scan *structLexer) *lexer.Token { // nolint: interfacer
token, err := scan.Next()
if err != nil {
panic(err)
}
return token
}