123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- print('testing scanner')
- local function dostring (x) return assert(loadstring(x))() end
- dostring("x = 'a\0a'")
- assert(x == 'a\0a' and string.len(x) == 3)
- -- escape sequences
- assert('\n\"\'\\' == [[
- "'\]])
- assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$"))
- -- assume ASCII just for tests:
- assert("\09912" == 'c12')
- assert("\99ab" == 'cab')
- assert("\099" == '\99')
- assert("\099\n" == 'c\10')
- assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo')
- assert(010 .. 020 .. -030 == "1020-30")
- -- long variable names
- var = string.rep('a', 15000)
- prog = string.format("%s = 5", var)
- dostring(prog)
- assert(_G[var] == 5)
- var = nil
- print('+')
- -- escapes --
- assert("\n\t" == [[
- ]])
- assert([[
- $debug]] == "\n $debug")
- assert([[ [ ]] ~= [[ ] ]])
- -- long strings --
- b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
- assert(string.len(b) == 960)
- prog = [=[
- print('+')
- a1 = [["isto e' um string com várias 'aspas'"]]
- a2 = "'aspas'"
- assert(string.find(a1, a2) == 31)
- print('+')
- a1 = [==[temp = [[um valor qualquer]]; ]==]
- assert(loadstring(a1))()
- assert(temp == 'um valor qualquer')
- -- long strings --
- b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
- assert(string.len(b) == 960)
- print('+')
- a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- 00123456789012345678901234567890123456789123456789012345678901234567890123456789
- ]]
- assert(string.len(a) == 1863)
- assert(string.sub(a, 1, 40) == string.sub(b, 1, 40))
- x = 1
- ]=]
- print('+')
- x = nil
- dostring(prog)
- assert(x)
- prog = nil
- a = nil
- b = nil
- -- testing line ends
- prog = [[
- a = 1 -- a comment
- b = 2
- x = [=[
- hi
- ]=]
- y = "\
- hello\r\n\
- "
- return debug.getinfo(1).currentline
- ]]
- for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
- local prog, nn = string.gsub(prog, "\n", n)
- assert(dostring(prog) == nn)
- assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
- end
- -- testing comments and strings with long brackets
- a = [==[]=]==]
- assert(a == "]=")
- a = [==[[===[[=[]]=][====[]]===]===]==]
- assert(a == "[===[[=[]]=][====[]]===]===")
- a = [====[[===[[=[]]=][====[]]===]===]====]
- assert(a == "[===[[=[]]=][====[]]===]===")
- a = [=[]]]]]]]]]=]
- assert(a == "]]]]]]]]")
- --[===[
- x y z [==[ blu foo
- ]==
- ]
- ]=]==]
- error error]=]===]
- -- generate all strings of four of these chars
- local x = {"=", "[", "]", "\n"}
- local len = 4
- local function gen (c, n)
- if n==0 then coroutine.yield(c)
- else
- for _, a in pairs(x) do
- gen(c..a, n-1)
- end
- end
- end
- for s in coroutine.wrap(function () gen("", len) end) do
- assert(s == loadstring("return [====[\n"..s.."]====]")())
- end
- -- testing decimal point locale
- if os.setlocale("pt_BR") or os.setlocale("ptb") then
- assert(tonumber("3,4") == 3.4 and tonumber"3.4" == nil)
- assert(assert(loadstring("return 3.4"))() == 3.4)
- assert(assert(loadstring("return .4,3"))() == .4)
- assert(assert(loadstring("return 4."))() == 4.)
- assert(assert(loadstring("return 4.+.5"))() == 4.5)
- local a,b = loadstring("return 4.5.")
- assert(string.find(b, "'4%.5%.'"))
- assert(os.setlocale("C"))
- else
- (Message or print)(
- '\a\n >>> pt_BR locale not available: skipping decimal point tests <<<\n\a')
- end
- print('OK')
|