aboutsummaryrefslogtreecommitdiff
path: root/testes/attrib.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/attrib.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/attrib.lua')
-rw-r--r--testes/attrib.lua24
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
87local function D (x) 87local function D (x)
88 x = string.gsub(x, "/", dirsep) 88 local x = string.gsub(x, "/", dirsep)
89 return DIR .. x 89 return DIR .. x
90end 90end
91 91
@@ -106,7 +106,7 @@ local function createfiles (files, preextras, posextras)
106 end 106 end
107end 107end
108 108
109function removefiles (files) 109local 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)
155end 155end
156 156
157a = require"names" 157local a = require"names"
158assert(a[1] == "names" and a[2] == D"names.lua") 158assert(a[1] == "names" and a[2] == D"names.lua")
159 159
160_G.a = nil
161local st, msg = pcall(require, "err") 160local st, msg = pcall(require, "err")
162assert(not st and string.find(msg, "arithmetic") and B == 15) 161assert(not st and string.find(msg, "arithmetic") and B == 15)
163st, msg = pcall(require, "synerr") 162st, msg = pcall(require, "synerr")
@@ -191,6 +190,7 @@ try("X", "XXxX", AA, "libs/XXxX")
191 190
192 191
193removefiles(files) 192removefiles(files)
193NAME, 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
225removefiles(files) 225removefiles(files)
226 226AA = nil
227 227
228package.path = "" 228package.path = ""
229assert(not pcall(require, "file_does_not_exist")) 229assert(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
308end 309end
309 310
310_ENV = _G 311_ENV = _G
@@ -338,10 +339,10 @@ print("testing assignments, logical operators, and constructors")
338 339
339local res, res2 = 27 340local res, res2 = 27
340 341
341a, b = 1, 2+3 342local a, b = 1, 2+3
342assert(a==1 and b==5) 343assert(a==1 and b==5)
343a={} 344a={}
344function f() return 10, 11, 12 end 345local function f() return 10, 11, 12 end
345a.x, b, a[1] = 1, 2, f() 346a.x, b, a[1] = 1, 2, f()
346assert(a.x==1 and b==2 and a[1]==10) 347assert(a.x==1 and b==2 and a[1]==10)
347a[f()], b, a[f()+3] = f(), a, 'x' 348a[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)
362end 363end
363 364
364a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 365local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
365assert(not a and b and c and d==6) 366assert(not a and b and c and d==6)
366 367
367d = 20 368d = 20
@@ -419,6 +420,7 @@ assert(not pcall(function () local a = {[nil] = 10} end))
419assert(a[nil] == undef) 420assert(a[nil] == undef)
420a = nil 421a = nil
421 422
423local a, b, c
422a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} 424a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
423a, a.x, a.y = a, a[-3] 425a, a.x, a.y = a, a[-3]
424assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) 426assert(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
456end 458end
457 459
458maxintF = maxint + 0.0 -- float version 460local maxintF = maxint + 0.0 -- float version
459 461
460assert(maxintF == maxint and math.type(maxintF) == "float" and 462assert(maxintF == maxint and math.type(maxintF) == "float" and
461 maxintF >= 2.0^14) 463 maxintF >= 2.0^14)