summaryrefslogtreecommitdiff
path: root/honey/ecs/ecs.test.lua
blob: 74e27dc56acfa5c9da8c5766e2cc80211324a681 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
local testCount = 0
local failCount = 0

local function test(msg, f)
	testCount = testCount + 1
	local success, error = xpcall(f, debug.traceback)
	if success then
		print(msg .. "\t\t[OK]")
	else
		failCount = failCount + 1
		print(msg .. "\t\t[FAIL]")
		print(error)
	end
end


local ecs = require 'ecs'


--===== Component tests =====--

local Component = ecs.Component

test("factories work as expected", function()
	local factory = Component.newFactory("health", { percent=100 })
	local comp1 = factory()
	assert(comp1.__type == "health", "bad component type for comp1")
	assert(comp1.percent == 100, "bat percent for comp1")

	local comp2 = factory{ percent=50 }
	assert(comp2.__type == "health", "bad component type for comp2")
	assert(comp2.percent == 50, "bad percent for comp2")

	local success = pcall(function()
		comp2.dne = 5
	end)
	assert(not success, "incorrectly succeeded in setting comp2.dne")

	local success = pcall(function()
		local comp3 = factory{ percent = 44, something = 2 }
	end)
	assert(not success, "incorrectly succeeded in creating comp3")
end)


test("components serialize as expected", function()
	local position = Component.newFactory("position", { x=0, y=0, z=0 })
	local comp = position{x=10, y=15, z=10}
	local str = tostring(comp)
	local tbl = (loadstring("return " .. str))()
	assert(tbl.__type == "position", "bad type")
	assert(tbl.x == 10, "bad x")
	assert(tbl.y == 15, "bad y")
	assert(tbl.z == 10, "bad z")
end)


test("components serialize successfully with subcomponents", function()
	local position = Component.newFactory("position", { x=0, y=0, z=0 })
	local player = Component.newFactory("player", { name="", position={} })

	local p = player{ name="hannah", position=position{x=10, y=9, z=8} }
	local tbl = (loadstring("return " .. tostring(p)))()
	assert(tbl.__type == "player")
	assert(tbl.name == "hannah")
	assert(tbl.position.__type == "position")
	assert(tbl.position.x == 10)
	assert(tbl.position.y == 9)
	assert(tbl.position.z == 8)
end)


--===== EntityDb tests =====--

local EntityDb = ecs.EntityDb


test("EntityDb.createEntity() always returns a new id", function()
	local db = EntityDb()

	local ids = {}
	for i=1,100 do
		local id = db:createEntity()
		assert(ids[id] == nil, "id was already returned!")
		ids[id] = true
	end
end)


test("EntityDb.queryComponent() gets all entities with a given component", function()
	local db = EntityDb()

	local ids = {}
	for i=1,100 do
		local id = db:createEntity()
		if i%2==0 then
			ids[id] = 5*i
			db:addComponent(id, "number", 5*i)
		end
	end

	local query = db:queryComponent("number")
	local count = 0
	for id, number in pairs(query) do
		count = count + 1
		assert(number == ids[id])
	end
	assert(count == 50)
end)


test("EntityDb.queryEntity() gets all components associated with an entity", function()
	local db = EntityDb()

	local entity
	for i=1,100 do
		local id = db:createEntity()
		if i%2 == 0 then db:addComponent(id, "number", 2) end
		if i%3 == 0 then db:addComponent(id, "string", "hello") end
		if i%5 == 0 then db:addComponent(id, "number2", 4) end
		if i%7 == 0 then db:addComponent(id, "string2", "world") end
		if i == 30 then entity=id end
	end

	local query = db:queryEntity(entity)
	assert(query.number == 2)
	assert(query.string == "hello")
	assert(query.number2 == 4)
	assert(query.string2 == nil)
end)


test("EntityDb.removeComponent() removes components correctly", function()
	local db = EntityDb()

	local id = db:createEntity()
	db:addComponent(id, "number", 2)
	db:addComponent(id, "string", "hello")
	db:addComponent(id, "number2", 4)
	db:addComponent(id, "string2", "world")

	local query = db:queryEntity(id)
	assert(query.number == 2)
	assert(query.string == "hello")
	assert(query.number2 == 4)
	assert(query.string2 == "world")

	db:removeComponent(id, "string2")
	query = db:queryEntity(id)
	assert(query.number == 2)
	assert(query.string == "hello")
	assert(query.number2 == 4)
	assert(query.string2 == nil)

	db:removeComponent(id, "number2")
	query = db:queryEntity(id)
	assert(query.number == 2)
	assert(query.string == "hello")
	assert(query.number2 == nil)
	assert(query.string2 == nil)

	db:removeComponent(id, "string")
	query = db:queryEntity(id)
	assert(query.number == 2)
	assert(query.string == nil)
	assert(query.number2 == nil)
	assert(query.string2 == nil)

	db:removeComponent(id, "number")
	query = db:queryEntity(id)
	assert(query.number == nil)
	assert(query.string == nil)
	assert(query.number2 == nil)
	assert(query.string2 == nil)
end)


test("EntityDb.removeComponent() deletes component table when empty", function()
	local db = EntityDb()
	local id1 = db:createEntity()
	local id2 = db:createEntity()
	db:addComponent(id1, "number", 2)
	db:addComponent(id2, "number", 3)

	assert(db.components.number ~= nil)
	db:removeComponent(id1, "number")
	assert(db.components.number ~= nil)
	db:removeComponent(id2, "number")
	assert(db.components.number == nil)
end)


test("EntityDb.removeComponent() does nothing if the component is not present", function()
	local db = EntityDb()
	local id1 = db:createEntity()
	local id2 = db:createEntity()
	db:addComponent(id1, "number", 2)
	db:addComponent(id2, "number", 3)

	assert(db.components.number ~= nil)
	db:removeComponent(id1, "number")
	assert(db.components.number ~= nil)
	db:removeComponent(id1, "number")
	assert(db.components.number ~= nil)

	db:removeComponent(id2, "number")
	assert(db.components.number == nil)

end)


test("EntityDb.deleteEntity() correctly removes an entity", function()
	local db = EntityDb()
	local id1 = db:createEntity()
	local id2 = db:createEntity()
	db:addComponent(id1, "number", 2)
	db:addComponent(id2, "number", 3)

	local query = db:queryComponent("number")
	assert(query[id1] and query[id2])
	db:deleteEntity(id1)
	query = db:queryComponent("number")
	assert(query[id1] == nil)
	assert(query[id2] ~= nil)

	assert(false == pcall(function()
		local query = db:queryEntity(id1)
	end))
end)


--===== SystemDb tests =====--

local SystemDb = ecs.SystemDb

test("addSystem() correctly sorts systems", function()
	local sdb = SystemDb(nil)
	local str = ""
	sdb:addSystem(function () return {
		update=function(self, dt) str = str .. "c" end,
		priority = 3,
	} end)
	sdb:addSystem{
		update=function(self, dt) str = "a" end,
		priority = 1,
	}
	sdb:addSystem{
		update=function(self, dt) str = str .. "b" end,
		priority = 2,
	}
	sdb:update(0)
	assert(str == "abc")
end)


test("removeSystem() correctly handles things", function()
	local sdb = SystemDb(nil)
	local str = ""
	sdb:addSystem(function () return {
		update=function(self, dt) str = str .. "c" end,
		priority = 3,
	} end)
	sdb:addSystem{
		update=function(self, dt) str = "a" end,
		priority = 1,
	}
	local id = sdb:addSystem{
		update=function(self, dt) str = str .. "b" end,
		priority = 2,
	}
	sdb:update(0)
	assert(str == "abc")

	sdb:removeSystem(id)
	sdb:update(1)
	assert(str == "ac")
end)


print(string.format("ran %d tests, %d failed", testCount, failCount))