aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/test/test_llex_mk3.lua
diff options
context:
space:
mode:
Diffstat (limited to 'LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/test/test_llex_mk3.lua')
-rw-r--r--LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/test/test_llex_mk3.lua500
1 files changed, 0 insertions, 500 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/test/test_llex_mk3.lua b/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/test/test_llex_mk3.lua
deleted file mode 100644
index 8b0eec9..0000000
--- a/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/test/test_llex_mk3.lua
+++ /dev/null
@@ -1,500 +0,0 @@
1--[[--------------------------------------------------------------------
2
3 test_llex.lua
4 Test for llex.lua
5 This file is part of Yueliang.
6
7 Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
8 The COPYRIGHT file describes the conditions
9 under which this software may be distributed.
10
11 See the ChangeLog for more information.
12
13----------------------------------------------------------------------]]
14
15------------------------------------------------------------------------
16-- if BRIEF is not set to false, auto-test will silently succeed
17------------------------------------------------------------------------
18BRIEF = true -- if set to true, messages are less verbose
19
20local lex_init = require("../llex_mk3")
21
22------------------------------------------------------------------------
23-- simple manual tests
24------------------------------------------------------------------------
25
26--[[
27local function dump(z, source)
28 local luaX = lex_init(z, source)
29 while true do
30 local tok, seminfo = luaX:lex()
31 if tok == "<name>" then
32 seminfo = " "..seminfo
33 elseif tok == "<number>" then
34 seminfo = " "..seminfo
35 elseif tok == "<string>" then
36 seminfo = " '"..seminfo.."'"
37 else
38 seminfo = ""
39 end
40 io.stdout:write(tok..seminfo.."\n")
41 if tok == "<eof>" then break end
42 end
43end
44
45local function try_string(chunk)
46 dump(chunk, "=string")
47end
48local function try_file(filename)
49 local inf = io.open(filename, "r")
50 if not inf then error("file not found") end
51 local data = inf:read("*a")
52 inf:close()
53 dump(data, "@"..filename)
54end
55
56z = try_string("local c = z:getc()")
57z = try_file("test_lzio_mk2.lua")
58z = try_file("test_llex_mk2.lua")
59os.exit()
60--]]
61
62------------------------------------------------------------------------
63-- auto-testing of simple test cases to validate lexer behaviour:
64-- * NOTE coverage has not been checked; not comprehensive
65-- * only test cases with non-empty comments are processed
66-- * if no result, then the output is displayed for manual decision
67-- (output may be used to set expected success or fail text)
68-- * cases expected to be successful may be a partial match
69-- * cases expected to fail may also be a partial match
70------------------------------------------------------------------------
71
72-- [[
73local function auto_test()
74 local PASS, FAIL = true, false
75 ------------------------------------------------------------------
76 -- table of test cases
77 ------------------------------------------------------------------
78 local test_cases =
79 {
80 -------------------------------------------------------------
81 --{ "comment", -- comment about the test
82 -- "chunk", -- chunk to test
83 -- PASS, -- PASS or FAIL outcome
84 -- "output", -- output to compare against
85 --},
86 -------------------------------------------------------------
87 { "empty chunk string, test EOS",
88 "",
89 PASS, "1 <eof>",
90 },
91 -------------------------------------------------------------
92 { "line number counting",
93 "\n\n\r\n",
94 PASS, "4 <eof>",
95 },
96 -------------------------------------------------------------
97 { "various whitespaces",
98 " \n\t\t\n \t \t \n\n",
99 PASS, "5 <eof>",
100 },
101 -------------------------------------------------------------
102 { "short comment ending in EOS",
103 "-- moo moo",
104 PASS, "1 <eof>",
105 },
106 -------------------------------------------------------------
107 { "short comment ending in newline",
108 "-- moo moo\n",
109 PASS, "2 <eof>",
110 },
111 -------------------------------------------------------------
112 { "several lines of short comments",
113 "--moo\n-- moo moo\n\n--\tmoo\n",
114 PASS, "5 <eof>",
115 },
116 -------------------------------------------------------------
117 { "basic block comment",
118 "--[[bovine]]",
119 PASS, "1 <eof>",
120 },
121 -------------------------------------------------------------
122 { "unterminated block comment 1",
123 "--[[bovine",
124 FAIL, ":1: unfinished long comment",
125 },
126 -------------------------------------------------------------
127 { "unterminated block comment 2",
128 "--[[bovine]",
129 FAIL, ":1: unfinished long comment",
130 },
131 -------------------------------------------------------------
132 { "unterminated block comment 3",
133 "--[[bovine\nmoo moo\nwoof",
134 FAIL, ":3: unfinished long comment",
135 },
136 -------------------------------------------------------------
137 { "basic long string",
138 "\n[[bovine]]\n",
139 PASS, "2 <string> = bovine\n3 <eof>",
140 },
141 -------------------------------------------------------------
142 { "first newline consumed in long string",
143 "[[\nmoo]]",
144 PASS, "2 <string> = moo\n2 <eof>",
145 },
146 -------------------------------------------------------------
147 { "multiline long string",
148 "[[moo\nmoo moo\n]]",
149 PASS, "3 <string> = moo\nmoo moo\n\n3 <eof>",
150 },
151 -------------------------------------------------------------
152 { "unterminated long string 1",
153 "\n[[\nbovine",
154 FAIL, ":3: unfinished long string",
155 },
156 -------------------------------------------------------------
157 { "unterminated long string 2",
158 "[[bovine]",
159 FAIL, ":1: unfinished long string",
160 },
161 -------------------------------------------------------------
162 { "unterminated long string 3",
163 "[[[[ \n",
164 FAIL, ":2: unfinished long string",
165 },
166 -------------------------------------------------------------
167 { "nested long string 1",
168 "[[moo[[moo]]moo]]",
169 PASS, "moo[[moo]]moo",
170 },
171 -------------------------------------------------------------
172 { "nested long string 2",
173 "[[moo[[moo[[[[]]]]moo]]moo]]",
174 PASS, "moo[[moo[[[[]]]]moo]]moo",
175 },
176 -------------------------------------------------------------
177 { "nested long string 3",
178 "[[[[[[]]]][[[[]]]]]]",
179 PASS, "[[[[]]]][[[[]]]]",
180 },
181 -------------------------------------------------------------
182 { "brackets in long strings 1",
183 "[[moo[moo]]",
184 PASS, "moo[moo",
185 },
186 -------------------------------------------------------------
187 { "brackets in long strings 2",
188 "[[moo[[moo]moo]]moo]]",
189 PASS, "moo[[moo]moo]]moo",
190 },
191 -------------------------------------------------------------
192 { "unprocessed escapes in long strings",
193 [[ [[\a\b\f\n\r\t\v\123]] ]],
194 PASS, [[\a\b\f\n\r\t\v\123]],
195 },
196 -------------------------------------------------------------
197 { "unbalanced long string",
198 "[[moo]]moo]]",
199 PASS, "1 <string> = moo\n1 <name> = moo\n1 CHAR = ']'\n1 CHAR = ']'\n1 <eof>",
200 },
201 -------------------------------------------------------------
202 { "keywords 1",
203 "and break do else",
204 PASS, "1 and\n1 break\n1 do\n1 else\n1 <eof>",
205 },
206 -------------------------------------------------------------
207 { "keywords 2",
208 "elseif end false for",
209 PASS, "1 elseif\n1 end\n1 false\n1 for\n1 <eof>",
210 },
211 -------------------------------------------------------------
212 { "keywords 3",
213 "function if in local nil",
214 PASS, "1 function\n1 if\n1 in\n1 local\n1 nil\n1 <eof>",
215 },
216 -------------------------------------------------------------
217 { "keywords 4",
218 "not or repeat return",
219 PASS, "1 not\n1 or\n1 repeat\n1 return\n1 <eof>",
220 },
221 -------------------------------------------------------------
222 { "keywords 5",
223 "then true until while",
224 PASS, "1 then\n1 true\n1 until\n1 while\n1 <eof>",
225 },
226 -------------------------------------------------------------
227 { "concat and dots",
228 ".. ...",
229 PASS, "1 ..\n1 ...\n1 <eof>",
230 },
231 -------------------------------------------------------------
232 { "shbang handling 1",
233 "#blahblah",
234 PASS, "1 <eof>",
235 },
236 -------------------------------------------------------------
237 { "shbang handling 2",
238 "#blahblah\nmoo moo\n",
239 PASS, "2 <name> = moo\n2 <name> = moo\n3 <eof>",
240 },
241 -------------------------------------------------------------
242 { "empty string",
243 [['']],
244 PASS, "1 <string> = \n1 <eof>",
245 },
246 -------------------------------------------------------------
247 { "single-quoted string",
248 [['bovine']],
249 PASS, "1 <string> = bovine\n1 <eof>",
250 },
251 -------------------------------------------------------------
252 { "double-quoted string",
253 [["bovine"]],
254 PASS, "1 <string> = bovine\n1 <eof>",
255 },
256 -------------------------------------------------------------
257 { "unterminated string 1",
258 [['moo ]],
259 FAIL, ":1: unfinished string",
260 },
261 -------------------------------------------------------------
262 { "unterminated string 2",
263 [["moo \n]],
264 FAIL, ":1: unfinished string",
265 },
266 -------------------------------------------------------------
267 { "escaped newline in string, line number counted",
268 "\"moo\\\nmoo\\\nmoo\"",
269 PASS, "3 <string> = moo\nmoo\nmoo\n3 <eof>",
270 },
271 -------------------------------------------------------------
272 { "escaped characters in string 1",
273 [["moo\amoo"]],
274 PASS, "1 <string> = moo\amoo",
275 },
276 -------------------------------------------------------------
277 { "escaped characters in string 2",
278 [["moo\bmoo"]],
279 PASS, "1 <string> = moo\bmoo",
280 },
281 -------------------------------------------------------------
282 { "escaped characters in string 3",
283 [["moo\f\n\r\t\vmoo"]],
284 PASS, "1 <string> = moo\f\n\r\t\vmoo",
285 },
286 -------------------------------------------------------------
287 { "escaped characters in string 4",
288 [["\\ \" \' \? \[ \]"]],
289 PASS, "1 <string> = \\ \" \' \? \[ \]",
290 },
291 -------------------------------------------------------------
292 { "escaped characters in string 5",
293 [["\z \k \: \;"]],
294 PASS, "1 <string> = z k : ;",
295 },
296 -------------------------------------------------------------
297 { "escaped characters in string 6",
298 [["\8 \65 \160 \180K \097097"]],
299 PASS, "1 <string> = \8 \65 \160 \180K \097097\n",
300 },
301 -------------------------------------------------------------
302 { "escaped characters in string 7",
303 [["\666"]],
304 FAIL, ":1: escape sequence too large",
305 },
306 -------------------------------------------------------------
307 { "simple numbers",
308 "123 123+",
309 PASS, "1 <number> = 123\n1 <number> = 123\n1 CHAR = '+'\n1 <eof>",
310 },
311 -------------------------------------------------------------
312 { "longer numbers",
313 "1234567890 12345678901234567890",
314 PASS, "1 <number> = 1234567890\n1 <number> = 1.2345678901235e+19\n",
315 },
316 -------------------------------------------------------------
317 { "fractional numbers",
318 ".123 .12345678901234567890",
319 PASS, "1 <number> = 0.123\n1 <number> = 0.12345678901235\n",
320 },
321 -------------------------------------------------------------
322 { "more numbers with decimal points",
323 "12345.67890 1.1.",
324 PASS, "1 <number> = 12345.6789\n1 <number> = 1.1\n1 CHAR = '.'\n",
325 },
326 -------------------------------------------------------------
327 { "double decimal points",
328 ".1.1",
329 FAIL, ":1: malformed number",
330 },
331 -------------------------------------------------------------
332 { "double dots within numbers",
333 "1..1",
334 FAIL, ":1: ambiguous syntax (dots follows digits)",
335 },
336 -------------------------------------------------------------
337 { "incomplete exponential numbers",
338 "123e",
339 FAIL, ":1: malformed number",
340 },
341 -------------------------------------------------------------
342 { "exponential numbers 1",
343 "1234e5 1234e5.",
344 PASS, "1 <number> = 123400000\n1 <number> = 123400000\n1 CHAR = '.'",
345 },
346 -------------------------------------------------------------
347 { "exponential numbers 2",
348 "1234e56 1.23e123",
349 PASS, "1 <number> = 1.234e+59\n1 <number> = 1.23e+123\n",
350 },
351 -------------------------------------------------------------
352 { "exponential numbers 3",
353 "12.34e+",
354 FAIL, ":1: malformed number",
355 },
356 -------------------------------------------------------------
357 { "exponential numbers 4",
358 "12.34e+5 123.4e-5 1234.E+5",
359 PASS, "1 <number> = 1234000\n1 <number> = 0.001234\n1 <number> = 123400000\n",
360 },
361 -------------------------------------------------------------
362 { "single character symbols 1",
363 "= > < ~",
364 PASS, "1 CHAR = '='\n1 CHAR = '>'\n1 CHAR = '<'\n1 CHAR = '~'\n",
365 },
366 -------------------------------------------------------------
367 { "double character symbols",
368 "== >= <= ~=",
369 PASS, "1 ==\n1 >=\n1 <=\n1 ~=\n",
370 },
371 -------------------------------------------------------------
372 { "simple identifiers",
373 "abc ABC",
374 PASS, "1 <name> = abc\n1 <name> = ABC\n1 <eof>",
375 },
376 -------------------------------------------------------------
377 { "more identifiers",
378 "_abc _ABC",
379 PASS, "1 <name> = _abc\n1 <name> = _ABC\n1 <eof>",
380 },
381 -------------------------------------------------------------
382 { "still more identifiers",
383 "_aB_ _123",
384 PASS, "1 <name> = _aB_\n1 <name> = _123\n1 <eof>",
385 },
386 -------------------------------------------------------------
387 { "invalid control character",
388 "\4",
389 FAIL, ":1: invalid control char",
390 },
391 -------------------------------------------------------------
392 { "single character symbols 2",
393 "` ! @ $ %",
394 PASS, "1 CHAR = '`'\n1 CHAR = '!'\n1 CHAR = '@'\n1 CHAR = '$'\n1 CHAR = '%'\n",
395 },
396 -------------------------------------------------------------
397 { "single character symbols 3",
398 "^ & * ( )",
399 PASS, "1 CHAR = '^'\n1 CHAR = '&'\n1 CHAR = '*'\n1 CHAR = '('\n1 CHAR = ')'\n",
400 },
401 -------------------------------------------------------------
402 { "single character symbols 4",
403 "_ - + \\ |",
404 PASS, "1 <name> = _\n1 CHAR = '-'\n1 CHAR = '+'\n1 CHAR = '\\'\n1 CHAR = '|'\n",
405 },
406 -------------------------------------------------------------
407 { "single character symbols 5",
408 "{ } [ ] :",
409 PASS, "1 CHAR = '{'\n1 CHAR = '}'\n1 CHAR = '['\n1 CHAR = ']'\n1 CHAR = ':'\n",
410 },
411 -------------------------------------------------------------
412 { "single character symbols 6",
413 "; , . / ?",
414 PASS, "1 CHAR = ';'\n1 CHAR = ','\n1 CHAR = '.'\n1 CHAR = '/'\n1 CHAR = '?'\n",
415 },
416 -------------------------------------------------------------
417 }
418 ------------------------------------------------------------------
419 -- perform a test case
420 ------------------------------------------------------------------
421 function do_test_case(count, test_case)
422 if comment == "" then return end -- skip empty entries
423 local comment, chunk, outcome, matcher = unpack(test_case)
424 local result = PASS
425 local output = ""
426 -- initialize lexer
427 local luaX = lex_init(chunk, "=test")
428 -- lexer test loop
429 local status, token, seminfo
430 repeat
431 -- protected call
432 status, token, seminfo = pcall(luaX.lex, luaX)
433 output = output..luaX.ln.." "
434 if status then
435 -- successful call
436 if string.len(token) > 1 then
437 if token == "<name>"
438 or token == "<number>"
439 or token == "<string>" then
440 token = token.." = "..seminfo
441 end
442 elseif string.byte(token) >= 32 then -- displayable chars
443 token = "CHAR = '"..token.."'"
444 else -- control characters
445 token = "CHAR = (".. string.byte(token)..")"
446 end
447 output = output..token.."\n"
448 else
449 -- failed call
450 output = output..token -- token is the error message
451 result = FAIL
452 break
453 end
454 until token == "<eof>"
455 -- decision making and reporting
456 local head = "Test "..count..": "..comment
457 if matcher == "" then
458 -- nothing to check against, display for manual check
459 print(head.."\nMANUAL please check manually"..
460 "\n--chunk---------------------------------\n"..chunk..
461 "\n--actual--------------------------------\n"..output..
462 "\n\n")
463 return
464 else
465 if outcome == PASS then
466 -- success expected, may be a partial match
467 if string.find(output, matcher, 1, 1) and result == PASS then
468 if not BRIEF then print(head.."\nOK expected success\n") end
469 return
470 end
471 else
472 -- failure expected, may be a partial match
473 if string.find(output, matcher, 1, 1) and result == FAIL then
474 if not BRIEF then print(head.."\nOK expected failure\n") end
475 return
476 end
477 end
478 -- failed because of unmatched string or boolean result
479 local function passfail(status)
480 if status == PASS then return "PASS" else return "FAIL" end
481 end
482 print(head.." *FAILED*"..
483 "\noutcome="..passfail(outcome)..
484 "\nactual= "..passfail(result)..
485 "\n--chunk---------------------------------\n"..chunk..
486 "\n--expected------------------------------\n"..matcher..
487 "\n--actual--------------------------------\n"..output..
488 "\n\n")
489 end
490 end
491 ------------------------------------------------------------------
492 -- perform auto testing
493 ------------------------------------------------------------------
494 for i,test_case in ipairs(test_cases) do
495 do_test_case(i, test_case)
496 end
497end
498
499auto_test()
500--]]