Skip to content

Commit

Permalink
Add (very debug) selector hijacking API
Browse files Browse the repository at this point in the history
  • Loading branch information
edk0 committed May 6, 2016
1 parent a40c3b7 commit 801b86d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions fireplace/dsl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .copy import *
from .evaluator import *
from .hijack import *
from .lazynum import *
from .random_picker import *
from .selector import *
Expand Down
36 changes: 36 additions & 0 deletions fireplace/dsl/hijack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from contextlib import contextmanager
from .selector import Selector


class HijackedSelector(Selector):
def __init__(self, *a, **kw):
raise NotImplementedError

def eval(self, entities, source):
return self._hijack_.eval(entities, source)


def hijack(victim, replace):
if victim.__class__ is not HijackedSelector:
victim._truth_ = victim.__class__
victim.__class__ = HijackedSelector
victim._hijack_ = replace


def unhijack(victim):
try:
victim.__class__ = victim._truth_
except AttributeError as e:
raise ValueError("not a hijacked selector") from e


@contextmanager
def hijacked(victim, replace):
if not isinstance(victim, Selector):
raise TypeError("not a selector: %r" % victim)
prev = victim.__class__
try:
hijack(victim, replace)
yield
finally:
victim.__class__ = prev
10 changes: 10 additions & 0 deletions tests/test_dsl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python
import pytest
from utils import *
from fireplace.dsl import *
from fireplace.exceptions import *
from fireplace.card import Card


Expand Down Expand Up @@ -189,3 +191,11 @@ def test_positional_selectors():
assert len(adjacent) == 2
assert adjacent[0] is wisp2
assert adjacent[1] is wisp3


def test_hijack():
game = prepare_game()
vial = game.player1.give("LOEA16_8")
with hijacked(RANDOM_ENEMY_MINION, FRIENDLY_HERO):
with pytest.raises(GameOver):
vial.play()

0 comments on commit 801b86d

Please sign in to comment.