summaryrefslogtreecommitdiff
path: root/tests/world.lua
blob: ea35105fca653c00e849a5de8b55d8865a4b7b43 (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
local world = require "world"
local component = require "component"

local M = {}

function M.world()
	local w = world()
	w:object()
	w:object()
	w:object()
	local i = 0
	for _ in w:iterate() do
		i = i + 1
	end
	assert(i == 3)
end

local A = component()
local B = component({A})
local C = component({A}, {B})

function M.object()
	local w = world()
	local o = w:object(A())
	assert(o[A])

	o:disable()
	for _ in w:iterate() do assert(false) end
	for _ in w:iterate(A) do assert(false) end

	o:enable()
	local ok
	for _ in w:iterate() do ok = true end
	assert(ok) ok = false
	for _ in w:iterate(A) do ok = true end
	assert(ok) ok = false

	for c in o:all_components() do
		assert(c.component_type == A)
		ok = true
	end
	assert(ok)
end

function M.object_dependencies()
	local w = world()
	local o = w:object(A())

	assert_error(o.add, o, A)
	o:add(C())
	assert(o[B])
	assert_error(o.remove, o, B)
	assert_error(o.remove, o, A)
	o:remove(C)
	assert(not o[C])
end

return M