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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
import sys
import pytest
from sdl2.ext.ebs import Entity, System, Applicator, World
class Position(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class Movement(object):
def __init__(self, vx=0, vy=0):
self.vx = vx
self.vy = vy
class PositionEntity(Entity):
def __init__(self, world, x=0, y=0):
self.position = Position(x, y)
class MovingEntity(Entity):
def __init__(self, world, x=0, y=0, vx=0, vy=0):
self.position = Position(x, y)
self.movement = Movement(vx, vy)
class PosEntity(Entity):
def __init__(self, world, x=0, y=0):
self.pos = Position(x, y)
class PositionSystem(System):
def __init__(self):
super(PositionSystem, self).__init__()
self.componenttypes = (Position,)
def process(self, world, components):
for c in components:
c.x += 1
c.y += 1
class MovementApplicator(Applicator):
def __init__(self):
super(MovementApplicator, self).__init__()
self.componenttypes = (Position, Movement)
def process(self, world, componentsets):
for p, m in componentsets:
p.x += m.vx
p.y += m.vy
class TestSDL2ExtEBS(object):
__tags__ = ["ebs", "sdl2ext"]
def test_Entity(self):
world = World()
world.add_system(PositionSystem())
e = Entity(world)
e2 = Entity(world)
assert isinstance(e, Entity)
assert isinstance(e2, Entity)
assert e != e2
p = PositionEntity(world)
assert isinstance(p, PositionEntity)
assert isinstance(p, Entity)
def test_Entity_id(self):
world = World()
ent1 = Entity(world)
ent2 = Entity(world)
assert ent1.id != ent2.id
def test_Entity_world(self):
world = World()
world2 = World()
ent1 = Entity(world)
ent2 = Entity(world2)
assert ent1.world == world
assert ent1.world != world2
assert ent2.world == world2
assert ent2.world != world
assert ent1.world != ent2.world
def test_Entity_delete(self):
w = World()
e1 = Entity(w)
e2 = Entity(w)
assert len(w.entities) == 2
e1.delete()
assert len(w.entities) == 1
e2.delete()
assert len(w.entities) == 0
# The next two should have no effect
e1.delete()
e2.delete()
def test_Entity__inheritance(self):
world = World()
pos1 = PositionEntity(world)
pos2 = PositionEntity(world, 10, 10)
for p in (pos1, pos2):
assert isinstance(p, PositionEntity)
assert isinstance(p, Entity)
assert isinstance(p.position, Position)
def test_Entity__access(self):
world = World()
pos1 = PositionEntity(world)
pos2 = PosEntity(world)
pos1.position.x = 10
# components are _always_ identified by a lower-case class name.
def sx(p, v):
p.pos.x = v
with pytest.raises(AttributeError):
sx(pos2, 10)
def test_World(self):
w = World()
assert isinstance(w, World)
def test_World_add_remove_system(self):
world = World()
assert isinstance(world, World)
class SimpleSystem(object):
def __init__(self):
self.componenttypes = (Position,)
def process(self, world, components):
pass
for method in (world.add_system, world.remove_system):
for val in (None, "Test", Position, Entity(world)):
with pytest.raises(ValueError):
method(val)
psystem = SimpleSystem()
world.add_system(psystem)
assert len(world.systems) != 0
assert psystem in world.systems
world.remove_system(psystem)
assert len(world.systems) == 0
assert psystem not in world.systems
psystem = PositionSystem()
world.add_system(psystem)
assert len(world.systems) != 0
assert psystem in world.systems
entity = PositionEntity(world)
assert isinstance(entity.position, Position)
world.remove_system(psystem)
assert len(world.systems) == 0
assert psystem not in world.systems
# The data must stay intact in the world, even if the processing
# system has been removed.
assert isinstance(entity.position, Position)
def test_World_entities(self):
w = World()
assert len(w.entities) == 0
for x in range(100):
Entity(w)
assert len(w.entities) == 100
def test_World_delete(self):
w = World()
e1 = Entity(w)
e2 = Entity(w)
assert len(w.entities) == 2
w.delete(e1)
assert len(w.entities) == 1
w.delete(e2)
assert len(w.entities) == 0
# The next two should have no effect
w.delete(e1)
w.delete(e2)
def test_World_delete_entities(self):
w = World()
e1 = Entity(w)
e2 = Entity(w)
assert len(w.entities) == 2
w.delete_entities((e1, e2))
assert len(w.entities) == 0
# The next should have no effect
w.delete_entities((e1, e2))
def test_World_get_entities(self):
w = World()
e1 = PositionEntity(w, 1, 1)
e2 = PositionEntity(w, 1, 2)
assert len(w.get_entities(e1.position)) == 1
e2.position.y = 1
assert len(w.get_entities(e1.position)) == 2
def test_System(self):
world = World()
with pytest.raises(ValueError):
world.add_system(None)
with pytest.raises(ValueError):
world.add_system(1234)
with pytest.raises(ValueError):
world.add_system("Test")
class ErrornousSystem(System):
def __init__(self):
super(ErrornousSystem, self).__init__()
esystem = ErrornousSystem()
# No component types defined.
with pytest.raises(ValueError):
world.add_system(esystem)
assert len(world.systems) == 0
psystem = PositionSystem()
world.add_system(psystem)
assert psystem in world.systems
def test_System_process(self):
world = World()
class ErrornousSystem(System):
def __init__(self):
super(ErrornousSystem, self).__init__()
self.componenttypes = (Position,)
esystem = ErrornousSystem()
world.add_system(esystem)
for x in range(10):
PositionEntity(world)
assert esystem in world.systems
with pytest.raises(NotImplementedError):
world.process()
world2 = World()
psystem = PositionSystem()
world2.add_system(psystem)
for x in range(10):
PositionEntity(world2)
assert psystem in world2.systems
world2.process()
for c in world2.components[Position].values():
assert c.x == 1
assert c.y == 1
world2.process()
for c in world2.components[Position].values():
assert c.x == 2
assert c.y == 2
def test_Applicator(self):
world = World()
class ErrornousApplicator(Applicator):
def __init__(self):
super(ErrornousApplicator, self).__init__()
eapplicator = ErrornousApplicator()
# No component types defined.
with pytest.raises(ValueError):
world.add_system(eapplicator)
assert len(world.systems) == 0
mapplicator = MovementApplicator()
world.add_system(mapplicator)
assert mapplicator in world.systems
def test_Applicator_process(self):
world = World()
class ErrornousApplicator(Applicator):
def __init__(self):
super(ErrornousApplicator, self).__init__()
self.componenttypes = (Position, Movement)
eapplicator = ErrornousApplicator()
world.add_system(eapplicator)
for x in range(10):
MovingEntity(world)
assert eapplicator in world.systems
with pytest.raises(NotImplementedError):
world.process()
world2 = World()
mapplicator = MovementApplicator()
world2.add_system(mapplicator)
for x in range(10):
MovingEntity(world2, vx=1, vy=1)
assert mapplicator in world2.systems
world2.process()
for c in world2.components[Position].values():
assert c.x == 1
assert c.y == 1
world2.process()
for c in world2.components[Position].values():
assert c.x == 2
assert c.y == 2
|