-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex_test.go
115 lines (99 loc) · 2.3 KB
/
lex_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package lexrec
import (
//"fmt"
"strings"
"testing"
)
const (
ItemIgnore ItemType = ItemEOF + 1 + iota
ItemAorB
ItemTab
ItemA
ItemColon
ItemB
ItemEmit
)
type lexTest struct {
name string
input string
record Record
expect []Item
}
/*
var (
tItemEOF = Item{ItemEOF, 0, ""}
)
var parseA = Record{Buflen:1, ErrorFn:SkipPast("\n"), States:[]Binding{{ItemA, AcceptRun("a", true),true}}}
var lexTests = []lexTest{
{"empty", "", parseA, []Item[tItemEOF]},
}
func collect(l *Lexer) []Item {
items := []Item{}
for {
item := l.NextItem()
items = append(items, items)
if item.Type == ItemEOF {
break
}
}
return items
}
func TestLex(t *testing.T) {
for _, test := range lexTests {
l := NewLexer(test.name, test.record)
items := collect(l)
n := len(test.expect)
if len(items) < n {
t.Errorf("missing trailing items: %v", test.expect[len(items):])
} else if len(items) > n {
t.Errorf("unexpected extra items: %v", test.items[n:])
}
for i := range items {
if items[i].Type != test.expect[i].Type {
t.Errorf("got Type %q expected Type %q\n", items[i].Type, test.expect[i].Type)
}
if items[i].Pos != test.expect[i].Pos {
t.Errorf("got Pos %q expected Pos %q\n", items[i].Pos, test.expect[i].Pos)
}
if items[i].Value != test.expect[i].Value {
t.Errorf("got Value %q expected Value %q\n", items[i].Value, test.expect[i].Value)
}
}
}
}
*/
var acceptRunA = AcceptRun("a", true)
var aRecord = Record{
Buflen: 1,
ErrorFn: SkipPast("\n"),
States: []Binding{
{ItemEmit, acceptRunA, true}}}
func TestLexerAcceptRunA(t *testing.T) {
r := strings.NewReader("aaaaaaaaaa")
l, err := NewLexer("TestLexerAccceptRunA", r, aRecord)
if err != nil {
t.Fatal(err)
}
item := l.NextItem()
if len(item.Value) != 10 {
t.Errorf("expected 10 bytes, got %d\n", len(item.Value))
}
}
func TestLexerSkipPast(t *testing.T) {
r := strings.NewReader("bbb\n\n\n\n\na")
l, err := NewLexer("TestLexerSkipPast", r, aRecord)
if err != nil {
t.Fatal(err)
}
item := l.NextItem()
if item.Type != ItemError {
t.Fatalf("expected ItemError on character b, got %q", item)
}
item = l.NextItem()
if item.Type != ItemEmit {
t.Fatalf("expected ItemEmit on character b, got %q", item)
}
if item.Value != "a" {
t.Fatalf("expected ItemEmit of one character 'a', got %q", item.Value)
}
}