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

Revision 378, 6.3 KB checked in by zenbitz, 10 years ago (diff)

Fixed internal tree test

  • 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            'NPC': 'Mr. Npc',
28            'START': 'main',
29            'AVATAR': 'gui/icons/npc.png',
30            'SECTIONS': {
31                'main': [
32                    { "say": "Greetings stranger" },
33                    { "responses": [
34                        ["Hi, can you tell me where I am?", "friendly"],
35                        ["Watch your words", "aggro"],
36                        ["This one toggles", "toggles", "show == True"],
37                        ["Always display this one", "display", "True and True"],
38                        ["response3", "end"],
39                    ] }
40                ],
41                'friendly': [
42                    { "say": "You sure are lost" },
43                    { "responses": [
44                        ["Thanks, I know", "thanks"],
45                        ["Wait what did you say before?", "back"],
46                    ] }
47                ],
48                'aggro': [
49                    { "say": "Die Pig! PAAAAR!!!!" },
50                    { "responses": [
51                        ["ruh-ro raggy!", "toggles"],
52                        ["Uh, just kidding??", "back"],
53                    ] }
54                ],
55                'toggles': [
56                    { "say": "you turn me on!" },
57                    { "responses": [
58                        ["you turn me off", "back"],
59                    ] }
60                ],
61                'display': [
62                    { "say": "Forever Young!" },
63                    { "responses": [
64                        ["Alphaville sucks!", "back"],
65                    ] }
66                ],
67                'thanks': [
68                    { "say": "We haven't seen one of your kind in ages" },
69                    { "responses": [
70                        ["Blah blah blah", "display"],
71                        ["Say the other thing again", "back"],
72                    ] }
73                ],
74            }
75        }
76        # record actions in test_vars
77        test_vars = { "say": None, "responses": [] }
78
79        def sayCb(state, text):
80            state["say"] = text
81
82        self.replies = ["resp1", "back", "end"]
83
84        def npcAvatarCb(state, image):
85            state['npc_avatar'] = image
86
87        def responsesCb(state, responses):
88            state['responses'] = responses
89
90        callbacks = {
91            "say": sayCb,
92            "responses": responsesCb,
93            "npc_avatar": npcAvatarCb
94        }
95
96        self.dialogue = DialogueEngine(self.tree, callbacks, test_vars)
97
98    def assertSay(self, text):
99        self.assertEqual(text, self.dialogue.state['say'])
100
101    def assertResponses(self, responses):
102        self.assertEqual(responses, self.dialogue.state['responses'])
103
104    def assertNpcImage(self, image):
105        self.assertEqual(image, self.dialogue.state['npc_avatar'])
106
107    def testSimple(self):
108        """Test basic dialogue interaction"""
109        self.dialogue.state['show'] = False
110        self.dialogue.run()
111
112        self.assertNpcImage('gui/icons/npc.png')
113        self.assertSay('Greetings stranger')
114        self.assertResponses([
115            ["Hi, can you tell me where I am?", "friendly"],
116            ["Watch your words", "aggro"],
117            ["Always display this one", "display", "True and True"],
118            ["response3", "end"],
119        ])
120        self.dialogue.reply(0)
121
122        self.assertSay('You sure are lost')
123        self.assertResponses([
124            ["Thanks, I know", "thanks"],
125            ["Wait what did you say before?", "back"]
126        ])
127        self.dialogue.state['show'] = True
128        self.dialogue.reply(1)
129
130        self.assertSay('Greetings stranger')
131        self.assertResponses([
132            ["Hi, can you tell me where I am?", "friendly"],
133            ["Watch your words", "aggro"],
134            ["This one toggles", "toggles", "show == True"],
135            ["Always display this one", "display", "True and True"],
136            ["response3", "end"],
137        ])
138        self.dialogue.reply(0)
139
140        self.assertSay('You sure are lost')
141        self.dialogue.reply(1)
142
143        self.assertSay('Greetings stranger')
144        self.dialogue.reply(0)
145
146        self.assertSay('You sure are lost')
147        self.dialogue.reply(0)
148
149        self.assertSay("We haven't seen one of your kind in ages")
150        self.dialogue.reply(1)
151
152        self.assertSay('You sure are lost')
153        self.dialogue.reply(1)
154
155        self.assertSay('Greetings stranger')
156
157    def testAllDialogueFiles(self):
158        """ Runs the validator on all dialogue files available. """
159       
160        val = DialogueValidator()
161        diag_dir = os.path.join(os.path.curdir, "dialogue");
162        num_faulty_files = 0
163       
164        # Test the dialogue files
165        for dialogue in os.listdir(diag_dir):
166            fname = os.path.join(diag_dir, dialogue)
167            if os.path.isfile(fname) :
168                try:
169                    assert(val.validateDialogueFromFile(fname,"."))
170                except DialogueFormatException as dfe:
171                    print "\nError found in file: ", fname
172                    print "Error was: %s" % (dfe)
173                    num_faulty_files += 1
174       
175        # Test the internal tree
176        try:
177            assert(val.validateDialogue(self.tree,"."))
178        except DialogueFormatException, dfe:
179            print "\nError found in internal tree: ", fname
180            print "Error was: %s" % (dfe)
181            num_faulty_files += 1           
182       
183        assert(num_faulty_files == 0)
184 
185if __name__ == "__main__":
186    unittest.main()
Note: See TracBrowser for help on using the repository browser.