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

Revision 369, 4.9 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #147. Patch by eliedebrauwer: Added dialogue-validator code and unittests, already capable of structure checking. Unittest fail atm. Still pending for ticket 147 is a standalone validator. comment[s:trac, t:147]

  • Property svn:eol-style set to native
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 os
19import sys
20import unittest
21from scripts.dialogue import DialogueEngine
22from scripts.dialoguevalidator import DialogueValidator
23
24class TestDialogue(unittest.TestCase):
25    def setUp(self):
26        self.tree = {
27            'START': 'main',
28            'AVATAR': 'npc.png',
29            'SECTIONS': {
30                'main': [
31                    { "say": "Greetings stranger" },
32                    { "responses": [
33                        ["Hi, can you tell me where I am?", "friendly"],
34                        ["Watch your words", "aggro"],
35                        ["This one toggles", "toggles", "show == True"],
36                        ["Always display this one", "display", "True and True"],
37                        ["response3", "stop"],
38                    ] }
39                ],
40                'friendly': [
41                    { "say": "You sure are lost" },
42                    { "responses": [
43                        ["Thanks, I know", "thanks"],
44                        ["Wait what did you say before?", "back"],
45                    ] }
46                ],
47                'thanks': [
48                    { "say": "We haven't seen one of your kind in ages" },
49                    { "responses": [
50                        ["Blah blah blah", "foo"],
51                        ["Say the other thing again", "back"],
52                    ] }
53                ],
54            }
55        }
56        # record actions in test_vars
57        test_vars = { "say": None, "responses": [] }
58
59        def sayCb(state, text):
60            state["say"] = text
61
62        self.replies = ["resp1", "back", "stop"]
63
64        def npcAvatarCb(state, image):
65            state['npc_avatar'] = image
66
67        def responsesCb(state, responses):
68            state['responses'] = responses
69
70        callbacks = {
71            "say": sayCb,
72            "responses": responsesCb,
73            "npc_avatar": npcAvatarCb
74        }
75
76        self.dialogue = DialogueEngine(self.tree, callbacks, test_vars)
77
78    def assertSay(self, text):
79        self.assertEqual(text, self.dialogue.state['say'])
80
81    def assertResponses(self, responses):
82        self.assertEqual(responses, self.dialogue.state['responses'])
83
84    def assertNpcImage(self, image):
85        self.assertEqual(image, self.dialogue.state['npc_avatar'])
86
87    def testSimple(self):
88        """Test basic dialogue interaction"""
89        self.dialogue.state['show'] = False
90        self.dialogue.run()
91
92        self.assertNpcImage('npc.png')
93        self.assertSay('Greetings stranger')
94        self.assertResponses([
95            ["Hi, can you tell me where I am?", "friendly"],
96            ["Watch your words", "aggro"],
97            ["Always display this one", "display", "True and True"],
98            ["response3", "stop"],
99        ])
100        self.dialogue.reply(0)
101
102        self.assertSay('You sure are lost')
103        self.assertResponses([
104            ["Thanks, I know", "thanks"],
105            ["Wait what did you say before?", "back"]
106        ])
107        self.dialogue.state['show'] = True
108        self.dialogue.reply(1)
109
110        self.assertSay('Greetings stranger')
111        self.assertResponses([
112            ["Hi, can you tell me where I am?", "friendly"],
113            ["Watch your words", "aggro"],
114            ["This one toggles", "toggles", "show == True"],
115            ["Always display this one", "display", "True and True"],
116            ["response3", "stop"],
117        ])
118        self.dialogue.reply(0)
119
120        self.assertSay('You sure are lost')
121        self.dialogue.reply(1)
122
123        self.assertSay('Greetings stranger')
124        self.dialogue.reply(0)
125
126        self.assertSay('You sure are lost')
127        self.dialogue.reply(0)
128
129        self.assertSay("We haven't seen one of your kind in ages")
130        self.dialogue.reply(1)
131
132        self.assertSay('You sure are lost')
133        self.dialogue.reply(1)
134
135        self.assertSay('Greetings stranger')
136
137    def testAllDialogueFiles(self):
138        """ Runs the validator on all dialogue files available. """
139       
140        val = DialogueValidator()
141        diag_dir = os.path.join(os.path.curdir, "dialogue");
142        for dialogue in os.listdir(diag_dir):
143            fname = os.path.join(diag_dir, dialogue)
144            if os.path.isfile(fname) :
145                val.validateDialogue(fname,".")
146 
147if __name__ == "__main__":
148    unittest.main()
Note: See TracBrowser for help on using the repository browser.