-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlexer_test.go
More file actions
201 lines (192 loc) · 4.82 KB
/
lexer_test.go
File metadata and controls
201 lines (192 loc) · 4.82 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package lawparser
import (
"testing"
)
var textest = "abcdefghijkl"
var spaces = " "
var newlines = "\n \n \n"
var words = "comida comer perro"
var digits = "12 34 56 1000 10"
var reserveds = "artículo capítulo sección Sección Capitulo"
var romans = "I II III V X XXX"
var ordinals = "Primero segundo tercero cuarto quinto quinta"
var latins = "Bis bis ter Ter"
var symbols = "@ * . - ¬ | ° ¨ ^ .-"
var containers = "\nartículo 1.-\nCapítulo 10\n\nArtículo primero.-\nArtículo décimo.-\nSección 30 bis\n"
var subarticlelet = "\na)\nb)\nc)"
var subarticlenum = "\n1)\nprimero)\n1 bis)"
var undefinedB = []byte{1, 2, 3, 4}
func TestPeek(t *testing.T) {
textestB := []byte(textest)
l := lex(textestB)
if l.char != 'a' {
t.Error("expected a got ", string(l.char))
}
backup := l.pos
l.peek()
l.peek()
if l.char != 'c' {
t.Error("expected c got ", string(l.char))
}
l.peek()
l.peek()
if l.char != 'e' {
t.Error("expected e got ", string(l.char))
}
l.reset(backup)
if l.char != 'a' {
t.Error("expected a got ", string(l.char))
}
}
func TestReset(t *testing.T) {
textestB := []byte(textest)
l := lex(textestB)
if l.char != 'a' {
t.Error("expected a got ", string(l.char))
}
backup := l.pos //a
l.peek()
l.peek()
backup2 := l.pos //c
l.peek()
l.peek()
l.peek()
if l.char != 'f' {
t.Error("expected f got ", string(l.char))
}
l.reset(backup2)
if l.char != 'c' {
t.Error("expected c got ", string(l.char))
}
l.reset(backup)
if l.char != 'a' {
t.Error("expected a got ", string(l.char))
}
}
func TestConfirm(t *testing.T) {
textestB := []byte(textest)
l := lex(textestB)
if l.char != 'a' {
t.Error("expected a got ", string(l.char))
}
l.peek()
l.peek()
l.confirm()
if l.char != 'c' {
t.Error("expected c got ", string(l.char))
}
if l.pos != Pos(0) {
t.Error("expected pos==0, got pos==", l.pos)
}
}
func TestLexSpace(t *testing.T) {
spacesB := []byte(spaces)
l := lex(spacesB)
for it := l.nextItem(); it.typ != itemEOF; it = l.nextItem() {
}
}
func TestLexNewLine(t *testing.T) {
newlinesB := []byte(newlines)
l := lex(newlinesB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemNewLine {
t.Error("Expected itemNewLine got ", item)
}
}
}
func TestLexWord(t *testing.T) {
wordsB := []byte(words)
l := lex(wordsB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemWord {
t.Error("Expected itemWord got ", item)
}
}
}
func TestLexDigits(t *testing.T) {
digitsB := []byte(digits)
l := lex(digitsB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemNumber {
t.Error("Expected itemNumber got ", item)
}
}
}
func TestLexSymbols(t *testing.T) {
symbolsB := []byte(symbols)
l := lex(symbolsB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemSymbol {
t.Error("Expected itemSymbol got ", item)
}
}
}
func TestLexReserveds(t *testing.T) {
reservedsB := []byte(reserveds)
l := lex(reservedsB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemReserved {
t.Error("Expected itemReserved got ", item)
}
}
}
func TestLexOrdinals(t *testing.T) {
ordinalsB := []byte(ordinals)
l := lex(ordinalsB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemOrdinal {
t.Error("Expected itemOrdinal got ", item)
}
}
}
func TestLexRomans(t *testing.T) {
romansB := []byte(romans)
l := lex(romansB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemRoman {
t.Error("Expected itemRoman got ", item)
}
}
}
func TestLexLatins(t *testing.T) {
latinsB := []byte(latins)
l := lex(latinsB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemLatin {
t.Error("Expected itemLatin got ", item)
}
}
}
func TestLexContainers(t *testing.T) {
containersB := []byte(containers)
l := lex(containersB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ < itemContainer {
t.Error("Expected itemContainer got ", item)
}
}
}
func TestLexSubArticleLet(t *testing.T) {
subarticleletB := []byte(subarticlelet)
l := lex(subarticleletB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemSubArticleLet {
t.Error("Expected itemSubArticleLet got ", item)
}
}
}
func TestLexSubArticleNum(t *testing.T) {
subarticlenumB := []byte(subarticlenum)
l := lex(subarticlenumB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
if item.typ != itemSubArticleNum {
t.Error("Expected itemSubArticleNum got ", item)
}
}
}
func TestLexUndefined(t *testing.T) {
l := lex(undefinedB)
for item := l.nextItem(); item.typ != itemEOF; item = l.nextItem() {
t.Error("Expecting no items here, got ", item)
}
}