]> git.lizzy.rs Git - minetest.git/blob - games/devtest/mods/unittests/misc.lua
Add callback on_mapblocks_changed
[minetest.git] / games / devtest / mods / unittests / misc.lua
1 local function test_random()
2         -- Try out PseudoRandom
3         local pseudo = PseudoRandom(13)
4         assert(pseudo:next() == 22290)
5         assert(pseudo:next() == 13854)
6 end
7 unittests.register("test_random", test_random)
8
9 local function test_dynamic_media(cb, player)
10         if core.get_player_information(player:get_player_name()).protocol_version < 40 then
11                 core.log("warning", "test_dynamic_media: Client too old, skipping test.")
12                 return cb()
13         end
14
15         -- Check that the client acknowledges media transfers
16         local path = core.get_worldpath() .. "/test_media.obj"
17         local f = io.open(path, "w")
18         f:write("# contents don't matter\n")
19         f:close()
20
21         local call_ok = false
22         local ok = core.dynamic_add_media({
23                 filepath = path,
24                 to_player = player:get_player_name(),
25         }, function(name)
26                 if not call_ok then
27                         return cb("impossible condition")
28                 end
29                 cb()
30         end)
31         if not ok then
32                 return cb("dynamic_add_media() returned error")
33         end
34         call_ok = true
35
36         -- if the callback isn't called this test will just hang :shrug:
37 end
38 unittests.register("test_dynamic_media", test_dynamic_media, {async=true, player=true})
39
40 local function test_v3f_metatable(player)
41         assert(vector.check(player:get_pos()))
42 end
43 unittests.register("test_v3f_metatable", test_v3f_metatable, {player=true})
44
45 local function test_v3s16_metatable(player, pos)
46         local node = minetest.get_node(pos)
47         local found_pos = minetest.find_node_near(pos, 0, node.name, true)
48         assert(vector.check(found_pos))
49 end
50 unittests.register("test_v3s16_metatable", test_v3s16_metatable, {map=true})
51
52 local function test_clear_meta(_, pos)
53         local ref = core.get_meta(pos)
54
55         for way = 1, 3 do
56                 ref:set_string("foo", "bar")
57                 assert(ref:contains("foo"))
58
59                 if way == 1 then
60                         ref:from_table({})
61                 elseif way == 2 then
62                         ref:from_table(nil)
63                 else
64                         ref:set_string("foo", "")
65                 end
66
67                 assert(#core.find_nodes_with_meta(pos, pos) == 0, "clearing failed " .. way)
68         end
69 end
70 unittests.register("test_clear_meta", test_clear_meta, {map=true})
71
72 local on_punch_called
73 minetest.register_on_punchnode(function()
74         on_punch_called = true
75 end)
76 unittests.register("test_punch_node", function(_, pos)
77         minetest.place_node(pos, {name="basenodes:dirt"})
78         on_punch_called = false
79         minetest.punch_node(pos)
80         minetest.remove_node(pos)
81         -- currently failing: assert(on_punch_called)
82 end, {map=true})
83
84 local function test_compress()
85         -- This text should be compressible, to make sure the results are... normal
86         local text = "The\000 icey canoe couldn't move very well on the\128 lake. The\000 ice was too stiff and the icey canoe's paddles simply wouldn't punch through."
87         local methods = {
88                 "deflate",
89                 "zstd",
90                 -- "noodle", -- for warning alarm test
91         }
92         local zstd_magic = string.char(0x28, 0xB5, 0x2F, 0xFD)
93         for _, method in ipairs(methods) do
94                 local compressed = core.compress(text, method)
95                 assert(core.decompress(compressed, method) == text, "input/output mismatch for compression method " .. method)
96                 local has_zstd_magic = compressed:sub(1, 4) == zstd_magic
97                 if method == "zstd" then
98                         assert(has_zstd_magic, "zstd magic number not in zstd method")
99                 else
100                         assert(not has_zstd_magic, "zstd magic number in method " .. method .. " (which is not zstd)")
101                 end
102         end
103 end
104 unittests.register("test_compress", test_compress)
105
106 local function test_game_info()
107         local info = minetest.get_game_info()
108         local game_conf = Settings(info.path .. "/game.conf")
109         assert(info.id == "devtest")
110         assert(info.title == game_conf:get("title"))
111 end
112 unittests.register("test_game_info", test_game_info)
113
114 local function test_mapgen_edges(cb)
115         -- Test that the map can extend to the expected edges and no further.
116         local min_edge, max_edge = minetest.get_mapgen_edges()
117         local min_finished = {}
118         local max_finished = {}
119         local function finish()
120                 if #min_finished ~= 1 then
121                         return cb("Expected 1 block to emerge around mapgen minimum edge")
122                 end
123                 if min_finished[1] ~= (min_edge / minetest.MAP_BLOCKSIZE):floor() then
124                         return cb("Expected block within minimum edge to emerge")
125                 end
126                 if #max_finished ~= 1 then
127                         return cb("Expected 1 block to emerge around mapgen maximum edge")
128                 end
129                 if max_finished[1] ~= (max_edge / minetest.MAP_BLOCKSIZE):floor() then
130                         return cb("Expected block within maximum edge to emerge")
131                 end
132                 return cb()
133         end
134         local emerges_left = 2
135         local function emerge_block(blockpos, action, blocks_left, finished)
136                 if action ~= minetest.EMERGE_CANCELLED then
137                         table.insert(finished, blockpos)
138                 end
139                 if blocks_left == 0 then
140                         emerges_left = emerges_left - 1
141                         if emerges_left == 0 then
142                                 return finish()
143                         end
144                 end
145         end
146         minetest.emerge_area(min_edge:subtract(1), min_edge, emerge_block, min_finished)
147         minetest.emerge_area(max_edge, max_edge:add(1), emerge_block, max_finished)
148 end
149 unittests.register("test_mapgen_edges", test_mapgen_edges, {map=true, async=true})
150
151 local finish_test_on_mapblocks_changed
152 minetest.register_on_mapblocks_changed(function(modified_blocks, modified_block_count)
153         if finish_test_on_mapblocks_changed then
154                 finish_test_on_mapblocks_changed(modified_blocks, modified_block_count)
155                 finish_test_on_mapblocks_changed = nil
156         end
157 end)
158 local function test_on_mapblocks_changed(cb, player, pos)
159         local bp1 = (pos / minetest.MAP_BLOCKSIZE):floor()
160         local bp2 = bp1:add(1)
161         for _, bp in ipairs({bp1, bp2}) do
162                 -- Make a modification in the block.
163                 local p = bp * minetest.MAP_BLOCKSIZE
164                 minetest.load_area(p)
165                 local meta = minetest.get_meta(p)
166                 meta:set_int("test_on_mapblocks_changed", meta:get_int("test_on_mapblocks_changed") + 1)
167         end
168         finish_test_on_mapblocks_changed = function(modified_blocks, modified_block_count)
169                 if modified_block_count < 2 then
170                         return cb("Expected at least two mapblocks to be recorded as modified")
171                 end
172                 if not modified_blocks[minetest.hash_node_position(bp1)] or
173                                 not modified_blocks[minetest.hash_node_position(bp2)] then
174                         return cb("The expected mapblocks were not recorded as modified")
175                 end
176                 cb()
177         end
178 end
179 unittests.register("test_on_mapblocks_changed", test_on_mapblocks_changed, {map=true, async=true})