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/db.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/db.lua')
-rw-r--r-- | testes/db.lua | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/testes/db.lua b/testes/db.lua index f891e9b8..02b96aca 100644 --- a/testes/db.lua +++ b/testes/db.lua | |||
@@ -16,7 +16,7 @@ end | |||
16 | assert(not debug.gethook()) | 16 | assert(not debug.gethook()) |
17 | 17 | ||
18 | local testline = 19 -- line where 'test' is defined | 18 | local testline = 19 -- line where 'test' is defined |
19 | function test (s, l, p) -- this must be line 19 | 19 | local function test (s, l, p) -- this must be line 19 |
20 | collectgarbage() -- avoid gc during trace | 20 | collectgarbage() -- avoid gc during trace |
21 | local function f (event, line) | 21 | local function f (event, line) |
22 | assert(event == 'line') | 22 | assert(event == 'line') |
@@ -50,7 +50,7 @@ end | |||
50 | 50 | ||
51 | 51 | ||
52 | -- test file and string names truncation | 52 | -- test file and string names truncation |
53 | a = "function f () end" | 53 | local a = "function f () end" |
54 | local function dostring (s, x) return load(s, x)() end | 54 | local function dostring (s, x) return load(s, x)() end |
55 | dostring(a) | 55 | dostring(a) |
56 | assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) | 56 | assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) |
@@ -72,7 +72,8 @@ dostring(a, string.format("=%s", string.rep('x', 500))) | |||
72 | assert(string.find(debug.getinfo(f).short_src, "^x*$")) | 72 | assert(string.find(debug.getinfo(f).short_src, "^x*$")) |
73 | dostring(a, "=") | 73 | dostring(a, "=") |
74 | assert(debug.getinfo(f).short_src == "") | 74 | assert(debug.getinfo(f).short_src == "") |
75 | a = nil; f = nil; | 75 | _G.a = nil; _G.f = nil; |
76 | _G[string.rep("p", 400)] = nil | ||
76 | 77 | ||
77 | 78 | ||
78 | repeat | 79 | repeat |
@@ -120,6 +121,7 @@ else | |||
120 | end | 121 | end |
121 | ]], {2,3,4,7}) | 122 | ]], {2,3,4,7}) |
122 | 123 | ||
124 | |||
123 | test([[ | 125 | test([[ |
124 | local function foo() | 126 | local function foo() |
125 | end | 127 | end |
@@ -128,6 +130,7 @@ A = 1 | |||
128 | A = 2 | 130 | A = 2 |
129 | A = 3 | 131 | A = 3 |
130 | ]], {2, 3, 2, 4, 5, 6}) | 132 | ]], {2, 3, 2, 4, 5, 6}) |
133 | _G.A = nil | ||
131 | 134 | ||
132 | 135 | ||
133 | test([[-- | 136 | test([[-- |
@@ -175,6 +178,8 @@ end | |||
175 | 178 | ||
176 | test([[for i=1,4 do a=1 end]], {1,1,1,1}) | 179 | test([[for i=1,4 do a=1 end]], {1,1,1,1}) |
177 | 180 | ||
181 | _G.a = nil | ||
182 | |||
178 | 183 | ||
179 | do -- testing line info/trace with large gaps in source | 184 | do -- testing line info/trace with large gaps in source |
180 | 185 | ||
@@ -194,6 +199,7 @@ do -- testing line info/trace with large gaps in source | |||
194 | end | 199 | end |
195 | end | 200 | end |
196 | end | 201 | end |
202 | _G.a = nil | ||
197 | 203 | ||
198 | 204 | ||
199 | do -- testing active lines | 205 | do -- testing active lines |
@@ -287,7 +293,6 @@ foo(200, 3, 4) | |||
287 | local a = {} | 293 | local a = {} |
288 | for i = 1, (_soft and 100 or 1000) do a[i] = i end | 294 | for i = 1, (_soft and 100 or 1000) do a[i] = i end |
289 | foo(table.unpack(a)) | 295 | foo(table.unpack(a)) |
290 | a = nil | ||
291 | 296 | ||
292 | 297 | ||
293 | 298 | ||
@@ -307,13 +312,14 @@ do -- test hook presence in debug info | |||
307 | debug.sethook() | 312 | debug.sethook() |
308 | assert(count == 4) | 313 | assert(count == 4) |
309 | end | 314 | end |
315 | _ENV.a = nil | ||
310 | 316 | ||
311 | 317 | ||
312 | -- hook table has weak keys | 318 | -- hook table has weak keys |
313 | assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') | 319 | assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') |
314 | 320 | ||
315 | 321 | ||
316 | a = {}; L = nil | 322 | a = {}; local L = nil |
317 | local glob = 1 | 323 | local glob = 1 |
318 | local oldglob = glob | 324 | local oldglob = glob |
319 | debug.sethook(function (e,l) | 325 | debug.sethook(function (e,l) |
@@ -354,7 +360,7 @@ function foo() | |||
354 | end; foo() -- set L | 360 | end; foo() -- set L |
355 | -- check line counting inside strings and empty lines | 361 | -- check line counting inside strings and empty lines |
356 | 362 | ||
357 | _ = 'alo\ | 363 | local _ = 'alo\ |
358 | alo' .. [[ | 364 | alo' .. [[ |
359 | 365 | ||
360 | ]] | 366 | ]] |
@@ -403,6 +409,7 @@ function g(a,b) return (a+1) + f() end | |||
403 | 409 | ||
404 | assert(g(0,0) == 30) | 410 | assert(g(0,0) == 30) |
405 | 411 | ||
412 | _G.f, _G.g = nil | ||
406 | 413 | ||
407 | debug.sethook(nil); | 414 | debug.sethook(nil); |
408 | assert(not debug.gethook()) | 415 | assert(not debug.gethook()) |
@@ -446,7 +453,7 @@ local function collectlocals (level) | |||
446 | end | 453 | end |
447 | 454 | ||
448 | 455 | ||
449 | X = nil | 456 | local X = nil |
450 | a = {} | 457 | a = {} |
451 | function a:f (a, b, ...) local arg = {...}; local c = 13 end | 458 | function a:f (a, b, ...) local arg = {...}; local c = 13 end |
452 | debug.sethook(function (e) | 459 | debug.sethook(function (e) |
@@ -469,6 +476,7 @@ a:f(1,2,3,4,5) | |||
469 | assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) | 476 | assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) |
470 | assert(XX == 12) | 477 | assert(XX == 12) |
471 | assert(not debug.gethook()) | 478 | assert(not debug.gethook()) |
479 | _G.XX = nil | ||
472 | 480 | ||
473 | 481 | ||
474 | -- testing access to local variables in return hook (bug in 5.2) | 482 | -- testing access to local variables in return hook (bug in 5.2) |
@@ -593,6 +601,7 @@ end | |||
593 | 601 | ||
594 | debug.sethook() | 602 | debug.sethook() |
595 | 603 | ||
604 | local g, g1 | ||
596 | 605 | ||
597 | -- tests for tail calls | 606 | -- tests for tail calls |
598 | local function f (x) | 607 | local function f (x) |
@@ -638,7 +647,7 @@ h(false) | |||
638 | debug.sethook() | 647 | debug.sethook() |
639 | assert(b == 2) -- two tail calls | 648 | assert(b == 2) -- two tail calls |
640 | 649 | ||
641 | lim = _soft and 3000 or 30000 | 650 | local lim = _soft and 3000 or 30000 |
642 | local function foo (x) | 651 | local function foo (x) |
643 | if x==0 then | 652 | if x==0 then |
644 | assert(debug.getinfo(2).what == "main") | 653 | assert(debug.getinfo(2).what == "main") |
@@ -940,7 +949,7 @@ end | |||
940 | 949 | ||
941 | 950 | ||
942 | print("testing debug functions on chunk without debug info") | 951 | print("testing debug functions on chunk without debug info") |
943 | prog = [[-- program to be loaded without debug information (strip) | 952 | local prog = [[-- program to be loaded without debug information (strip) |
944 | local debug = require'debug' | 953 | local debug = require'debug' |
945 | local a = 12 -- a local variable | 954 | local a = 12 -- a local variable |
946 | 955 | ||