#Factorio / Lua: Reversed `ipairs`
1 messages · Page 1 of 1 (latest)
I made an implementation of iterator class based on the documentation & suggestions above (see #mod-dev-help message and #mod-dev-help message) and noticed that despite using Lua 5.2, Factorio doesn't check for __ipairs metamethod
To prove that hypothesis, I made both __pairs and __ipairs metamethods return error.
And that's the test result:
------------------
1) TestIterToolsReversedIter.test_simple_table_via_ipairs
...actorio\.deps\__bridgelib__\bridgelib\misc\itertools.lua:76: Invalid Operation: Cannot use '__index' method on bridgelib.misc.itertools.reversed("table: 0000000000f6dd20") (key=1)
stack traceback:
...actorio\.deps\__bridgelib__\bridgelib\misc\itertools.lua:76: in function <...actorio\.deps\__bridgelib__\bridgelib\misc\itertools.lua:71>
[C]: in for iterator 'for iterator'
D:\git\factorio\bridgelib\tests\misc\test-itertools.lua:142: in upvalue 'TestIterToolsReversedIter.test_simple_table_via_ipairs'
2) TestIterToolsReversedIter.test_simple_table_via_pairs
...actorio\.deps\__bridgelib__\bridgelib\misc\itertools.lua:116: abstract_iter_meta.__pairs: Not Implemented
Ran 2 tests in 0.002 seconds, 0 successes, 2 errors, 9 non-selected: in upvalue 'TestIterToolsReversedIter.test_simple_table_via_pairs'rtools.lua:115>
(I have explicitly disallowed using __index on the iterator result to avoid inappropriate usage)
Tests are simple as hell:
function TestIterToolsReversedIter:test_simple_table_via_pairs()
local input = { 1, 7, -231, true, 'test', false, 'foo', { }, 'bar' }
local expected = { 'bar', { }, 'foo', false, 'test', true, -231, 7, 1 }
local actual = { }
for _, el in pairs(itertools.reversed_iter(input))
do table.insert(actual, el)
end
lu.assertEquals(actual, expected)
end
function TestIterToolsReversedIter:test_simple_table_via_ipairs()
local input = { 1, 7, -231, true, 'test', false, 'foo', { }, 'bar' }
local expected = { 'bar', { }, 'foo', false, 'test', true, -231, 7, 1 }
local actual = { }
for _, el in ipairs(itertools.reversed_iter(input))
do table.insert(actual, el)
end
lu.assertEquals(actual, expected)
end