|
| 1 | +--[[------------------------------------------------------------------- |
| 2 | +
|
| 3 | + LSDTest.lua |
| 4 | + Test functions for LuaSrcDiet.lua, including automatic testing. |
| 5 | +
|
| 6 | + Copyright (c) 2005 Kein-Hong Man <khman@users.sf.net> |
| 7 | + The COPYRIGHT file describes the conditions under which this |
| 8 | + software may be distributed (basically a Lua 5-style license.) |
| 9 | +
|
| 10 | + http://luaforge.net/projects/luasrcdiet/ |
| 11 | + (TODO) http://www.geocities.com/keinhong/luasrcdiet.html |
| 12 | + See the ChangeLog for more information. |
| 13 | +
|
| 14 | +----------------------------------------------------------------------- |
| 15 | +--]] |
| 16 | + |
| 17 | +--[[------------------------------------------------------------------- |
| 18 | +-- Notes: |
| 19 | +-- * To run: lua LSDTest.lua |
| 20 | +-- * TODO include test suite for lexer (currently elsewhere) |
| 21 | +-- * Look near the end of the script for testing options. |
| 22 | +-- * Setting TEST disables main() in LuaSrcDiet.lua. |
| 23 | +----------------------------------------------------------------------- |
| 24 | +--]] |
| 25 | + |
| 26 | +----------------------------------------------------------------------- |
| 27 | +TEST = true -- always true to skip LuaSrcDiet's main() |
| 28 | +require("LuaSrcDiet.lua") |
| 29 | + |
| 30 | +----------------------------------------------------------------------- |
| 31 | +-- tests adjacent pairs to see whether lexer accepts it |
| 32 | +-- * most pairs would not be valid Lua code, just valid lexer input |
| 33 | +----------------------------------------------------------------------- |
| 34 | +function TestTokenPairs() |
| 35 | + local items = { |
| 36 | + "while'foo'", "foo'bar'", "12then", "12.34foo", |
| 37 | + "'foo'end", "'foo'bar", "'foo''bar'", "'foo'123", |
| 38 | + "12.3'foo'", |
| 39 | + } |
| 40 | + for _, chunk in ipairs(items) do |
| 41 | + llex:setstring(chunk, "(test)") |
| 42 | + local ltok, lorig, lval |
| 43 | + while ltok ~= "TK_EOS" do |
| 44 | + lline = llex.line |
| 45 | + ltok, lorig, lval = llex:lex() |
| 46 | + lorig = lorig or "" |
| 47 | + if ltok ~= "TK_EOS" then |
| 48 | + print(lline, ltok, "'"..lorig.."'") |
| 49 | + end |
| 50 | + end |
| 51 | + print() |
| 52 | + end |
| 53 | +end |
| 54 | + |
| 55 | +----------------------------------------------------------------------- |
| 56 | +-- simple token dumper for visual inspection |
| 57 | +----------------------------------------------------------------------- |
| 58 | +function DumpTokens(filename) |
| 59 | + if not filename and type(filename) ~= "string" then |
| 60 | + error("invalid filename specified for DumpTokens") |
| 61 | + end |
| 62 | + local INF = io.open(filename, "rb") |
| 63 | + if not INF then |
| 64 | + error("cannot open \""..filename.."\" for reading") |
| 65 | + end |
| 66 | + llex:setinput(INF, filename) |
| 67 | + local ltok, lorig, lval |
| 68 | + while ltok ~= "TK_EOS" do |
| 69 | + lline = llex.line |
| 70 | + ltok, lorig, lval = llex:lex() |
| 71 | + print(lline, ltok) |
| 72 | + lorig = lorig or "" |
| 73 | + print("'"..lorig.."'") |
| 74 | + lval = lval or "" |
| 75 | + print(lval) |
| 76 | + end |
| 77 | + -- INF closed by llex |
| 78 | +end |
| 79 | + |
| 80 | +----------------------------------------------------------------------- |
| 81 | +-- program entry point |
| 82 | +----------------------------------------------------------------------- |
| 83 | + |
| 84 | +-- token dump for visual inspection |
| 85 | +--[[ |
| 86 | +DumpTokens("LuaSrcDiet.lua") |
| 87 | +--]] |
| 88 | +-- try out no-whitespace pairs |
| 89 | +--[[ |
| 90 | +TestTokenPairs() |
| 91 | +--]] |
| 92 | + |
| 93 | +-- end of script |
0 commit comments