diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:34:11 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-28 18:34:11 -0300 |
commit | 314745ed8438d1276c6c928d5f9d4be018dfadb6 (patch) | |
tree | 594b7e873f2c29113d95c75147ab10865cdd772c /testes/errors.lua | |
parent | 0825cf237d9d3505155f8b40bcf83ea1b135e8da (diff) | |
download | lua-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.lua | 83 |
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") | |||
114 | checkmessage("a = {} < 1", "attempt to compare") | 114 | checkmessage("a = {} < 1", "attempt to compare") |
115 | checkmessage("a = {} <= 1", "attempt to compare") | 115 | checkmessage("a = {} <= 1", "attempt to compare") |
116 | 116 | ||
117 | checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") | 117 | checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'") |
118 | checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") | 118 | checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'") |
119 | checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") | 119 | checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") |
120 | assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) | 120 | assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'")) |
121 | checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") | 121 | checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number") |
122 | checkmessage("a=(1)..{}", "a table value") | 122 | checkmessage("aaa=(1)..{}", "a table value") |
123 | |||
124 | _G.aaa, _G.bbbb = nil | ||
123 | 125 | ||
124 | -- calls | 126 | -- calls |
125 | checkmessage("local a; a(13)", "local 'a'") | 127 | checkmessage("local a; a(13)", "local 'a'") |
@@ -134,12 +136,13 @@ checkmessage([[ | |||
134 | 136 | ||
135 | -- tail calls | 137 | -- tail calls |
136 | checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") | 138 | checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") |
137 | checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'") | 139 | checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'") |
140 | |||
141 | checkmessage("aaa = #print", "length of a function value") | ||
142 | checkmessage("aaa = #3", "length of a number value") | ||
138 | 143 | ||
139 | checkmessage("a = #print", "length of a function value") | 144 | _G.aaa = nil |
140 | checkmessage("a = #3", "length of a number value") | ||
141 | 145 | ||
142 | aaa = nil | ||
143 | checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") | 146 | checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") |
144 | checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") | 147 | checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") |
145 | checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") | 148 | checkmessage("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 | ||
153 | checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") | 156 | checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") |
154 | 157 | ||
155 | checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'") | 158 | checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'") |
156 | checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") | 159 | checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") |
157 | checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'") | 160 | checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'") |
158 | checkmessage("aaa={}; x=-aaa", "global 'aaa'") | 161 | checkmessage("aaa={}; x=-aaa", "global 'aaa'") |
159 | 162 | ||
160 | -- short circuit | 163 | -- short circuit |
161 | checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", | 164 | checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)", |
162 | "local 'bbbb'") | 165 | "local 'bbbb'") |
163 | checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") | 166 | checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)", |
167 | "local 'bbbb'") | ||
164 | checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") | 168 | checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") |
165 | checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") | 169 | checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") |
166 | assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) | 170 | assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) |
@@ -187,8 +191,8 @@ checkmessage("return ~-3e40", "has no integer representation") | |||
187 | checkmessage("return ~-3.009", "has no integer representation") | 191 | checkmessage("return ~-3.009", "has no integer representation") |
188 | checkmessage("return 3.009 & 1", "has no integer representation") | 192 | checkmessage("return 3.009 & 1", "has no integer representation") |
189 | checkmessage("return 34 >> {}", "table value") | 193 | checkmessage("return 34 >> {}", "table value") |
190 | checkmessage("a = 24 // 0", "divide by zero") | 194 | checkmessage("aaa = 24 // 0", "divide by zero") |
191 | checkmessage("a = 1 % 0", "'n%0'") | 195 | checkmessage("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 |
270 | local t = {} | 274 | local t = {} |
271 | for i = 1, 1000 do | 275 | for i = 1, 1000 do |
272 | t[i] = "a = x" .. i | 276 | t[i] = "aaa = x" .. i |
273 | end | 277 | end |
274 | local s = table.concat(t, "; ") | 278 | local s = table.concat(t, "; ") |
275 | t = nil | 279 | t = nil |
276 | checkmessage(s.."; a = bbb + 1", "global 'bbb'") | 280 | checkmessage(s.."; aaa = bbb + 1", "global 'bbb'") |
277 | checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'") | 281 | checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'") |
278 | checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'") | 282 | checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'") |
279 | checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") | 283 | checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") |
280 | 284 | ||
281 | checkmessage([[aaa=9 | 285 | checkmessage([[aaa=9 |
@@ -324,14 +328,17 @@ main() | |||
324 | ]], "global 'NoSuchName'") | 328 | ]], "global 'NoSuchName'") |
325 | print'+' | 329 | print'+' |
326 | 330 | ||
327 | a = {}; setmetatable(a, {__index = string}) | 331 | aaa = {}; setmetatable(aaa, {__index = string}) |
328 | checkmessage("a:sub()", "bad self") | 332 | checkmessage("aaa:sub()", "bad self") |
329 | checkmessage("string.sub('a', {})", "#2") | 333 | checkmessage("string.sub('a', {})", "#2") |
330 | checkmessage("('a'):sub{}", "#1") | 334 | checkmessage("('a'):sub{}", "#1") |
331 | 335 | ||
332 | checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") | 336 | checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") |
333 | checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") | 337 | checkmessage("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 | ||
337 | local function f (n) | 344 | local 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'. |
352 | idsize = 60 - 1 | 359 | local idsize = 60 - 1 |
353 | local function checksize (source) | 360 | local 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 | ||
412 | local p = [[ | 419 | local 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 |
415 | g() | 422 | g() |
416 | ]] | 423 | ]] |
417 | X=3;lineerror((p), 3) | 424 | XX=3;lineerror((p), 3) |
418 | X=0;lineerror((p), false) | 425 | XX=0;lineerror((p), false) |
419 | X=1;lineerror((p), 2) | 426 | XX=1;lineerror((p), 2) |
420 | X=2;lineerror((p), 1) | 427 | XX=2;lineerror((p), 1) |
428 | _G.XX, _G.g, _G.f = nil | ||
421 | 429 | ||
422 | 430 | ||
423 | lineerror([[ | 431 | lineerror([[ |
@@ -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 | |||
561 | end | 571 | end |
562 | 572 | ||
563 | -- xpcall with arguments | 573 | -- xpcall with arguments |
564 | a, b, c = xpcall(string.find, error, "alo", "al") | 574 | local a, b, c = xpcall(string.find, error, "alo", "al") |
565 | assert(a and b == 1 and c == 2) | 575 | assert(a and b == 1 and c == 2) |
566 | a, b, c = xpcall(string.find, function (x) return {} end, true, "al") | 576 | a, b, c = xpcall(string.find, function (x) return {} end, true, "al") |
567 | assert(not a and type(b) == "table" and c == nil) | 577 | assert(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 |
582 | checksyntax("\255a = 1", "", "<\\255>", 1) | 592 | checksyntax("\255a = 1", "", "<\\255>", 1) |
583 | 593 | ||
584 | doit('I = load("a=9+"); a=3') | 594 | doit('I = load("a=9+"); aaa=3') |
585 | assert(a==3 and not I) | 595 | assert(_G.aaa==3 and not _G.I) |
596 | _G.I,_G.aaa = nil | ||
586 | print('+') | 597 | print('+') |
587 | 598 | ||
588 | lim = 1000 | 599 | local lim = 1000 |
589 | if _soft then lim = 100 end | 600 | if _soft then lim = 100 end |
590 | for i=1,lim do | 601 | for i=1,lim do |
591 | doit('a = ') | 602 | doit('a = ') |