Skip to content

Commit f02cd60

Browse files
committed
Update tests
1 parent 270d7e8 commit f02cd60

9 files changed

+395
-45
lines changed

.luacheckrc

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
unused_args = false
2+
3+
-- Exclude regression tests / unit tests
4+
exclude_files = {
5+
"**/core/**",
6+
"**/spec/**",
7+
"**/demo_spec/**",
8+
}
9+
10+
globals = {
11+
"mineunit", "world",
12+
"mineunit_path", "fixture", "fixture_path",
13+
"core", "minetest",
14+
"vector", "dump","dump2",
15+
"default",
16+
}
17+
18+
read_globals = {
19+
-- luassert
20+
assert = { fields = {
21+
"is_string", "is_table", "player_or_name", "is_ItemStack", "is_Player"
22+
}},
23+
24+
-- Mineunit
25+
"mineunit_config", "mineunit_conf_defaults", "mineunit_conf_override",
26+
"NodeTimerRef", "MetaDataRef", "NodeMetaRef", "ObjectRef", "InvRef",
27+
"DEPRECATED",
28+
29+
-- Minetest
30+
string = {fields = {"split", "trim"}},
31+
table = {fields = {"copy", "getn"}},
32+
"PseudoRandom", "ItemStack", "VoxelArea", "VoxelManip", "Settings",
33+
}

mod.conf

Whitespace-only changes.

spec/assert_spec.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("Mineunit assert", function()
2525
it("does not accept string", function() assert.error(function() test_assert("SX") end) end)
2626
it("does not accept empty arguments", function() assert.error(function() test_assert() end) end)
2727
it("does not accept nil", function() assert.error(function() test_assert(nil) end) end)
28-
it("accepts ItemStack", function() test_assert(ItemStack()) end)
28+
it("accepts ItemStack", function() test_assert(ItemStack(nil)) end)
2929
end)
3030

3131
describe("is_InvRef", function()
@@ -60,7 +60,7 @@ describe("Mineunit assert", function()
6060
describe("type override", function()
6161

6262
it("returns Player as userdata", function() assert.equals("userdata", type(Player())) end)
63-
it("returns ItemStack as userdata", function() assert.equals("userdata", type(ItemStack())) end)
63+
it("returns ItemStack as userdata", function() assert.equals("userdata", type(ItemStack(nil))) end)
6464
it("returns InvRef as userdata", function() assert.equals("userdata", type(InvRef())) end)
6565
it("returns InvList as table", function()
6666
local inv = InvRef()

spec/craft_spec.lua

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
-- For self tests package path must be set in a way that makes package loaders search current directory first
2+
package.path = "./?.lua;../?/init.lua;../?.lua;" --.. package.path
3+
4+
describe("Craft API", function()
5+
6+
require("mineunit")
7+
mineunit("core")
8+
mineunit("itemstack")
9+
mineunit("entity")
10+
sourcefile("player")
11+
fixture("items")
12+
13+
local CM = mineunit.CraftManager
14+
15+
minetest.register_craft({
16+
output = 'bridge',
17+
recipe = {
18+
{'stone', 'stone', 'stone'}
19+
}
20+
})
21+
22+
minetest.register_craft({
23+
output = 'cross',
24+
recipe = {
25+
{'stone', 'stone', 'stone'},
26+
{'', 'stone', ''},
27+
{'', 'stone', ''},
28+
}
29+
})
30+
31+
describe("get_all_craft_recipes", function()
32+
33+
it("returns nil without matches", function()
34+
local result = core.get_all_craft_recipes("testunknown")
35+
assert.is_nil(result)
36+
end)
37+
38+
it("returns matching recipes", function()
39+
local result = core.get_all_craft_recipes("wood")
40+
assert.is_table(result)
41+
end)
42+
43+
end)
44+
45+
describe("get_craft_result", function()
46+
47+
it("throws without arguments", function()
48+
assert.has_error(function() core.get_craft_result() end)
49+
end)
50+
51+
it("throws without without items", function()
52+
assert.has_error(function()
53+
core.get_craft_result({
54+
method = "normal",
55+
width = 1,
56+
})
57+
end)
58+
end)
59+
60+
it("throws without with invalid items", function()
61+
assert.has_error(function()
62+
core.get_craft_result({
63+
method = "normal",
64+
width = 1,
65+
items = "wood",
66+
})
67+
end)
68+
end)
69+
70+
it("does not modify input for string items", function()
71+
local input = {
72+
method = "normal",
73+
width = 3,
74+
items = {
75+
"tree 1"
76+
},
77+
}
78+
local expected_address = tostring(input)
79+
local expected_result = table.copy(input)
80+
core.get_craft_result(input)
81+
-- Compare table address
82+
assert.equals(expected_address, tostring(input))
83+
assert.same(expected_result, input)
84+
end)
85+
86+
it("does not modify input for ItemStack items", function()
87+
local input = {
88+
method = "normal",
89+
width = 3,
90+
items = {
91+
ItemStack("tree 1")
92+
},
93+
}
94+
local expected_address = tostring(input)
95+
local expected_result = table.copy(input)
96+
core.get_craft_result(input)
97+
-- Compare table address
98+
assert.equals(expected_address, tostring(input))
99+
assert.same(expected_result, input)
100+
end)
101+
102+
it("returns results", function()
103+
local input = {
104+
method = "normal",
105+
width = 3,
106+
items = {
107+
ItemStack("tree 1")
108+
},
109+
}
110+
local expected_result = {
111+
time = 0,
112+
replacements = {},
113+
item = ItemStack("wood 4")
114+
}
115+
local expected_leftover = {
116+
width = 3,
117+
items = {
118+
ItemStack(nil)
119+
},
120+
method = "normal"
121+
}
122+
local result, leftover = core.get_craft_result(input)
123+
assert.same(expected_result, result)
124+
assert.same(expected_leftover, leftover)
125+
end)
126+
127+
end)
128+
129+
end)

spec/fixtures/items.lua

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
core.register_node(":chest", {
2+
description = "chest",
3+
buildable_to = false,
4+
walkable = true,
5+
on_construct = function(pos)
6+
local meta = core.get_meta(pos)
7+
local inv = meta:get_inventory()
8+
inv:set_size("chest", 3)
9+
end,
10+
})
11+
12+
core.register_node(":stone", {
13+
description = "stone",
14+
buildable_to = false,
15+
walkable = true,
16+
drawtype = "nodebox",
17+
node_box = {
18+
type = "fixed",
19+
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
20+
},
21+
collision_box = {
22+
type = "fixed",
23+
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
24+
},
25+
selection_box = {
26+
type = "fixed",
27+
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
28+
},
29+
nodebox = {}
30+
})
31+
32+
core.register_node(":bridge", {
33+
description = "bridge",
34+
buildable_to = false,
35+
walkable = true,
36+
drawtype = "nodebox",
37+
node_box = {
38+
type = "fixed",
39+
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
40+
},
41+
collision_box = {
42+
type = "fixed",
43+
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
44+
},
45+
selection_box = {
46+
type = "fixed",
47+
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
48+
},
49+
nodebox = {}
50+
})
51+
52+
minetest.register_node(":tree", {
53+
description = "Apple Tree",
54+
tiles = {"tree_top.png", "tree_top.png", "tree.png"},
55+
paramtype2 = "facedir",
56+
is_ground_content = false,
57+
groups = {tree = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2},
58+
on_place = minetest.rotate_node
59+
})
60+
61+
minetest.register_node(":wood", {
62+
description = "Apple Wood Planks",
63+
paramtype2 = "facedir",
64+
place_param2 = 0,
65+
tiles = {"wood.png"},
66+
is_ground_content = false,
67+
groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2, wood = 1},
68+
})
69+
70+
minetest.register_node(":bush_stem", {
71+
description = "Bush Stem",
72+
drawtype = "plantlike",
73+
visual_scale = 1.41,
74+
tiles = {"bush_stem.png"},
75+
inventory_image = "bush_stem.png",
76+
wield_image = "bush_stem.png",
77+
paramtype = "light",
78+
sunlight_propagates = true,
79+
groups = {choppy = 2, oddly_breakable_by_hand = 1, flammable = 2},
80+
selection_box = {
81+
type = "fixed",
82+
fixed = {-7 / 16, -0.5, -7 / 16, 7 / 16, 0.5, 7 / 16},
83+
},
84+
})
85+
86+
minetest.register_craft({
87+
output = "wood 4",
88+
recipe = {
89+
{"tree"},
90+
}
91+
})
92+
93+
minetest.register_craft({
94+
output = "wood",
95+
recipe = {
96+
{"bush_stem"},
97+
}
98+
})
99+
100+
minetest.register_craft({
101+
type = "fuel",
102+
recipe = "bush_stem",
103+
burntime = 7,
104+
})
105+
106+
minetest.register_craft({
107+
type = "fuel",
108+
recipe = "group:wood",
109+
burntime = 7,
110+
})
111+
112+
minetest.register_craft({
113+
type = "fuel",
114+
recipe = "group:tree",
115+
burntime = 30,
116+
})

0 commit comments

Comments
 (0)