-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_test.go
143 lines (120 loc) · 3.39 KB
/
lexer_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main_test
import "testing"
import . "github.com/nfisher/apib2go"
func Test_Lexer_Next_should_increment_through_characters_in_string(t *testing.T) {
l := New("meta.apib", `ab`)
for i, r := range []rune{'a', 'b', EOF} {
ch := l.Next()
if ch != r {
t.Errorf("[%v] got ch = %v, want %v", i, ch, r)
}
}
}
func Test_Lexer_Errorf_should_emit_error_with_description(t *testing.T) {
l := New("meta.apib", `func`)
go func() {
l.Errorf("Because")
}()
e := <-l.Items
if e.Type != ItemError {
t.Fatalf("got e.Type = %v, want %v", e.Type, ItemError)
}
if e.Value != "Because" {
t.Fatalf("got e.Value = %v, want %v", e.Value, "Because")
}
}
func Test_Lexer_Accept(t *testing.T) {
l := New("meta.apib", `bo`)
dataTable := [][]interface{}{
{"zb", true, "should accept first character"},
{"b", false, "should not accept second character"},
{"bo", true, "should accept second character"},
{"o", false, "should not accept EOF"},
}
for i, td := range dataTable {
valid := td[0].(string)
actual := l.Accept(valid)
expected := td[1].(bool)
if actual != expected {
t.Errorf("[%v] got l.Accept(%v) = %v, want %v. %v", i, valid, actual, expected, td[2].(string))
}
}
}
func Test_Lexer_AcceptRun(t *testing.T) {
// valid run runes, expected position, expectation desc.
dataTable := [][]interface{}{
{"012", 2, "should move position forward 2 runes"},
{"0123456789.", 11, "should move position forward 11 runes"},
{"0123456789.!eha", 18, "should move position forward to eof"},
{".!eha", 0, "should not move position forward"},
}
for i, td := range dataTable {
valid := td[0].(string)
l := New("meta.apib", `123456789.0e!hahah`)
l.AcceptRun(valid)
actual := l.Pos()
expected := td[1].(int)
if actual != expected {
desc := td[2].(string)
t.Errorf("[%v] got l.AcceptRun(%v) = %v, want %v. %v", i, valid, actual, expected, desc)
}
}
}
func Test_Lexer_AcceptUntil(t *testing.T) {
dataTable := [][]interface{}{
{"\n\r", "12345\r\n", 5, "12345", "should stop at carriage return."},
{"\r\n", "12345\n67890", 5, "12345", "should stop at new line."},
{"\r\n", "12345", 5, "12345", "should stop at EOF."},
{":", "FORMAT: 1A9", 6, "FORMAT", "should stop at colon."},
}
for i, td := range dataTable {
stop := td[0].(string)
l := New("meta.apib", td[1].(string))
l.AcceptUntil(stop)
pos := l.Pos()
expPos := td[2].(int)
if pos != expPos {
t.Errorf("[%v] want l.Pos() = %v, got %v", i, expPos, pos)
}
var pants ItemType = 100
go func() {
l.Emit(pants)
}()
exp := td[3].(string)
item := <-l.Items
if item.Value != exp {
t.Errorf("[%v] want item.Value = %v, go %v", i, exp, item.Value)
}
}
}
func Test_Lexer_AcceptClasses(t *testing.T) {
l := New("meta.apib", `func`)
l.AcceptClasses(RuneSet("f"), RuneSet("u"))
var pants ItemType = 100
go func() {
l.Emit(pants)
}()
item := <-l.Items
expVal := "fu"
if item.Value != expVal {
t.Errorf("got item.Value = %v, want %v", item.Value, expVal)
}
if item.Type != pants {
t.Errorf("got item.Type = %v, want %v", item.Type, pants)
}
}
func Test_Lexer_Emit(t *testing.T) {
l := New("meta.apib", `func stuff()`)
l.AcceptRun("func")
var pants ItemType = 100
go func() {
l.Emit(pants)
}()
item := <-l.Items
if item.Value != "func" {
t.Errorf("got item.Value = %v, want %v", item.Value, "func")
}
if item.Type != pants {
t.Errorf("got item.Type = %v, want %v", item.Type, pants)
}
}