-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathmisc.lua
More file actions
42 lines (37 loc) · 1.04 KB
/
misc.lua
File metadata and controls
42 lines (37 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-- tests misc functions added by dfhack.lua
config.target = 'core'
function test.safe_pairs()
for k,v in safe_pairs(nil) do
expect.fail('nil should not be iterable')
end
for k,v in safe_pairs('a') do
expect.fail('a string should not be iterable')
end
for k,v in safe_pairs({}) do
expect.fail('an empty table should not be iterable')
end
for k,v in safe_pairs(df.item._identity) do
expect.fail('a non-iterable light userdata var should not be iterable')
end
local iterated = 0
local t = {a='hello', b='world', [1]='extra'}
for k,v in safe_pairs(t) do
expect.eq(t[k], v)
iterated = iterated + 1
end
expect.eq(3, iterated)
end
function test.safe_pairs_ipairs()
local t = {1, 2}
setmetatable(t, {
__pairs = function()
expect.fail('pairs() should not be called')
end,
})
local iterated = 0
for k,v in safe_pairs(t, ipairs) do
expect.eq(k, v)
iterated = iterated + 1
end
expect.eq(#t, iterated)
end