diff options
Diffstat (limited to 'testes')
-rw-r--r-- | testes/files.lua | 37 | ||||
-rw-r--r-- | testes/locals.lua | 8 |
2 files changed, 43 insertions, 2 deletions
diff --git a/testes/files.lua b/testes/files.lua index e68eb9b8..34fcf851 100644 --- a/testes/files.lua +++ b/testes/files.lua | |||
@@ -200,7 +200,7 @@ return x + y * z | |||
200 | assert(f:close()) | 200 | assert(f:close()) |
201 | f = coroutine.wrap(dofile) | 201 | f = coroutine.wrap(dofile) |
202 | assert(f(file) == 10) | 202 | assert(f(file) == 10) |
203 | print(f(100, 101) == 20) | 203 | assert(f(100, 101) == 20) |
204 | assert(f(200) == 100 + 200 * 101) | 204 | assert(f(200) == 100 + 200 * 101) |
205 | assert(os.remove(file)) | 205 | assert(os.remove(file)) |
206 | 206 | ||
@@ -422,6 +422,41 @@ assert(load(io.lines(file, "L"), nil, nil, t))() | |||
422 | assert(t.a == -((10 + 34) * 2)) | 422 | assert(t.a == -((10 + 34) * 2)) |
423 | 423 | ||
424 | 424 | ||
425 | do -- testing closing file in line iteration | ||
426 | |||
427 | -- get the to-be-closed variable from a loop | ||
428 | local function gettoclose (lv) | ||
429 | lv = lv + 1 | ||
430 | for i = 1, math.maxinteger do | ||
431 | local n, v = debug.getlocal(lv, i) | ||
432 | if n == "(for toclose)" then | ||
433 | return v | ||
434 | end | ||
435 | end | ||
436 | end | ||
437 | |||
438 | local f | ||
439 | for l in io.lines(file) do | ||
440 | f = gettoclose(1) | ||
441 | assert(io.type(f) == "file") | ||
442 | break | ||
443 | end | ||
444 | assert(io.type(f) == "closed file") | ||
445 | |||
446 | f = nil | ||
447 | local function foo (name) | ||
448 | for l in io.lines(name) do | ||
449 | f = gettoclose(1) | ||
450 | assert(io.type(f) == "file") | ||
451 | error(f) -- exit loop with an error | ||
452 | end | ||
453 | end | ||
454 | local st, msg = pcall(foo, file) | ||
455 | assert(st == false and io.type(msg) == "closed file") | ||
456 | |||
457 | end | ||
458 | |||
459 | |||
425 | -- test for multipe arguments in 'lines' | 460 | -- test for multipe arguments in 'lines' |
426 | io.output(file); io.write"0123456789\n":close() | 461 | io.output(file); io.write"0123456789\n":close() |
427 | for a,b in io.lines(file, 1, 1) do | 462 | for a,b in io.lines(file, 1, 1) do |
diff --git a/testes/locals.lua b/testes/locals.lua index 869ac1ff..28f88e54 100644 --- a/testes/locals.lua +++ b/testes/locals.lua | |||
@@ -371,6 +371,8 @@ do | |||
371 | x = x - 1 | 371 | x = x - 1 |
372 | if x > 0 then return x end | 372 | if x > 0 then return x end |
373 | end, | 373 | end, |
374 | nil, -- state | ||
375 | nil, -- control variable | ||
374 | function () -- closing function | 376 | function () -- closing function |
375 | numopen = numopen - 1 | 377 | numopen = numopen - 1 |
376 | end | 378 | end |
@@ -392,12 +394,16 @@ do | |||
392 | -- repeat test with '__open' metamethod instead of a function | 394 | -- repeat test with '__open' metamethod instead of a function |
393 | local function open (x) | 395 | local function open (x) |
394 | numopen = numopen + 1 | 396 | numopen = numopen + 1 |
397 | local state = setmetatable({x}, | ||
398 | {__close = function () numopen = numopen - 1 end}) | ||
395 | return | 399 | return |
396 | function (t) -- iteraction function | 400 | function (t) -- iteraction function |
397 | t[1] = t[1] - 1 | 401 | t[1] = t[1] - 1 |
398 | if t[1] > 0 then return t[1] end | 402 | if t[1] > 0 then return t[1] end |
399 | end, | 403 | end, |
400 | setmetatable({x}, {__close = function () numopen = numopen - 1 end}) | 404 | state, |
405 | nil, | ||
406 | state -- to-be-closed | ||
401 | end | 407 | end |
402 | 408 | ||
403 | local s = 0 | 409 | local s = 0 |