source: trunk/game/tests/test_dialogue.py @ 290

Revision 290, 4.5 KB checked in by orlandov, 10 years ago (diff)

Add a new test to fix broken unit test introduced by or1andov--

Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18import sys
19import unittest
20from scripts.dialogue import DialogueEngine
21
22class TestDialogue(unittest.TestCase):
23    def setUp(self):
24        self.tree = {
25            'START': 'main',
26            'AVATAR': 'npc.png',
27            'SECTIONS': {
28                'main': [
29                    { "say": "Greetings stranger" },
30                    { "responses": [
31                        ["Hi, can you tell me where I am?", "friendly"],
32                        ["Watch your words", "aggro"],
33                        ["This one toggles", "toggles", "show == True"],
34                        ["Always display this one", "display", "True and True"],
35                        ["response3", "stop"],
36                    ] }
37                ],
38                'friendly': [
39                    { "say": "You sure are lost" },
40                    { "responses": [
41                        ["Thanks, I know", "thanks"],
42                        ["Wait what did you say before?", "back"],
43                    ] }
44                ],
45                'thanks': [
46                    { "say": "We haven't seen one of your kind in ages" },
47                    { "responses": [
48                        ["Blah blah blah", "foo"],
49                        ["Say the other thing again", "back"],
50                    ] }
51                ],
52            }
53        }
54        # record actions in test_vars
55        test_vars = { "say": None, "responses": [] }
56
57        def say_cb(state, text):
58            state["say"] = text
59
60        self.replies = ["resp1", "back", "stop"]
61
62        def npc_avatar_cb(state, image):
63            state['npc_avatar'] = image
64
65        def responses_cb(state, responses):
66            state['responses'] = responses
67
68        callbacks = {
69            "say": say_cb,
70            "responses": responses_cb,
71            "npc_avatar": npc_avatar_cb
72        }
73
74        self.dialogue = DialogueEngine(self.tree, callbacks, test_vars)
75
76    def assert_say(self, text):
77        self.assertEqual(text, self.dialogue.state['say'])
78
79    def assert_responses(self, responses):
80        self.assertEqual(responses, self.dialogue.state['responses'])
81
82    def assert_npc_image(self, image):
83        self.assertEqual(image, self.dialogue.state['npc_avatar'])
84
85    def test_simple(self):
86        """Test basic dialogue interaction"""
87        self.dialogue.state['show'] = False
88        self.dialogue.run()
89
90        self.assert_npc_image('npc.png')
91        self.assert_say('Greetings stranger')
92        self.assert_responses([
93            ["Hi, can you tell me where I am?", "friendly"],
94            ["Watch your words", "aggro"],
95            ["Always display this one", "display", "True and True"],
96            ["response3", "stop"],
97        ])
98        self.dialogue.reply(0)
99
100        self.assert_say('You sure are lost')
101        self.assert_responses([
102            ["Thanks, I know", "thanks"],
103            ["Wait what did you say before?", "back"]
104        ])
105        self.dialogue.state['show'] = True
106        self.dialogue.reply(1)
107
108        self.assert_say('Greetings stranger')
109        self.assert_responses([
110            ["Hi, can you tell me where I am?", "friendly"],
111            ["Watch your words", "aggro"],
112            ["This one toggles", "toggles", "show == True"],
113            ["Always display this one", "display", "True and True"],
114            ["response3", "stop"],
115        ])
116        self.dialogue.reply(0)
117
118        self.assert_say('You sure are lost')
119        self.dialogue.reply(1)
120
121        self.assert_say('Greetings stranger')
122        self.dialogue.reply(0)
123
124        self.assert_say('You sure are lost')
125        self.dialogue.reply(0)
126
127        self.assert_say("We haven't seen one of your kind in ages")
128        self.dialogue.reply(1)
129
130        self.assert_say('You sure are lost')
131        self.dialogue.reply(1)
132
133        self.assert_say('Greetings stranger')
134
135if __name__ == "__main__":
136    unittest.main()
Note: See TracBrowser for help on using the repository browser.