1print('testing scanner') 2 3local function dostring (x) return assert(loadstring(x))() end 4 5dostring("x = 'a\0a'") 6assert(x == 'a\0a' and string.len(x) == 3) 7 8-- escape sequences 9assert('\n\"\'\\' == [[ 10 11"'\]]) 12 13assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$")) 14 15-- assume ASCII just for tests: 16assert("\09912" == 'c12') 17assert("\99ab" == 'cab') 18assert("\099" == '\99') 19assert("\099\n" == 'c\10') 20assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo') 21 22assert(010 .. 020 .. -030 == "1020-30") 23 24-- long variable names 25 26var = string.rep('a', 15000) 27prog = string.format("%s = 5", var) 28dostring(prog) 29assert(_G[var] == 5) 30var = nil 31print('+') 32 33-- escapes -- 34assert("\n\t" == [[ 35 36 ]]) 37assert([[ 38 39 $debug]] == "\n $debug") 40assert([[ [ ]] ~= [[ ] ]]) 41-- long strings -- 42b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" 43assert(string.len(b) == 960) 44prog = [=[ 45print('+') 46 47a1 = [["isto e' um string com v�rias 'aspas'"]] 48a2 = "'aspas'" 49 50assert(string.find(a1, a2) == 31) 51print('+') 52 53a1 = [==[temp = [[um valor qualquer]]; ]==] 54assert(loadstring(a1))() 55assert(temp == 'um valor qualquer') 56-- long strings -- 57b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" 58assert(string.len(b) == 960) 59print('+') 60 61a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 6200123456789012345678901234567890123456789123456789012345678901234567890123456789 6300123456789012345678901234567890123456789123456789012345678901234567890123456789 6400123456789012345678901234567890123456789123456789012345678901234567890123456789 6500123456789012345678901234567890123456789123456789012345678901234567890123456789 6600123456789012345678901234567890123456789123456789012345678901234567890123456789 6700123456789012345678901234567890123456789123456789012345678901234567890123456789 6800123456789012345678901234567890123456789123456789012345678901234567890123456789 6900123456789012345678901234567890123456789123456789012345678901234567890123456789 7000123456789012345678901234567890123456789123456789012345678901234567890123456789 7100123456789012345678901234567890123456789123456789012345678901234567890123456789 7200123456789012345678901234567890123456789123456789012345678901234567890123456789 7300123456789012345678901234567890123456789123456789012345678901234567890123456789 7400123456789012345678901234567890123456789123456789012345678901234567890123456789 7500123456789012345678901234567890123456789123456789012345678901234567890123456789 7600123456789012345678901234567890123456789123456789012345678901234567890123456789 7700123456789012345678901234567890123456789123456789012345678901234567890123456789 7800123456789012345678901234567890123456789123456789012345678901234567890123456789 7900123456789012345678901234567890123456789123456789012345678901234567890123456789 8000123456789012345678901234567890123456789123456789012345678901234567890123456789 8100123456789012345678901234567890123456789123456789012345678901234567890123456789 8200123456789012345678901234567890123456789123456789012345678901234567890123456789 8300123456789012345678901234567890123456789123456789012345678901234567890123456789 84]] 85assert(string.len(a) == 1863) 86assert(string.sub(a, 1, 40) == string.sub(b, 1, 40)) 87x = 1 88]=] 89 90print('+') 91x = nil 92dostring(prog) 93assert(x) 94 95prog = nil 96a = nil 97b = nil 98 99 100-- testing line ends 101prog = [[ 102a = 1 -- a comment 103b = 2 104 105 106x = [=[ 107hi 108]=] 109y = "\ 110hello\r\n\ 111" 112return debug.getinfo(1).currentline 113]] 114 115for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do 116 local prog, nn = string.gsub(prog, "\n", n) 117 assert(dostring(prog) == nn) 118 assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n") 119end 120 121 122-- testing comments and strings with long brackets 123a = [==[]=]==] 124assert(a == "]=") 125 126a = [==[[===[[=[]]=][====[]]===]===]==] 127assert(a == "[===[[=[]]=][====[]]===]===") 128 129a = [====[[===[[=[]]=][====[]]===]===]====] 130assert(a == "[===[[=[]]=][====[]]===]===") 131 132a = [=[]]]]]]]]]=] 133assert(a == "]]]]]]]]") 134 135 136--[===[ 137x y z [==[ blu foo 138]== 139] 140]=]==] 141error error]=]===] 142 143-- generate all strings of four of these chars 144local x = {"=", "[", "]", "\n"} 145local len = 4 146local function gen (c, n) 147 if n==0 then coroutine.yield(c) 148 else 149 for _, a in pairs(x) do 150 gen(c..a, n-1) 151 end 152 end 153end 154 155for s in coroutine.wrap(function () gen("", len) end) do 156 assert(s == loadstring("return [====[\n"..s.."]====]")()) 157end 158 159 160-- testing decimal point locale 161if os.setlocale("pt_BR") or os.setlocale("ptb") then 162 assert(tonumber("3,4") == 3.4 and tonumber"3.4" == nil) 163 assert(assert(loadstring("return 3.4"))() == 3.4) 164 assert(assert(loadstring("return .4,3"))() == .4) 165 assert(assert(loadstring("return 4."))() == 4.) 166 assert(assert(loadstring("return 4.+.5"))() == 4.5) 167 local a,b = loadstring("return 4.5.") 168 assert(string.find(b, "'4%.5%.'")) 169 assert(os.setlocale("C")) 170else 171 (Message or print)( 172 '\a\n >>> pt_BR locale not available: skipping decimal point tests <<<\n\a') 173end 174 175 176print('OK') 177