github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/testdata/lua-5.3.3-tests/literals.lua (about)

     1  -- $Id: literals.lua,v 1.35 2016/05/03 15:35:29 roberto Exp $
     2  
     3  print('testing scanner')
     4  
     5  local debug = require "debug"
     6  
     7  
     8  local function dostring (x) return assert(load(x), "")() end
     9  
    10  dostring("x \v\f = \t\r 'a\0a' \v\f\f")
    11  assert(x == 'a\0a' and string.len(x) == 3)
    12  
    13  -- escape sequences
    14  assert('\n\"\'\\' == [[
    15  
    16  "'\]])
    17  
    18  assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$"))
    19  
    20  -- assume ASCII just for tests:
    21  assert("\09912" == 'c12')
    22  assert("\99ab" == 'cab')
    23  assert("\099" == '\99')
    24  assert("\099\n" == 'c\10')
    25  assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo')
    26  
    27  assert(010 .. 020 .. -030 == "1020-30")
    28  
    29  -- hexadecimal escapes
    30  assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232")
    31  
    32  -- local function lexstring (x, y, n)
    33    -- local f = assert(load('return ' .. x ..
    34              -- ', require"debug".getinfo(1).currentline', ''))
    35    -- local s, l = f()
    36    -- assert(s == y and l == n)
    37  -- end
    38  
    39  -- lexstring("'abc\\z  \n   efg'", "abcefg", 2)
    40  -- lexstring("'abc\\z  \n\n\n'", "abc", 4)
    41  -- lexstring("'\\z  \n\t\f\v\n'",  "", 3)
    42  -- lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5)
    43  -- lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5)
    44  -- lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4)
    45  -- lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4)
    46  -- lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2)
    47  
    48  assert("abc\z
    49          def\z
    50          ghi\z
    51         " == 'abcdefghi')
    52  
    53  
    54  -- UTF-8 sequences
    55  assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0))
    56  
    57  -- limits for 1-byte sequences
    58  assert("\u{0}\u{7F}" == "\x00\z\x7F")
    59  
    60  -- limits for 2-byte sequences
    61  assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF")
    62  
    63  -- limits for 3-byte sequences
    64  assert("\u{800}\u{FFFF}" ==   "\xE0\xA0\x80\z\xEF\xBF\xBF")
    65  
    66  -- limits for 4-byte sequences
    67  assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF")
    68  
    69  
    70  -- Error in escape sequences
    71  local function lexerror (s, err)
    72    local st, msg = load('return ' .. s, '')
    73    if err ~= '<eof>' then err = err .. "'" end
    74    -- TODO or spec no need to guarantee that it returns the same error as clua's one.
    75    -- assert(not st and string.find(msg, "near .-" .. err))
    76    assert(not st and msg ~= "")
    77  end
    78  
    79  lexerror([["abc\x"]], [[\x"]])
    80  lexerror([["abc\x]], [[\x]])
    81  lexerror([["\x]], [[\x]])
    82  lexerror([["\x5"]], [[\x5"]])
    83  lexerror([["\x5]], [[\x5]])
    84  lexerror([["\xr"]], [[\xr]])
    85  lexerror([["\xr]], [[\xr]])
    86  lexerror([["\x.]], [[\x.]])
    87  lexerror([["\x8%"]], [[\x8%%]])
    88  lexerror([["\xAG]], [[\xAG]])
    89  lexerror([["\g"]], [[\g]])
    90  lexerror([["\g]], [[\g]])
    91  lexerror([["\."]], [[\%.]])
    92  
    93  lexerror([["\999"]], [[\999"]])
    94  lexerror([["xyz\300"]], [[\300"]])
    95  lexerror([["   \256"]], [[\256"]])
    96  
    97  -- errors in UTF-8 sequences
    98  lexerror([["abc\u{110000}"]], [[abc\u{110000]])   -- too large
    99  lexerror([["abc\u11r"]], [[abc\u1]])    -- missing '{'
   100  lexerror([["abc\u"]], [[abc\u"]])    -- missing '{'
   101  lexerror([["abc\u{11r"]], [[abc\u{11r]])    -- missing '}'
   102  lexerror([["abc\u{11"]], [[abc\u{11"]])    -- missing '}'
   103  lexerror([["abc\u{11]], [[abc\u{11]])    -- missing '}'
   104  lexerror([["abc\u{r"]], [[abc\u{r]])     -- no digits
   105  
   106  -- unfinished strings
   107  lexerror("[=[alo]]", "<eof>")
   108  lexerror("[=[alo]=", "<eof>")
   109  lexerror("[=[alo]", "<eof>")
   110  lexerror("'alo", "<eof>")
   111  lexerror("'alo \\z  \n\n", "<eof>")
   112  lexerror("'alo \\z", "<eof>")
   113  lexerror([['alo \98]], "<eof>")
   114  
   115  -- valid characters in variable names
   116  for i = 0, 255 do
   117    if i ~= 35 then -- TODO or spec initial '#' is handled specially
   118      local s = string.char(i)
   119      assert(not string.find(s, "[a-zA-Z_]") == not load(s .. "=1", ""))
   120      assert(not string.find(s, "[a-zA-Z_0-9]") ==
   121             not load("a" .. s .. "1 = 1", ""))
   122    end
   123  end
   124  
   125  
   126  -- long variable names
   127  
   128  var1 = string.rep('a', 15000) .. '1'
   129  var2 = string.rep('a', 15000) .. '2'
   130  prog = string.format([[
   131    %s = 5
   132    %s = %s + 1
   133    return function () return %s - %s end
   134  ]], var1, var2, var1, var1, var2)
   135  local f = dostring(prog)
   136  assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
   137  var1, var2, f = nil
   138  print('+')
   139  
   140  -- escapes --
   141  assert("\n\t" == [[
   142  
   143  	]])
   144  assert([[
   145  
   146   $debug]] == "\n $debug")
   147  assert([[ [ ]] ~= [[ ] ]])
   148  -- long strings --
   149  b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
   150  assert(string.len(b) == 960)
   151  prog = [=[
   152  print('+')
   153  
   154  a1 = [["this is a 'string' with several 'quotes'"]]
   155  a2 = "'quotes'"
   156  
   157  assert(string.find(a1, a2) == 34)
   158  print('+')
   159  
   160  a1 = [==[temp = [[an arbitrary value]]; ]==]
   161  assert(load(a1))()
   162  assert(temp == 'an arbitrary value')
   163  -- long strings --
   164  b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
   165  assert(string.len(b) == 960)
   166  print('+')
   167  
   168  a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
   169  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   170  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   171  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   172  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   173  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   174  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   175  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   176  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   177  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   178  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   179  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   180  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   181  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   182  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   183  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   184  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   185  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   186  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   187  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   188  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   189  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   190  00123456789012345678901234567890123456789123456789012345678901234567890123456789
   191  ]]
   192  assert(string.len(a) == 1863)
   193  assert(string.sub(a, 1, 40) == string.sub(b, 1, 40))
   194  x = 1
   195  ]=]
   196  
   197  print('+')
   198  x = nil
   199  dostring(prog)
   200  assert(x)
   201  
   202  prog = nil
   203  a = nil
   204  b = nil
   205  
   206  
   207  -- testing line ends
   208  prog = [[
   209  a = 1        -- a comment
   210  b = 2
   211  
   212  
   213  x = [=[
   214  hi
   215  ]=]
   216  y = "\
   217  hello\r\n\
   218  "
   219  return require"debug".getinfo(1).currentline
   220  ]]
   221  
   222  -- TODO or support no plan to support "\r", "\n\r" as line break
   223  -- for i, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
   224  for i, n in pairs{"\n", "\r\n"} do
   225    local prog, nn = string.gsub(prog, "\n", n)
   226    assert(dostring(prog) == nn)
   227    assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
   228  end
   229  
   230  
   231  -- testing comments and strings with long brackets
   232  a = [==[]=]==]
   233  assert(a == "]=")
   234  
   235  a = [==[[===[[=[]]=][====[]]===]===]==]
   236  assert(a == "[===[[=[]]=][====[]]===]===")
   237  
   238  a = [====[[===[[=[]]=][====[]]===]===]====]
   239  assert(a == "[===[[=[]]=][====[]]===]===")
   240  
   241  a = [=[]]]]]]]]]=]
   242  assert(a == "]]]]]]]]")
   243  
   244  
   245  --[===[
   246  x y z [==[ blu foo
   247  ]==
   248  ]
   249  ]=]==]
   250  error error]=]===]
   251  
   252  -- generate all strings of four of these chars
   253  local x = {"=", "[", "]", "\n"}
   254  local len = 4
   255  local function gen (c, n)
   256    if n==0 then coroutine.yield(c)
   257    else
   258      for _, a in pairs(x) do
   259        gen(c..a, n-1)
   260      end
   261    end
   262  end
   263  
   264  for s in coroutine.wrap(function () gen("", len) end) do
   265    assert(s == load("return [====[\n"..s.."]====]", "")())
   266  end
   267  
   268  
   269  -- TODO or spec setlocale is not implemented
   270  -- testing decimal point locale
   271  -- if os.setlocale("pt_BR") or os.setlocale("ptb") then
   272    -- assert(tonumber("3,4") == 3.4 and tonumber"3.4" == 3.4)
   273    -- assert(tonumber("  -.4  ") == -0.4)
   274    -- assert(tonumber("  +0x.41  ") == 0X0.41)
   275    -- assert(not load("a = (3,4)"))
   276    -- assert(assert(load("return 3.4"))() == 3.4)
   277    -- assert(assert(load("return .4,3"))() == .4)
   278    -- assert(assert(load("return 4."))() == 4.)
   279    -- assert(assert(load("return 4.+.5"))() == 4.5)
   280  
   281    -- assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1)
   282  
   283    -- assert(tonumber"inf" == nil and tonumber"NAN" == nil)
   284  
   285    -- assert(assert(load(string.format("return %q", 4.51)))() == 4.51)
   286  
   287    -- local a,b = load("return 4.5.")
   288    -- assert(string.find(b, "'4%.5%.'"))
   289  
   290    -- assert(os.setlocale("C"))
   291  -- else
   292    -- (Message or print)(
   293     -- '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n')
   294  -- end
   295  
   296  
   297  -- testing %q x line ends
   298  local s = "a string with \r and \n and \r\n and \n\r"
   299  local c = string.format("return %q", s)
   300  assert(assert(load(c))() == s)
   301  
   302  -- testing errors
   303  assert(not load"a = 'non-ending string")
   304  assert(not load"a = 'non-ending string\n'")
   305  assert(not load"a = '\\345'")
   306  assert(not load"a = [=x]")
   307  
   308  print('OK')