1
0
mirror of https://github.com/gsi-upm/soil synced 2024-09-28 18:41:43 +00:00
soil/tests/test_agents.py

48 lines
1.3 KiB
Python
Raw Normal View History

2022-07-05 10:08:34 +00:00
from unittest import TestCase
import pytest
from soil import agents, environment
from soil import time as stime
2022-07-05 10:08:34 +00:00
class Dead(agents.FSM):
@agents.default_state
@agents.state
def only(self):
2022-10-13 20:43:16 +00:00
return self.die()
2022-07-05 10:08:34 +00:00
2022-07-05 10:08:34 +00:00
class TestMain(TestCase):
2022-10-17 06:58:51 +00:00
def test_die_returns_infinity(self):
'''The last step of a dead agent should return time.INFINITY'''
d = Dead(unique_id=0, model=environment.Environment())
ret = d.step().abs(0)
print(ret, "next")
2022-10-17 11:58:14 +00:00
assert ret == stime.NEVER
2022-10-17 06:58:51 +00:00
2022-07-05 10:08:34 +00:00
def test_die_raises_exception(self):
2022-10-17 06:58:51 +00:00
'''A dead agent should raise an exception if it is stepped after death'''
2022-07-05 10:08:34 +00:00
d = Dead(unique_id=0, model=environment.Environment())
d.step()
2022-10-17 17:29:39 +00:00
with pytest.raises(stime.DeadAgent):
2022-07-05 10:08:34 +00:00
d.step()
2022-07-06 07:23:18 +00:00
2022-10-17 06:58:51 +00:00
def test_agent_generator(self):
'''
The step function of an agent could be a generator. In that case, the state of the
agent will be resumed after every call to step.
'''
class Gen(agents.BaseAgent):
def step(self):
a = 0
for i in range(5):
yield a
a += 1
e = environment.Environment()
g = Gen(model=e, unique_id=e.next_id())
for i in range(5):
t = g.step()
assert t == i