-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_game_props.py
More file actions
63 lines (49 loc) · 1.8 KB
/
test_game_props.py
File metadata and controls
63 lines (49 loc) · 1.8 KB
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
from functools import wraps
from hypothesis.strategies import choices
from hypothesis.stateful import RuleBasedStateMachine, rule, precondition
from toolz.functoolz import compose
from core import take, move, initial_state
def stateful(f):
@wraps(f)
def inner(self, *args, **kwargs):
self.state = f(self, self.state, *args, **kwargs)
return inner
def srulep(precond=lambda s: True, **kwargs):
return compose(precondition(precond), rule(**kwargs), stateful)
def eligible_locked_exits(state):
return [
exit_name for exit_name, (req_key, dest) in
state.location.exits.items() if req_key in state.inventory
]
class GameRules(RuleBasedStateMachine):
state = initial_state
@srulep(choice=choices())
def move(self, state, choice):
unlocked_exits = [
exit_name for (exit_name, (req_key, dest)) in
state.location.exits.items() if req_key is None
]
direc = choice(unlocked_exits)
st = move(state, direc)
assert st.location_name == state.location.exits[direc][1]
return st
@srulep(
precond=lambda self: len(self.state.location.items) > 0,
choice=choices()
)
def take(self, state, choice):
thing_name = choice(state.location.items.keys())
st = take(state, thing_name)
assert thing_name in (t.name for t in st.inventory)
assert thing_name not in st.location.items
return st
@srulep(
precond=lambda self: len(eligible_locked_exits(self.state)) > 0,
choice=choices()
)
def move_through_locked_door(self, state, choice):
direc = choice(eligible_locked_exits(state))
st = move(state, direc)
assert st.location_name == state.location.exits[direc][1]
return st
GameRules.TestCase().runTest()