def test_fuzzy_actor_converter(): """Tests fuzzy matching for Actor converter.""" game = make_test_game(["Alice", "Bob"]) @inject_converters def hey(game: Game, actor: Actor) -> str: return f"Hello, {actor.name}" assert hey(game, "alice") == "Hello, Alice"
def test_status_event(): """Tests status change events.""" actor_names = ["Alpha", "Bravo"] game = make_test_game(actor_names) log = [] class Chk(Subscriber): @handler def f(self, event: EStatusChange): log.append(event.new_val) Chk(game) game.actors[0].status["key1"] = 2 assert log == [2]
def test_activation_by_event(): """Tests activation via events, directly or via converters.""" game = make_test_game(["Alice", "Bob"]) alice = game.actors[0] bob = game.actors[1] a_v = alice.abilities[0] # VoteAbility(game, owner=alice, name="Vote") b_v = bob.abilities[0] # VoteAbility(game, owner=bob, name="Vote") game.phase_system.bump_phase() # start the day tally: Tally = game.aux.filter_by_type(Tally)[0] game.process_event(EActivate(game, a_v, target=bob), process_now=True) assert tally.results.vote_leaders == [bob] game.process_event(EActivate(game, "Alice/ability/Vote", target="unvote"), process_now=True) assert tally.results.vote_leaders == []
def test_alive_constraint(): """Tests alive constraint.""" game = make_test_game(["Alice", "Bob", "Charlie"]) alice = game.actors[0] bob = game.actors[1] # a_v = alice.abilities[0] # VoteAbility(game, owner=alice, name="Vote") b_v = bob.abilities[0] # VoteAbility(game, owner=bob, name="Vote") tally: Tally = game.aux.filter_by_type(Tally)[0] game.phase_system.bump_phase() # start the day # Test whether alive constraint works :) bob.status["dead"] = True e = EActivate(game, b_v, target=alice) # should fail to activate - Bob is dead game.process_event(e, process_now=True) assert tally.results.vote_counts == []
def test_phase_constraint(): """Tests phase constraint for voting.""" game = make_test_game(["Alice", "Bob"]) alice = game.actors[0] bob = game.actors[1] a_v = alice.abilities[0] # VoteAbility(game, owner=alice, name="Vote") b_v = bob.abilities[0] # VoteAbility(game, owner=bob, name="Vote") tally: Tally = game.aux.filter_by_type(Tally)[0] # Won't work - game still in "startup" game.process_event(EActivate(game, a_v, target=bob), process_now=True) assert tally.results.vote_leaders == [] game.phase_system.bump_phase() # start the day # Now it should work :) game.process_event(EActivate(game, a_v, target=bob), process_now=True) assert tally.results.vote_leaders == [bob]
def test_gen_action_ability(): """Tests action and ability generation.""" actor_names = ["Alpha", "Bravo"] def fake_action(self: Action): assert self.game.actor_names == actor_names AcFake = Action.generate(fake_action, name="AcFake", doc="Fake action") AbFake = Ability.generate(AcFake, name="AbFake", doc="Fake ability") @Ability.generate def AbFake2(self: Action): """fake ability 2""" assert self.game.actor_names == actor_names game = make_test_game(actor_names) a_abil = AbFake(game, owner=game.actors[0], name="a_abil") a_abil.activate() b_abil = AbFake2(game, owner="Bravo", name="b_abil") b_abil.activate()
def test_builder_direct(): """Tests the `test_builder()` function directly.""" actor_names = ["Wendy", "Pickle"] game = make_test_game(actor_names) assert game.actor_names == actor_names
def test_events_and_subscribers(): """General test of events and subscribers. Also makes sure order is deterministic. """ actor_names = ["Alpha", "Bravo"] game = make_test_game(actor_names) log = [] class EFake(Event): """Fake event""" class X1(Action): def doit(self): log.append("X1") class A(Subscriber): @handles(Event) def f(self, event: Event): log.append("A.f") class B(A): @handler def g(self, event: EFake) -> None: log.append("B.g") class C(Subscriber): @handles(EFake) def h1(self, event) -> List[X1]: log.append("C.h1") return [X1(self.game, self)] @handler def h2(self, event: X1.Pre) -> List[Action]: log.append("C.h2") return [] @handler def h3(self, event: X1.Post) -> None: log.append("C.h3") # Also legal: # -> Union[List[Action], List[X1]] # Illegal handlers # @handles(Event) # def z1(self, event) -> Action: # return Action(self.game, self) # @handles(Event) # def z2(self, event) -> Optional[Action]: # return None a = A(game) b = B(game) c = C(game) e = EFake(game) game.process_event(e, process_now=True) assumed_log = [ "B.g", "C.h1", "A.f", "A.f", "C.h2", "A.f", "A.f", "X1", "C.h3", "A.f", "A.f", ] assert log == assumed_log