aboutsummaryrefslogtreecommitdiff
path: root/testes/errors.lua
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:34:11 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-12-28 18:34:11 -0300
commit314745ed8438d1276c6c928d5f9d4be018dfadb6 (patch)
tree594b7e873f2c29113d95c75147ab10865cdd772c /testes/errors.lua
parent0825cf237d9d3505155f8b40bcf83ea1b135e8da (diff)
downloadlua-314745ed8438d1276c6c928d5f9d4be018dfadb6.tar.gz
lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.tar.bz2
lua-314745ed8438d1276c6c928d5f9d4be018dfadb6.zip
Avoid excessive name pollution in test files
Test files are more polite regarding the use of globals when locals would do, and when globals are necessary deleting them after use.
Diffstat (limited to 'testes/errors.lua')
-rw-r--r--testes/errors.lua83
1 files changed, 47 insertions, 36 deletions
diff --git a/testes/errors.lua b/testes/errors.lua
index 55bdab82..cf0ab526 100644
--- a/testes/errors.lua
+++ b/testes/errors.lua
@@ -114,12 +114,14 @@ checkmessage("a = {} | 1", "bitwise operation")
114checkmessage("a = {} < 1", "attempt to compare") 114checkmessage("a = {} < 1", "attempt to compare")
115checkmessage("a = {} <= 1", "attempt to compare") 115checkmessage("a = {} <= 1", "attempt to compare")
116 116
117checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") 117checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'")
118checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") 118checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'")
119checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") 119checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
120assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) 120assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
121checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") 121checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
122checkmessage("a=(1)..{}", "a table value") 122checkmessage("aaa=(1)..{}", "a table value")
123
124_G.aaa, _G.bbbb = nil
123 125
124-- calls 126-- calls
125checkmessage("local a; a(13)", "local 'a'") 127checkmessage("local a; a(13)", "local 'a'")
@@ -134,12 +136,13 @@ checkmessage([[
134 136
135-- tail calls 137-- tail calls
136checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") 138checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
137checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'") 139checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'")
140
141checkmessage("aaa = #print", "length of a function value")
142checkmessage("aaa = #3", "length of a number value")
138 143
139checkmessage("a = #print", "length of a function value") 144_G.aaa = nil
140checkmessage("a = #3", "length of a number value")
141 145
142aaa = nil
143checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") 146checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
144checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") 147checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
145checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") 148checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
@@ -152,15 +155,16 @@ checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
152 155
153checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") 156checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
154 157
155checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'") 158checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'")
156checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") 159checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
157checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'") 160checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'")
158checkmessage("aaa={}; x=-aaa", "global 'aaa'") 161checkmessage("aaa={}; x=-aaa", "global 'aaa'")
159 162
160-- short circuit 163-- short circuit
161checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", 164checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)",
162 "local 'bbbb'") 165 "local 'bbbb'")
163checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") 166checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)",
167 "local 'bbbb'")
164checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") 168checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
165checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") 169checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
166assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) 170assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
@@ -187,8 +191,8 @@ checkmessage("return ~-3e40", "has no integer representation")
187checkmessage("return ~-3.009", "has no integer representation") 191checkmessage("return ~-3.009", "has no integer representation")
188checkmessage("return 3.009 & 1", "has no integer representation") 192checkmessage("return 3.009 & 1", "has no integer representation")
189checkmessage("return 34 >> {}", "table value") 193checkmessage("return 34 >> {}", "table value")
190checkmessage("a = 24 // 0", "divide by zero") 194checkmessage("aaa = 24 // 0", "divide by zero")
191checkmessage("a = 1 % 0", "'n%0'") 195checkmessage("aaa = 1 % 0", "'n%0'")
192 196
193 197
194-- type error for an object which is neither in an upvalue nor a register. 198-- type error for an object which is neither in an upvalue nor a register.
@@ -269,13 +273,13 @@ end
269-- tests for field accesses after RK limit 273-- tests for field accesses after RK limit
270local t = {} 274local t = {}
271for i = 1, 1000 do 275for i = 1, 1000 do
272 t[i] = "a = x" .. i 276 t[i] = "aaa = x" .. i
273end 277end
274local s = table.concat(t, "; ") 278local s = table.concat(t, "; ")
275t = nil 279t = nil
276checkmessage(s.."; a = bbb + 1", "global 'bbb'") 280checkmessage(s.."; aaa = bbb + 1", "global 'bbb'")
277checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'") 281checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'")
278checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'") 282checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'")
279checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") 283checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
280 284
281checkmessage([[aaa=9 285checkmessage([[aaa=9
@@ -324,14 +328,17 @@ main()
324]], "global 'NoSuchName'") 328]], "global 'NoSuchName'")
325print'+' 329print'+'
326 330
327a = {}; setmetatable(a, {__index = string}) 331aaa = {}; setmetatable(aaa, {__index = string})
328checkmessage("a:sub()", "bad self") 332checkmessage("aaa:sub()", "bad self")
329checkmessage("string.sub('a', {})", "#2") 333checkmessage("string.sub('a', {})", "#2")
330checkmessage("('a'):sub{}", "#1") 334checkmessage("('a'):sub{}", "#1")
331 335
332checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") 336checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
333checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") 337checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
334 338
339_G.aaa = nil
340
341
335-- tests for errors in coroutines 342-- tests for errors in coroutines
336 343
337local function f (n) 344local function f (n)
@@ -349,7 +356,7 @@ checkerr("yield across", f)
349 356
350-- testing size of 'source' info; size of buffer for that info is 357-- testing size of 'source' info; size of buffer for that info is
351-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'. 358-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
352idsize = 60 - 1 359local idsize = 60 - 1
353local function checksize (source) 360local function checksize (source)
354 -- syntax error 361 -- syntax error
355 local _, msg = load("x", source) 362 local _, msg = load("x", source)
@@ -411,13 +418,14 @@ x
411 418
412local p = [[ 419local p = [[
413 function g() f() end 420 function g() f() end
414 function f(x) error('a', X) end 421 function f(x) error('a', XX) end
415g() 422g()
416]] 423]]
417X=3;lineerror((p), 3) 424XX=3;lineerror((p), 3)
418X=0;lineerror((p), false) 425XX=0;lineerror((p), false)
419X=1;lineerror((p), 2) 426XX=1;lineerror((p), 2)
420X=2;lineerror((p), 1) 427XX=2;lineerror((p), 1)
428_G.XX, _G.g, _G.f = nil
421 429
422 430
423lineerror([[ 431lineerror([[
@@ -449,11 +457,11 @@ if not _soft then
449 -- several tests that exaust the Lua stack 457 -- several tests that exaust the Lua stack
450 collectgarbage() 458 collectgarbage()
451 print"testing stack overflow" 459 print"testing stack overflow"
452 C = 0 460 local C = 0
453 -- get line where stack overflow will happen 461 -- get line where stack overflow will happen
454 local l = debug.getinfo(1, "l").currentline + 1 462 local l = debug.getinfo(1, "l").currentline + 1
455 local function auxy () C=C+1; auxy() end -- produce a stack overflow 463 local function auxy () C=C+1; auxy() end -- produce a stack overflow
456 function y () 464 function YY ()
457 collectgarbage("stop") -- avoid running finalizers without stack space 465 collectgarbage("stop") -- avoid running finalizers without stack space
458 auxy() 466 auxy()
459 collectgarbage("restart") 467 collectgarbage("restart")
@@ -465,9 +473,11 @@ if not _soft then
465 return (string.find(m, "stack overflow")) 473 return (string.find(m, "stack overflow"))
466 end 474 end
467 -- repeated stack overflows (to check stack recovery) 475 -- repeated stack overflows (to check stack recovery)
468 assert(checkstackmessage(doit('y()'))) 476 assert(checkstackmessage(doit('YY()')))
469 assert(checkstackmessage(doit('y()'))) 477 assert(checkstackmessage(doit('YY()')))
470 assert(checkstackmessage(doit('y()'))) 478 assert(checkstackmessage(doit('YY()')))
479
480 _G.YY = nil
471 481
472 482
473 -- error lines in stack overflow 483 -- error lines in stack overflow
@@ -561,7 +571,7 @@ do
561end 571end
562 572
563-- xpcall with arguments 573-- xpcall with arguments
564a, b, c = xpcall(string.find, error, "alo", "al") 574local a, b, c = xpcall(string.find, error, "alo", "al")
565assert(a and b == 1 and c == 2) 575assert(a and b == 1 and c == 2)
566a, b, c = xpcall(string.find, function (x) return {} end, true, "al") 576a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
567assert(not a and type(b) == "table" and c == nil) 577assert(not a and type(b) == "table" and c == nil)
@@ -581,11 +591,12 @@ checksyntax("a\1a = 1", "", "<\\1>", 1)
581-- test 255 as first char in a chunk 591-- test 255 as first char in a chunk
582checksyntax("\255a = 1", "", "<\\255>", 1) 592checksyntax("\255a = 1", "", "<\\255>", 1)
583 593
584doit('I = load("a=9+"); a=3') 594doit('I = load("a=9+"); aaa=3')
585assert(a==3 and not I) 595assert(_G.aaa==3 and not _G.I)
596_G.I,_G.aaa = nil
586print('+') 597print('+')
587 598
588lim = 1000 599local lim = 1000
589if _soft then lim = 100 end 600if _soft then lim = 100 end
590for i=1,lim do 601for i=1,lim do
591 doit('a = ') 602 doit('a = ')