source: nagslang/tests/test_environment.py@ 138:366b334a7018

Last change on this file since 138:366b334a7018 was 32:0e49648f8d74, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Arbitrary function condition.

File size: 6.2 KB
Line 
1from unittest import TestCase
2
3from nagslang import environment
4
5
6class YesCondition(environment.ProtagonistCondition):
7 def check(self, protagonist):
8 return True
9
10
11class NoCondition(environment.ProtagonistCondition):
12 def check(self, protagonist):
13 return False
14
15
16class ErrorCondition(environment.ProtagonistCondition):
17 def check(self, protagonist):
18 raise Exception()
19
20
21class FakeProtagonist(object):
22 def __init__(self, wolf=False, human=False, item=False):
23 self._wolf = wolf
24 self._human = human
25 self._item = item
26
27 def in_wolf_form(self):
28 return self._wolf
29
30 def in_human_form(self):
31 return self._human
32
33 def has_item(self, item):
34 return self._item
35
36
37class TestConditions(TestCase):
38 def assert_met(self, condition, protagonist=None):
39 self.assertTrue(condition.check(protagonist))
40
41 def assert_not_met(self, condition, protagonist=None):
42 self.assertFalse(condition.check(protagonist))
43
44 def assert_error(self, condition, protagonist=None):
45 self.assertRaises(Exception, condition.check, protagonist)
46
47 def test_test_conditions(self):
48 self.assert_met(YesCondition())
49 self.assert_not_met(NoCondition())
50 self.assert_error(ErrorCondition())
51
52 def test_all_conditions(self):
53 yes = YesCondition()
54 no = NoCondition()
55 err = ErrorCondition()
56 self.assert_met(environment.AllConditions(yes, yes))
57 self.assert_not_met(environment.AllConditions(yes, no))
58 self.assert_not_met(environment.AllConditions(no, err))
59 self.assert_error(environment.AllConditions(err, yes))
60 self.assert_error(environment.AllConditions(yes, err))
61
62 def test_any_condition(self):
63 yes = YesCondition()
64 no = NoCondition()
65 err = ErrorCondition()
66 self.assert_met(environment.AnyCondition(no, yes))
67 self.assert_not_met(environment.AnyCondition(no, no))
68 self.assert_met(environment.AnyCondition(yes, err))
69 self.assert_error(environment.AnyCondition(err, yes))
70 self.assert_error(environment.AnyCondition(no, err))
71
72 def test_wolf_form_condition(self):
73 wolf = environment.WolfFormCondition()
74 self.assert_met(wolf, FakeProtagonist(wolf=True, human=True))
75 self.assert_met(wolf, FakeProtagonist(wolf=True, human=False))
76 self.assert_not_met(wolf, FakeProtagonist(wolf=False, human=True))
77 self.assert_not_met(wolf, FakeProtagonist(wolf=False, human=False))
78
79 def test_human_form_condition(self):
80 human = environment.HumanFormCondition()
81 self.assert_met(human, FakeProtagonist(human=True, wolf=True))
82 self.assert_met(human, FakeProtagonist(human=True, wolf=False))
83 self.assert_not_met(human, FakeProtagonist(human=False, wolf=True))
84 self.assert_not_met(human, FakeProtagonist(human=False, wolf=False))
85
86 def test_item_required_condition(self):
87 item = environment.ItemRequiredCondition('item')
88 self.assert_met(item, FakeProtagonist(item=True))
89 self.assert_not_met(item, FakeProtagonist(item=False))
90
91 def test_function_condition(self):
92 condition = environment.FunctionCondition(lambda p: p.startswith('x'))
93 self.assert_met(condition, 'xxx')
94 self.assert_not_met(condition, 'yyy')
95
96
97class TestActions(TestCase):
98 def setUp(self):
99 self.state = {}
100
101 def make_action_func(self, name):
102 self.state.setdefault(name, [])
103
104 def action_func(protagonist, target):
105 self.state[name].append((protagonist, target))
106 return len(self.state[name])
107
108 return action_func
109
110 def make_action(self, name, condition=None):
111 return environment.Action(self.make_action_func(name), condition)
112
113 def assert_state(self, **kw):
114 self.assertEqual(self.state, kw)
115
116 def assert_action_selected(self, action, interactible, protagonist):
117 self.assertEqual(action, interactible.select_action(protagonist))
118
119 def test_unconditional_action(self):
120 action = self.make_action('action')
121 self.assert_state(action=[])
122 self.assertTrue(action.check(None))
123 self.assert_state(action=[])
124 self.assertEqual(1, action.perform('p', 't'))
125 self.assert_state(action=[('p', 't')])
126 self.assertEqual(2, action.perform('p2', 't2'))
127 self.assert_state(action=[('p', 't'), ('p2', 't2')])
128
129 def test_conditional_action(self):
130 yes_action = self.make_action('yes_action', YesCondition())
131 no_action = self.make_action('no_action', NoCondition())
132 self.assert_state(yes_action=[], no_action=[])
133 self.assertTrue(yes_action.check(None))
134 self.assert_state(yes_action=[], no_action=[])
135 self.assertFalse(no_action.check(None))
136 self.assert_state(yes_action=[], no_action=[])
137 self.assertEqual(1, yes_action.perform('p', 't'))
138 self.assert_state(yes_action=[('p', 't')], no_action=[])
139
140 def test_perform_bad_action(self):
141 action = self.make_action('action', NoCondition())
142 self.assert_state(action=[])
143 self.assertFalse(action.check(None))
144 self.assert_state(action=[])
145 self.assertRaises(ValueError, action.perform, 'p', 't')
146
147 def test_interactible_no_actions(self):
148 interactible = environment.Interactible()
149 self.assert_action_selected(None, interactible, None)
150
151 def test_interactible_unconditional_action(self):
152 action = self.make_action('action')
153 interactible = environment.Interactible(action)
154 self.assert_action_selected(action, interactible, None)
155
156 def test_interactible_conditional_actions(self):
157 wolf_action = self.make_action('wolf', environment.WolfFormCondition())
158 item_action = self.make_action(
159 'item', environment.ItemRequiredCondition('item'))
160 interactible = environment.Interactible(wolf_action, item_action)
161 self.assert_action_selected(
162 wolf_action, interactible, FakeProtagonist(wolf=True))
163 self.assert_action_selected(
164 item_action, interactible, FakeProtagonist(item=True))
165 self.assert_action_selected(
166 wolf_action, interactible, FakeProtagonist(wolf=True, item=True))
Note: See TracBrowser for help on using the repository browser.