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

Revision 370, 5.6 KB checked in by eliedebrauwer, 10 years ago (diff)

Ticket #147. Patch by eliedebrauwer: Added more verbosity in error reporting, updated unittest to be more verbose as well, added standalone validator utility. fixes[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, DialogueFormatException
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        num_faulty_files = 0
143       
144        # Test the dialogue files
145        for dialogue in os.listdir(diag_dir):
146            fname = os.path.join(diag_dir, dialogue)
147            if os.path.isfile(fname) :
148                try:
149                    assert(val.validateDialogueFromFile(fname,"."))
150                except DialogueFormatException as dfe:
151                    print "\nError found in file: ", fname
152                    print "Error was: %s" % (dfe)
153                    num_faulty_files += 1
154       
155        # Test the internal tree
156        #try:
157        #    assert(val.validateDialogue(self.tree,"."))
158        #except DialogueFormatException as dfe:
159        #    print "\nError found in inernal tree: ", fname
160        #    print "Error was: %s" % (dfe)
161        #    num_faulty_files += 1           
162       
163        assert(num_faulty_files == 0)
164 
165if __name__ == "__main__":
166    unittest.main()
Note: See TracBrowser for help on using the repository browser.