source: nagslang/tests/test_environment.py@ 28:c03982fe3c70

Last change on this file since 28:c03982fe3c70 was 28:c03982fe3c70, checked in by Jeremy Thurgood <firxen@…>, 9 years ago

Protagonist and environment.

File size: 6.0 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
92class TestActions(TestCase):
93 def setUp(self):
94 self.state = {}
95
96 def make_action_func(self, name):
97 self.state.setdefault(name, [])
98
99 def action_func(protagonist, target):
100 self.state[name].append((protagonist, target))
101 return len(self.state[name])
102
103 return action_func
104
105 def make_action(self, name, condition=None):
106 return environment.Action(self.make_action_func(name), condition)
107
108 def assert_state(self, **kw):
109 self.assertEqual(self.state, kw)
110
111 def assert_action_selected(self, action, interactible, protagonist):
112 self.assertEqual(action, interactible.select_action(protagonist))
113
114 def test_unconditional_action(self):
115 action = self.make_action('action')
116 self.assert_state(action=[])
117 self.assertTrue(action.check(None))
118 self.assert_state(action=[])
119 self.assertEqual(1, action.perform('p', 't'))
120 self.assert_state(action=[('p', 't')])
121 self.assertEqual(2, action.perform('p2', 't2'))
122 self.assert_state(action=[('p', 't'), ('p2', 't2')])
123
124 def test_conditional_action(self):
125 yes_action = self.make_action('yes_action', YesCondition())
126 no_action = self.make_action('no_action', NoCondition())
127 self.assert_state(yes_action=[], no_action=[])
128 self.assertTrue(yes_action.check(None))
129 self.assert_state(yes_action=[], no_action=[])
130 self.assertFalse(no_action.check(None))
131 self.assert_state(yes_action=[], no_action=[])
132 self.assertEqual(1, yes_action.perform('p', 't'))
133 self.assert_state(yes_action=[('p', 't')], no_action=[])
134
135 def test_perform_bad_action(self):
136 action = self.make_action('action', NoCondition())
137 self.assert_state(action=[])
138 self.assertFalse(action.check(None))
139 self.assert_state(action=[])
140 self.assertRaises(ValueError, action.perform, 'p', 't')
141
142 def test_interactible_no_actions(self):
143 interactible = environment.Interactible()
144 self.assert_action_selected(None, interactible, None)
145
146 def test_interactible_unconditional_action(self):
147 action = self.make_action('action')
148 interactible = environment.Interactible(action)
149 self.assert_action_selected(action, interactible, None)
150
151 def test_interactible_conditional_actions(self):
152 wolf_action = self.make_action('wolf', environment.WolfFormCondition())
153 item_action = self.make_action(
154 'item', environment.ItemRequiredCondition('item'))
155 interactible = environment.Interactible(wolf_action, item_action)
156 self.assert_action_selected(
157 wolf_action, interactible, FakeProtagonist(wolf=True))
158 self.assert_action_selected(
159 item_action, interactible, FakeProtagonist(item=True))
160 self.assert_action_selected(
161 wolf_action, interactible, FakeProtagonist(wolf=True, item=True))
Note: See TracBrowser for help on using the repository browser.