literals.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. print('testing scanner')
  2. local function dostring (x) return assert(loadstring(x))() end
  3. dostring("x = 'a\0a'")
  4. assert(x == 'a\0a' and string.len(x) == 3)
  5. -- escape sequences
  6. assert('\n\"\'\\' == [[
  7. "'\]])
  8. assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$"))
  9. -- assume ASCII just for tests:
  10. assert("\09912" == 'c12')
  11. assert("\99ab" == 'cab')
  12. assert("\099" == '\99')
  13. assert("\099\n" == 'c\10')
  14. assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo')
  15. assert(010 .. 020 .. -030 == "1020-30")
  16. -- long variable names
  17. var = string.rep('a', 15000)
  18. prog = string.format("%s = 5", var)
  19. dostring(prog)
  20. assert(_G[var] == 5)
  21. var = nil
  22. print('+')
  23. -- escapes --
  24. assert("\n\t" == [[
  25. ]])
  26. assert([[
  27. $debug]] == "\n $debug")
  28. assert([[ [ ]] ~= [[ ] ]])
  29. -- long strings --
  30. b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
  31. assert(string.len(b) == 960)
  32. prog = [=[
  33. print('+')
  34. a1 = [["isto e' um string com várias 'aspas'"]]
  35. a2 = "'aspas'"
  36. assert(string.find(a1, a2) == 31)
  37. print('+')
  38. a1 = [==[temp = [[um valor qualquer]]; ]==]
  39. assert(loadstring(a1))()
  40. assert(temp == 'um valor qualquer')
  41. -- long strings --
  42. b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
  43. assert(string.len(b) == 960)
  44. print('+')
  45. a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
  46. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  47. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  48. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  49. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  50. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  51. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  52. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  53. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  54. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  55. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  56. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  57. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  58. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  59. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  60. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  61. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  62. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  63. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  64. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  65. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  66. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  67. 00123456789012345678901234567890123456789123456789012345678901234567890123456789
  68. ]]
  69. assert(string.len(a) == 1863)
  70. assert(string.sub(a, 1, 40) == string.sub(b, 1, 40))
  71. x = 1
  72. ]=]
  73. print('+')
  74. x = nil
  75. dostring(prog)
  76. assert(x)
  77. prog = nil
  78. a = nil
  79. b = nil
  80. -- testing line ends
  81. prog = [[
  82. a = 1 -- a comment
  83. b = 2
  84. x = [=[
  85. hi
  86. ]=]
  87. y = "\
  88. hello\r\n\
  89. "
  90. return debug.getinfo(1).currentline
  91. ]]
  92. for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
  93. local prog, nn = string.gsub(prog, "\n", n)
  94. assert(dostring(prog) == nn)
  95. assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
  96. end
  97. -- testing comments and strings with long brackets
  98. a = [==[]=]==]
  99. assert(a == "]=")
  100. a = [==[[===[[=[]]=][====[]]===]===]==]
  101. assert(a == "[===[[=[]]=][====[]]===]===")
  102. a = [====[[===[[=[]]=][====[]]===]===]====]
  103. assert(a == "[===[[=[]]=][====[]]===]===")
  104. a = [=[]]]]]]]]]=]
  105. assert(a == "]]]]]]]]")
  106. --[===[
  107. x y z [==[ blu foo
  108. ]==
  109. ]
  110. ]=]==]
  111. error error]=]===]
  112. -- generate all strings of four of these chars
  113. local x = {"=", "[", "]", "\n"}
  114. local len = 4
  115. local function gen (c, n)
  116. if n==0 then coroutine.yield(c)
  117. else
  118. for _, a in pairs(x) do
  119. gen(c..a, n-1)
  120. end
  121. end
  122. end
  123. for s in coroutine.wrap(function () gen("", len) end) do
  124. assert(s == loadstring("return [====[\n"..s.."]====]")())
  125. end
  126. -- testing decimal point locale
  127. if os.setlocale("pt_BR") or os.setlocale("ptb") then
  128. assert(tonumber("3,4") == 3.4 and tonumber"3.4" == nil)
  129. assert(assert(loadstring("return 3.4"))() == 3.4)
  130. assert(assert(loadstring("return .4,3"))() == .4)
  131. assert(assert(loadstring("return 4."))() == 4.)
  132. assert(assert(loadstring("return 4.+.5"))() == 4.5)
  133. local a,b = loadstring("return 4.5.")
  134. assert(string.find(b, "'4%.5%.'"))
  135. assert(os.setlocale("C"))
  136. else
  137. (Message or print)(
  138. '\a\n >>> pt_BR locale not available: skipping decimal point tests <<<\n\a')
  139. end
  140. print('OK')