forked from alecthomas/participle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_test.go
More file actions
57 lines (48 loc) · 1.17 KB
/
struct_test.go
File metadata and controls
57 lines (48 loc) · 1.17 KB
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
package participle
import (
"reflect"
"testing"
"text/scanner"
"github.com/stretchr/testify/assert"
"github.com/alecthomas/participle/lexer"
)
func TestStructLexerTokens(t *testing.T) {
type testScanner struct {
A string `12`
B string `34`
}
scan := lexStruct(reflect.TypeOf(testScanner{}))
t12 := lexer.Token{Type: scanner.Int, Value: "12", Pos: lexer.Position{Line: 1, Column: 1}}
t34 := lexer.Token{Type: scanner.Int, Value: "34", Pos: lexer.Position{Line: 2, Column: 1}}
assert.Equal(t, t12, scan.Peek())
assert.Equal(t, 0, scan.field)
assert.Equal(t, t12, scan.Next())
assert.Equal(t, t34, scan.Peek())
assert.Equal(t, 0, scan.field)
assert.Equal(t, t34, scan.Next())
assert.Equal(t, 1, scan.field)
assert.Equal(t, lexer.EOFToken, scan.Next())
}
func TestStructLexer(t *testing.T) {
g := struct {
A string `"a"|`
B string `"b"`
}{}
gt := reflect.TypeOf(g)
r := lexStruct(gt)
f := []reflect.StructField{}
s := ""
for {
r.Peek()
rn := r.Next()
if rn.EOF() {
break
}
f = append(f, r.Field())
s += string(rn.String())
}
assert.Equal(t, `a|b`, s)
f0 := gt.Field(0)
f1 := gt.Field(1)
assert.Equal(t, []reflect.StructField{f0, f0, f1}, f)
}