File: test_scanner.lua

package info (click to toggle)
lua-leg 0.1.2-8
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 252 kB
  • sloc: makefile: 32
file content (228 lines) | stat: -rwxr-xr-x 5,403 bytes parent folder | download | duplicates (5)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
-------------------------------------------------------------------------------
-- Scanner test suite for Leg
--
-- Authors: Humberto Anjos
-- Copyright (c) 2007 Leg
--
-- $Id: test_scanner.lua,v 1.2 2007/11/22 21:15:24 hanjos Exp $
--
-------------------------------------------------------------------------------

local scanner = require 'leg.scanner'

print 'Testing keyword matching...'

for KEY, patt in pairs(scanner.keywords) do
  local key = string.lower(KEY)
  
  -- The key alone
  assert(patt:match(key))
  
  -- The key in uppercase
  assert(not patt:match(KEY))
  
  -- The key twice
  assert(not patt:match(key..key))
  
  -- The key at the end of something
  assert(not patt:match('yadda_'..key))
  
  -- The key at the beginning of something
  assert(not patt:match(key..'yaddayadda'))
end

print '...OK!'
print 'Testing symbol matching...'

for symbol, patt in pairs(scanner.symbols) do
  -- The symbol alone
  assert(patt:match(symbol))
  
  -- The symbol at the end of something
  assert(not patt:match('yadda '..symbol))
  
  -- The symbol at the beginning of something
  assert(patt:match(symbol..' yadda') == #symbol + 1)
end

print '...OK!'
print 'Testing IDENTIFIER...'

local ID = scanner.IDENTIFIER

-- a keyword
assert(not ID:match'and')

-- a keyword in the name
assert(ID:match'andale' == 7)

-- an operator
assert(ID:match'make-name' == 5)

-- spaces
assert(ID:match'one two three' == 4)

-- invalid characters
assert(not ID:match'!!name$')

-- accent marks
assert(ID:match'ao' == 6)

-- weird, but should match
assert(ID:match'_123' == 5)

-- starting with a digit
assert(not ID:match'1abc')

-- digits at the end
assert(ID:match'abc1234' == 8)

-- all underscores
assert(ID:match'___' == 4)

-- weird characters
assert(ID:match'\127\128\129' == 5)

print '...OK!'
print 'Testing NUMBER...'

local NUM = scanner.NUMBER

-- natural number
assert(NUM:match'123')

-- natural number
assert(NUM:match'101')

-- natural number
assert(NUM:match'001234567890')

-- decimal point
assert(NUM:match'.123')

-- decimal point
assert(NUM:match'1.')

-- decimal point
assert(NUM:match'12.343')

-- hexadecimal notation
assert(NUM:match'0x7a2fF')

-- hexadecimal notation (yes, this matches)
--assert(NUM:match'0x')

-- hexadecimal notation
assert(not NUM:match'!ff')

-- scientific notation
assert(NUM:match'1e-23')

-- scientific notation
assert(NUM:match'9.045e+3')

-- scientific notation
assert(NUM:match'.00678e2')

-- scientific notation (works, but blows up in flames)
--assert(not NUM:match'1.23e4e5')

-- negative number (this is the unary operator - applied to 0.1)
assert(not NUM:match'-.1')

-- malformed number (works, but blows up in flames)
--assert(NUM:match'123and')

print '...OK!'
print 'Testing STRING...'

local STR = scanner.STRING

-- single quotes
assert(STR:match"'What a pity...'")

-- single quotes with escape characters
assert(STR:match"'What \065 \112\105\116\121...'")

-- unclosed single quotes (works, but blows up in flames)
--assert(not STR:match"'what?")

-- single quotes with line breaks inside (works, but blows up in flames)
--assert(not STR:match"'what\n?'")

-- single quotes with valid line breaks inside
assert(STR:match"'what\\\n?'")

-- single quotes, escaped
assert(STR:match"'What a \\'pity\\'...'")

-- single quotes, with double quotes inside
assert(STR:match"'What a \"pity\"...'")

-- double quotes
assert(STR:match'"What a pity..."')

-- double quotes with escape characters
assert(STR:match'"What \065 \112\105\116\121..."')

-- unclosed double quotes (works, but blows up in flames)
--assert(not STR:match'"what?')

-- double quotes with line breaks inside (works, but blows up in flames)
--assert(not STR:match'"what\n?"')

-- double quotes with valid line breaks inside
assert(STR:match'"what\\\n?"')

-- double quotes, escaped
assert(STR:match'"What a \\"pity\\"..."')

-- double quotes, with single quotes inside
assert(STR:match'"What a \'pity\'..."')

-- long strings
assert(STR:match"[[If not for you...]]")

-- multi-line long strings
assert(STR:match"[[something\n\or\nanother]]")

-- embedded long strings
assert(STR:match"[=[fantasy for sale [[that's entertainment!]]]=]")

-- long strings with quotes inside
assert(STR:match"[[Package it like a \"rebel\" or a 'hero']]")

-- unclosed long strings (works, but blows up in flames)
--assert(not STR:match"[=[[[Reality]] withdraws")

print '...OK!'
print 'Testing COMMENT...'

local COM = scanner.COMMENT

-- single line comment
assert(COM:match'-- single line comment\nsome code here' == 23)

-- single line comment, no line break
assert(COM:match'-- she can manipulate reactions')

-- multi-line comment
assert(COM:match'--[[superconductor!\nThat\'s entertainment!]]')

-- multi-line comment, alternate closing
assert(COM:match'--[[superconductor!\nThat\'s entertainment!\n--]]')

-- unclosed comment (works, but blows up in flames)
--assert(not COM:match'--[[<waiting for The Clansman to start>')

-- embedded multi-line comment
assert(COM:match'--[=[That belongs to --[[the clan]]]=]')

-- embedded multi-line comment
assert(COM:match[===[--[[ multi-line
--[==[
asdaks \n
-- --[[ deve pegar comment multi-line
--print'oi' ]] ]===])

print '...OK!'