github.com/xmx/lua@v0.0.0-20230324063450-8a298e091302/_glua-tests/issues.lua (about) 1 2 -- issue #10 3 local function inspect(options) 4 options = options or {} 5 return type(options) 6 end 7 assert(inspect(nil) == "table") 8 9 local function inspect(options) 10 options = options or setmetatable({}, {__mode = "test"}) 11 return type(options) 12 end 13 assert(inspect(nil) == "table") 14 15 -- issue #16 16 local ok, msg = pcall(function() 17 local a = {} 18 a[nil] = 1 19 end) 20 assert(not ok and string.find(msg, "table index is nil", 1, true)) 21 22 -- issue #19 23 local tbl = {1,2,3,4,5} 24 assert(#tbl == 5) 25 assert(table.remove(tbl) == 5) 26 assert(#tbl == 4) 27 assert(table.remove(tbl, 3) == 3) 28 assert(#tbl == 3) 29 30 -- issue #24 31 local tbl = {string.find('hello.world', '.', 0)} 32 assert(tbl[1] == 1 and tbl[2] == 1) 33 assert(string.sub('hello.world', 0, 2) == "he") 34 35 -- issue 33 36 local a,b 37 a = function () 38 pcall(function() 39 end) 40 coroutine.yield("a") 41 return b() 42 end 43 44 b = function () 45 return "b" 46 end 47 48 local co = coroutine.create(a) 49 assert(select(2, coroutine.resume(co)) == "a") 50 assert(select(2, coroutine.resume(co)) == "b") 51 assert(coroutine.status(co) == "dead") 52 53 -- issue 37 54 function test(a, b, c) 55 b = b or string.format("b%s", a) 56 c = c or string.format("c%s", a) 57 assert(a == "test") 58 assert(b == "btest") 59 assert(c == "ctest") 60 end 61 test("test") 62 63 -- issue 39 64 assert(string.match("あいうえお", ".*あ.*") == "あいうえお") 65 assert(string.match("あいうえお", "あいうえお") == "あいうえお") 66 67 -- issue 47 68 assert(string.gsub("A\nA", ".", "A") == "AAA") 69 70 -- issue 62 71 local function level4() error("error!") end 72 local function level3() level4() end 73 local function level2() level3() end 74 local function level1() level2() end 75 local ok, result = xpcall(level1, function(err) 76 return debug.traceback("msg", 10) 77 end) 78 assert(result == [[msg 79 stack traceback:]]) 80 ok, result = xpcall(level1, function(err) 81 return debug.traceback("msg", 9) 82 end) 83 assert(result == string.gsub([[msg 84 stack traceback: 85 @TAB@[G]: ?]], "@TAB@", "\t")) 86 local ok, result = xpcall(level1, function(err) 87 return debug.traceback("msg", 0) 88 end) 89 90 assert(result == string.gsub([[msg 91 stack traceback: 92 @TAB@[G]: in function 'traceback' 93 @TAB@issues.lua:87: in function <issues.lua:86> 94 @TAB@[G]: in function 'error' 95 @TAB@issues.lua:71: in function 'level4' 96 @TAB@issues.lua:72: in function 'level3' 97 @TAB@issues.lua:73: in function 'level2' 98 @TAB@issues.lua:74: in function <issues.lua:74> 99 @TAB@[G]: in function 'xpcall' 100 @TAB@issues.lua:86: in main chunk 101 @TAB@[G]: ?]], "@TAB@", "\t")) 102 103 local ok, result = xpcall(level1, function(err) 104 return debug.traceback("msg", 3) 105 end) 106 107 assert(result == string.gsub([[msg 108 stack traceback: 109 @TAB@issues.lua:71: in function 'level4' 110 @TAB@issues.lua:72: in function 'level3' 111 @TAB@issues.lua:73: in function 'level2' 112 @TAB@issues.lua:74: in function <issues.lua:74> 113 @TAB@[G]: in function 'xpcall' 114 @TAB@issues.lua:103: in main chunk 115 @TAB@[G]: ?]], "@TAB@", "\t")) 116 117 -- issue 81 118 local tbl = { 119 [-1] = "a", 120 [0] = "b", 121 [1] = "c", 122 } 123 local a, b = next(tbl, nil) 124 assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c") 125 local a, b = next(tbl, a) 126 assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c") 127 local a, b = next(tbl, a) 128 assert( a == -1 and b == "a" or a == 0 and b == "b" or a == 1 and b == "c") 129 local a, b = next(tbl, a) 130 assert( a == nil and b == nil) 131 132 local tbl = {'a', 'b'} 133 local a, b = next(tbl, nil) 134 assert(a == 1 and b == "a") 135 local a, b = next(tbl, a) 136 assert(a == 2 and b == "b") 137 local a, b = next(tbl, a) 138 assert(a == nil and b == nil) 139 140 -- issue 82 141 local cr = function() 142 return coroutine.wrap(function() 143 coroutine.yield(1, "a") 144 coroutine.yield(2, "b") 145 end) 146 end 147 148 local f = cr() 149 local a, b = f() 150 assert(a == 1 and b == "a") 151 local a, b = f() 152 assert(a == 2 and b == "b") 153 154 -- issue 91, 92 155 local url = "www.aaa.bbb_abc123-321-cba_abc123" 156 assert(string.match(url, ".-([%w-]*)[.]*") == "www") 157 158 local s = "hello.world" 159 assert(s:match("([^.]+).world") == "hello") 160 161 local s = "hello-world" 162 assert(s:match("([^-]+)-world") == "hello") 163 164 -- issue 93 165 local t = {} 166 local ok, msg = pcall(function() t.notfound() end) 167 assert(not ok and string.find(msg, "attempt to call a non-function object", 1, true)) 168 169 -- issue 150 170 local util = { 171 fn = function() end 172 } 173 local b 174 local x = util.fn( 175 1, 176 (b or {}).x) 177 178 local s = [=[["a"]['b'][9] - ["a"]['b'][8] > ]=] 179 local result = {} 180 for i in s:gmatch([=[[[][^%s,]*[]]]=]) do 181 table.insert(result, i) 182 end 183 assert(result[1] == [=[["a"]['b'][9]]=]) 184 assert(result[2] == [=[["a"]['b'][8]]=]) 185 186 -- issue 168 187 local expected = 1 188 189 local result = math.random(1) 190 191 assert(result == expected) 192 193 -- issue 202 194 local t = {} 195 ok, res = pcall(table.remove, t) 196 if not ok or not res then 197 table.insert(t, {}) 198 else 199 assert(false) 200 end 201 ok, res = pcall(table.remove, t) 202 ok, res = pcall(table.remove, t) 203 assert(not ok or not res) 204 205 -- issue 204 206 local ok, message = pcall(nil) 207 assert(not ok) 208 assert(message == "attempt to call a nil value") 209 210 local ok, message = pcall(1) 211 assert(not ok) 212 assert(message == "attempt to call a number value") 213 214 ok, message = pcall(function() 215 pcall() 216 end) 217 assert(not ok and string.find(message, "bad argument #1 to pcall", 1, true)) 218 219 -- issue 216 220 local function bar() 221 return "bar" 222 end 223 224 local function test(foo) 225 local should_not_change 226 foo = foo or bar() 227 print(should_not_change) 228 return should_not_change 229 end 230 231 assert(test(nil) == nil) 232 233 -- issue 220 234 function test() 235 function f(v) 236 return v 237 end 238 local tbl = {y=0} 239 local a,b 240 a, b = f(10), f(20) 241 assert(tbl.y == 0) 242 end 243 test() 244 245 -- issue 222 246 function test() 247 local m = {n=2} 248 249 function m:f1() 250 return self:f3() >= self.n 251 end 252 253 function m:f2() 254 local v1, v2, v3 = m:f1() 255 assert(v1 == true) 256 assert(v2 == nil) 257 assert(v3 == nil) 258 end 259 260 function m:f3() 261 return 3 262 end 263 264 m:f2() 265 end 266 test() 267 268 -- issue #292 269 function test() 270 t0 = {} 271 t0.year = 2006 272 t0.month = 1 273 t0.day = 2 274 t0.hour = 15 275 t0.min = 4 276 t0.sec = 5 277 278 t1 = {} 279 t1.year = "2006" 280 t1.month = "1" 281 t1.day = "2" 282 t1.hour = "15" 283 t1.min = "4" 284 t1.sec = "5" 285 286 assert(os.time(t0) == os.time(t1)) 287 288 t2 = {} 289 t2.year = " 2006"--prefix blank space 290 t2.month = "1" 291 t2.day = "2" 292 t2.hour = "15" 293 t2.min = "4" 294 t2.sec = "5" 295 assert(os.time(t0) == os.time(t2)) 296 297 t3 = {} 298 t3.year = " 0002006"--prefix blank space and 0 299 t3.month = "1" 300 t3.day = "2" 301 t3.hour = "15" 302 t3.min = "4" 303 t3.sec = "5" 304 assert(os.time(t1) == os.time(t3)) 305 306 t4 = {} 307 t4.year = "0002006"--prefix 0 308 t4.month = "1" 309 t4.day = "2" 310 t4.hour = "15" 311 t4.min = "4" 312 t4.sec = "5" 313 assert(os.time(t1) == os.time(t4)) 314 315 t5 = {} 316 t5.year = "0x7d6"--prefix 0x 317 t5.month = "1" 318 t5.day = "2" 319 t5.hour = "15" 320 t5.min = "4" 321 t5.sec = "5" 322 assert(os.time(t1) == os.time(t5)) 323 324 t6 = {} 325 t6.year = "0X7d6"--prefix 0X 326 t6.month = "1" 327 t6.day = "2" 328 t6.hour = "15" 329 t6.min = "4" 330 t6.sec = "5" 331 assert(os.time(t1) == os.time(t6)) 332 end 333 test() 334 335 --issue #331 336 function test() 337 local select_a = function() 338 return select(3, "1") 339 end 340 assert(true == pcall(select_a)) 341 local select_b = function() 342 return select(0) 343 end 344 assert(false == pcall(select_b)) 345 local select_c = function() 346 return select(1/9) 347 end 348 assert(false == pcall(select_c)) 349 local select_d = function() 350 return select(1, "a") 351 end 352 assert("a" == select_d()) 353 local select_e = function() 354 return select(3, "a", "b", "c") 355 end 356 assert("c" == select_e()) 357 local select_f = function() 358 return select(0)(select(1/9)) 359 end 360 assert(false == pcall(select_f)) 361 end 362 test() 363 364 -- issue #363 365 -- Any expression enclosed in parentheses always results in only one value. 366 function test() 367 function ret2(a, b) 368 return a, b 369 end 370 function enclosed_ret() 371 return (ret2(1, 2)) 372 end 373 local a,b = enclosed_ret() 374 assert(a == 1 and b == nil) 375 376 function enclosed_vararg_ret(...) 377 return (...) 378 end 379 local a,b,c=enclosed_vararg_ret(1, 2, 3) 380 assert(a == 1 and b == nil and c == nil) 381 382 function enclosed_vararg_assign(...) 383 local a,b,c = (...) 384 return a,b,c 385 end 386 local a,b,c=enclosed_vararg_assign(1, 2, 3) 387 assert(a == 1 and b == nil and c == nil) 388 end 389 test() 390 391 -- issue #412 392 -- issue #418 393 -- Conversion from symmetric modulo is incorrect. 394 function test() 395 assert(-2 % -2 == 0) 396 assert(-1 % -2 == -1) 397 assert(0 % -2 == 0) 398 assert(1 % -2 == -1) 399 assert(2 % -2 == 0) 400 assert(-2 % 2 == 0) 401 assert(-1 % 2 == 1) 402 assert(0 % 2 == 0) 403 assert(1 % 2 == 1) 404 assert(2 % 2 == 0) 405 end 406 test() 407 408 -- issue #355 409 function test() 410 local x = "valid" 411 assert(x == "valid") 412 assert(zzz == nil) 413 x = zzz and "not-valid" or x 414 assert(x == "valid") 415 end 416 test() 417 418 function test() 419 local x = "valid" 420 local z = nil 421 assert(x == "valid") 422 assert(z == nil) 423 x = z and "not-valid" or x 424 assert(x == "valid") 425 end 426 test() 427 428 function test() 429 local x = "valid" 430 assert(x == "valid") 431 assert(zzz == nil) 432 x = zzz and "not-valid" or "still " .. x 433 assert(x == "still valid") 434 end 435 test() 436 437 -- issue #315 438 function test() 439 local a = {} 440 local d = 'e' 441 local f = 1 442 443 f, a.d = f, d 444 445 assert(f..", "..a.d == "1, e") 446 end 447 test() 448 449 -- issue #423 450 function test() 451 local a, b, c = "1", "3", "1" 452 a, b, c= tonumber(a), tonumber(b) or a, tonumber(c) 453 assert(a == 1) 454 assert(type(a) == "number") 455 assert(b == 3) 456 assert(type(b) == "number") 457 assert(c == 1) 458 assert(type(c) == "number") 459 end