-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.spec.lua
163 lines (133 loc) · 3.72 KB
/
data.spec.lua
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
local pos1 = { x=-32, y=-32, z=-32 }
local pos2 = { x=64, y=32, z=64 }
local storage = minetest.get_mod_storage()
local function check_store(store)
assert(store)
-- clear
store:clear()
-- create
store:set(pos1, {x=1})
-- read
local data = store:get(pos1)
assert(data)
assert(data.x == 1)
-- read non-existing
data = store:get(pos2)
assert(data == nil)
-- merge
store:merge(pos1, {y=2})
data = store:get(pos1)
assert(data)
assert(data.x == 1)
assert(data.y == 2)
-- merge into non-existing
store:merge(pos2, {z=3})
data = store:get(pos2)
assert(data)
assert(data.x == nil)
assert(data.y == nil)
assert(data.z == 3)
-- remove
store:set(pos1, nil)
assert(store:get(pos1) == nil)
-- save and purge store
store:save_stale_data()
store:purge()
-- validate persisted data
data = store:get(pos2)
assert(data)
assert(data.z == 3)
data = store:get(pos1)
assert(data == nil)
end
mtt.register("data storage (default serialization)", function(callback)
check_store(mapblock_lib.create_data_storage(storage))
callback()
end)
mtt.register("data storage (granularity = 50)", function(callback)
check_store(mapblock_lib.create_data_storage(storage, {
granularity = 50
}))
callback()
end)
mtt.register("data storage (prefix = test_)", function(callback)
check_store(mapblock_lib.create_data_storage(storage, {
prefix = "test_"
}))
callback()
end)
mtt.register("data storage (json serialization)", function(callback)
check_store(mapblock_lib.create_data_storage(storage, {
serialize = minetest.write_json,
deserialize = minetest.parse_json
}))
callback()
end)
local function check_storage_links(store)
assert(store)
store:clear()
-- original data
store:set({x=10,y=0,z=0}, {mydata=true})
-- link to original
store:set({x=20,y=0,z=0}, mapblock_lib.create_data_link({x=10,y=0,z=0}))
-- link to link
store:set({x=30,y=0,z=0}, mapblock_lib.create_data_link({x=20,y=0,z=0}))
-- ensure that links exist
assert(not mapblock_lib.is_data_link(store:get({x=10,y=0,z=0})))
assert(mapblock_lib.is_data_link(store:get({x=20,y=0,z=0})))
assert(mapblock_lib.is_data_link(store:get({x=30,y=0,z=0})))
-- simple link
local data, target_pos = mapblock_lib.resolve_data_link(store, {x=20,y=0,z=0})
assert(data)
assert(data.mydata)
assert(target_pos)
assert(target_pos.x == 10)
assert(target_pos.y == 0)
assert(target_pos.z == 0)
-- nested link
data, target_pos = mapblock_lib.resolve_data_link(store, {x=30,y=0,z=0})
assert(data)
assert(data.mydata)
assert(target_pos)
assert(target_pos.x == 10)
assert(target_pos.y == 0)
assert(target_pos.z == 0)
-- not a link (data)
data, target_pos = mapblock_lib.resolve_data_link(store, {x=10,y=0,z=0})
assert(data)
assert(data.mydata)
assert(target_pos)
assert(target_pos.x == 10)
assert(target_pos.y == 0)
assert(target_pos.z == 0)
-- not a link (nil)
data = mapblock_lib.resolve_data_link(store, {x=999,y=0,z=0})
assert(not data)
assert(target_pos)
assert(target_pos.x == 10)
assert(target_pos.y == 0)
assert(target_pos.z == 0)
end
mtt.register("data storage links (default serialization)", function(callback)
check_storage_links(mapblock_lib.create_data_storage(storage))
callback()
end)
mtt.register("data storage links (json serialization)", function(callback)
check_storage_links(mapblock_lib.create_data_storage(storage, {
serialize = minetest.write_json,
deserialize = minetest.parse_json
}))
callback()
end)
mtt.register("data storage links (granularity = 50)", function(callback)
check_storage_links(mapblock_lib.create_data_storage(storage, {
granularity = 50
}))
callback()
end)
mtt.register("data storage links (prefix = test)", function(callback)
check_storage_links(mapblock_lib.create_data_storage(storage, {
prefix = "test_"
}))
callback()
end)