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/attrib.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/attrib.lua')
-rw-r--r-- | testes/attrib.lua | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/testes/attrib.lua b/testes/attrib.lua index 83821c06..458488a8 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua | |||
@@ -85,7 +85,7 @@ local DIR = "libs" .. dirsep | |||
85 | 85 | ||
86 | -- prepend DIR to a name and correct directory separators | 86 | -- prepend DIR to a name and correct directory separators |
87 | local function D (x) | 87 | local function D (x) |
88 | x = string.gsub(x, "/", dirsep) | 88 | local x = string.gsub(x, "/", dirsep) |
89 | return DIR .. x | 89 | return DIR .. x |
90 | end | 90 | end |
91 | 91 | ||
@@ -106,7 +106,7 @@ local function createfiles (files, preextras, posextras) | |||
106 | end | 106 | end |
107 | end | 107 | end |
108 | 108 | ||
109 | function removefiles (files) | 109 | local function removefiles (files) |
110 | for n in pairs(files) do | 110 | for n in pairs(files) do |
111 | os.remove(D(n)) | 111 | os.remove(D(n)) |
112 | end | 112 | end |
@@ -154,10 +154,9 @@ local try = function (p, n, r, ext) | |||
154 | assert(ext == x) | 154 | assert(ext == x) |
155 | end | 155 | end |
156 | 156 | ||
157 | a = require"names" | 157 | local a = require"names" |
158 | assert(a[1] == "names" and a[2] == D"names.lua") | 158 | assert(a[1] == "names" and a[2] == D"names.lua") |
159 | 159 | ||
160 | _G.a = nil | ||
161 | local st, msg = pcall(require, "err") | 160 | local st, msg = pcall(require, "err") |
162 | assert(not st and string.find(msg, "arithmetic") and B == 15) | 161 | assert(not st and string.find(msg, "arithmetic") and B == 15) |
163 | st, msg = pcall(require, "synerr") | 162 | st, msg = pcall(require, "synerr") |
@@ -191,6 +190,7 @@ try("X", "XXxX", AA, "libs/XXxX") | |||
191 | 190 | ||
192 | 191 | ||
193 | removefiles(files) | 192 | removefiles(files) |
193 | NAME, REQUIRED, AA, B = nil | ||
194 | 194 | ||
195 | 195 | ||
196 | -- testing require of sub-packages | 196 | -- testing require of sub-packages |
@@ -223,7 +223,7 @@ assert(require"P1" == m and m.AA == 10) | |||
223 | 223 | ||
224 | 224 | ||
225 | removefiles(files) | 225 | removefiles(files) |
226 | 226 | AA = nil | |
227 | 227 | ||
228 | package.path = "" | 228 | package.path = "" |
229 | assert(not pcall(require, "file_does_not_exist")) | 229 | assert(not pcall(require, "file_does_not_exist")) |
@@ -305,6 +305,7 @@ else | |||
305 | assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") | 305 | assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") |
306 | assert(string.find(ext, "libs/lib1", 1, true)) | 306 | assert(string.find(ext, "libs/lib1", 1, true)) |
307 | assert(fs.id(45) == 45) | 307 | assert(fs.id(45) == 45) |
308 | _ENV.x, _ENV.y = nil | ||
308 | end | 309 | end |
309 | 310 | ||
310 | _ENV = _G | 311 | _ENV = _G |
@@ -338,10 +339,10 @@ print("testing assignments, logical operators, and constructors") | |||
338 | 339 | ||
339 | local res, res2 = 27 | 340 | local res, res2 = 27 |
340 | 341 | ||
341 | a, b = 1, 2+3 | 342 | local a, b = 1, 2+3 |
342 | assert(a==1 and b==5) | 343 | assert(a==1 and b==5) |
343 | a={} | 344 | a={} |
344 | function f() return 10, 11, 12 end | 345 | local function f() return 10, 11, 12 end |
345 | a.x, b, a[1] = 1, 2, f() | 346 | a.x, b, a[1] = 1, 2, f() |
346 | assert(a.x==1 and b==2 and a[1]==10) | 347 | assert(a.x==1 and b==2 and a[1]==10) |
347 | a[f()], b, a[f()+3] = f(), a, 'x' | 348 | a[f()], b, a[f()+3] = f(), a, 'x' |
@@ -353,15 +354,15 @@ do | |||
353 | local a,b,c | 354 | local a,b,c |
354 | a,b = 0, f(1) | 355 | a,b = 0, f(1) |
355 | assert(a == 0 and b == 1) | 356 | assert(a == 0 and b == 1) |
356 | A,b = 0, f(1) | 357 | a,b = 0, f(1) |
357 | assert(A == 0 and b == 1) | 358 | assert(a == 0 and b == 1) |
358 | a,b,c = 0,5,f(4) | 359 | a,b,c = 0,5,f(4) |
359 | assert(a==0 and b==5 and c==1) | 360 | assert(a==0 and b==5 and c==1) |
360 | a,b,c = 0,5,f(0) | 361 | a,b,c = 0,5,f(0) |
361 | assert(a==0 and b==5 and c==nil) | 362 | assert(a==0 and b==5 and c==nil) |
362 | end | 363 | end |
363 | 364 | ||
364 | a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 | 365 | local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 |
365 | assert(not a and b and c and d==6) | 366 | assert(not a and b and c and d==6) |
366 | 367 | ||
367 | d = 20 | 368 | d = 20 |
@@ -419,6 +420,7 @@ assert(not pcall(function () local a = {[nil] = 10} end)) | |||
419 | assert(a[nil] == undef) | 420 | assert(a[nil] == undef) |
420 | a = nil | 421 | a = nil |
421 | 422 | ||
423 | local a, b, c | ||
422 | a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} | 424 | a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} |
423 | a, a.x, a.y = a, a[-3] | 425 | a, a.x, a.y = a, a[-3] |
424 | assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) | 426 | assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) |
@@ -455,7 +457,7 @@ while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do | |||
455 | maxint = maxint // 2 | 457 | maxint = maxint // 2 |
456 | end | 458 | end |
457 | 459 | ||
458 | maxintF = maxint + 0.0 -- float version | 460 | local maxintF = maxint + 0.0 -- float version |
459 | 461 | ||
460 | assert(maxintF == maxint and math.type(maxintF) == "float" and | 462 | assert(maxintF == maxint and math.type(maxintF) == "float" and |
461 | maxintF >= 2.0^14) | 463 | maxintF >= 2.0^14) |