Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (very debug) selector hijacking API #350

Merged
merged 1 commit into from
May 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()