Explicitly invoke Python 3.x in shebang lines
[mudpy.git] / bin / test
1 #!/usr/bin/env python3
2 """Regression test script for the mudpy engine."""
3
4 # Copyright (c) 2004-2016 Jeremy Stanley <fungi@yuggoth.org>. Permission
5 # to use, copy, modify, and distribute this software is granted under
6 # terms provided in the LICENSE file distributed with this software.
7
8 import sys
9 import telnetlib
10 import time
11
12 test_account0_setup = (
13     (0, "Identify yourself:", "luser0"),
14     (0, "Enter your choice:", "n"),
15     (0, "Enter a new password for \"luser0\":", "Test123"),
16     (0, "Enter the same new password again:", "Test123"),
17     (0, "What would you like to do\?", "c"),
18     (0, "Pick a birth gender for your new avatar:", "f"),
19     (0, "Choose a name for her:", "1"),
20     (0, "What would you like to do?", "a"),
21     (0, "Whom would you like to awaken?", ""),
22 )
23
24 test_account1_setup = (
25     (1, "Identify yourself:", "luser1"),
26     (1, "Enter your choice:", "n"),
27     (1, "Enter a new password for \"luser1\":", "Test456"),
28     (1, "Enter the same new password again:", "Test456"),
29     (1, "What would you like to do\?", "c"),
30     (1, "Pick a birth gender for your new avatar:", "m"),
31     (1, "Choose a name for him:", "1"),
32     (1, "What would you like to do?", "a"),
33     (1, "Whom would you like to awaken?", ""),
34 )
35
36 test_actor_appears = (
37     (0, "You suddenly realize that .* is here\.", ""),
38 )
39
40 test_explicit_punctuation = (
41     (0, "> ", "say Hello there!"),
42     (0, 'You exclaim, "Hello there\!"', ""),
43     (1, 'exclaims, "Hello there\!"', "say And you are?"),
44     (1, 'You ask, "And you are\?"', ""),
45     (0, 'asks, "And you are\?"', "say I'm me, of course."),
46     (0, 'You say, "I\'m me, of course\."', ""),
47     (1, 'says, "I\'m me, of course\."', "say I wouldn't be so sure..."),
48     (1, 'You muse, "I wouldn\'t be so sure\.\.\."', ""),
49     (0, 'muses, "I wouldn\'t be so sure\.\.\."', "say You mean,"),
50     (0, 'You begin, "You mean,"', ""),
51     (1, 'begins, "You mean,"', "say I know-"),
52     (1, 'You begin, "I know-"', ""),
53     (0, 'begins, "I know-"', "say Don't interrupt:"),
54     (0, 'You begin, "Don\'t interrupt:"', ""),
55     (1, 'begins, "Don\'t interrupt:"', "say I wasn't interrupting;"),
56     (1, 'You begin, "I wasn\'t interrupting;"', ""),
57     (0, 'begins, "I wasn\'t interrupting;"', ""),
58 )
59
60 test_implicit_punctuation = (
61     (0, '> ', "say Whatever"),
62     (0, 'You say, "Whatever\."', ""),
63     (1, 'says, "Whatever\."', ""),
64 )
65
66 test_typo_replacement = (
67     (1, '> ', "say That's what i think."),
68     (1, 'You say, "That\'s what I think\."', ""),
69     (0, 'says, "That\'s what I think\."', "say You know what i'd like."),
70     (0, 'You say, "You know what I\'d like\."', ""),
71     (1, 'says, "You know what I\'d like\."', "say Then i'll tell you."),
72     (1, 'You say, "Then I\'ll tell you\."', ""),
73     (0, 'says, "Then I\'ll tell you\."', "say Now i'm ready."),
74     (0, 'You say, "Now I\'m ready\."', ""),
75     (1, 'says, "Now I\'m ready\."', "say That's teh idea."),
76     (1, 'You say, "That\'s the idea\."', ""),
77     (0, 'says, "That\'s the idea\."', "say It's what theyre saying."),
78     (0, 'You say, "It\'s what they\'re saying\."', ""),
79     (1, 'says, "It\'s what they\'re saying\."', "say Well, youre right."),
80     (1, 'You say, "Well, you\'re right\."', ""),
81     (0, 'says, "Well, you\'re right\."', ""),
82 )
83
84 test_sentence_capitalization = (
85     (0, "> ", "say this sentence should start with a capital T."),
86     (0, 'You say, "This sentence', ""),
87     (1, 'says, "This sentence', ""),
88 )
89
90 test_chat_mode = (
91     (1, '> ', "chat"),
92     (1, '(?s)Entering chat mode .*> \(chat\) ', "Feeling chatty."),
93     (1, 'You say, "Feeling chatty\."', "!chat"),
94     (0, 'says, "Feeling chatty\."', ""),
95     (1, '> ', "say Now less chatty."),
96     (1, 'You say, "Now less chatty\."', ""),
97     (0, 'says, "Now less chatty\."', ""),
98 )
99
100 test_movement = (
101     (0, "> ", "move north"),
102     (0, "You exit to the north\.", ""),
103     (1, "exits to the north\.", "move north"),
104     (0, "arrives from the south\.", "move south"),
105     (0, "You exit to the south\.", ""),
106     (1, "exits to the south\.", "move south"),
107     (0, "arrives from the north\.", "move east"),
108     (0, "You exit to the east\.", ""),
109     (1, "exits to the east\.", "move east"),
110     (0, "arrives from the west\.", "move west"),
111     (0, "You exit to the west\.", ""),
112     (1, "exits to the west\.", "move west"),
113     (0, "arrives from the east\.", "move up"),
114     (0, "You exit upward\.", ""),
115     (1, "exits upward\.", "move up"),
116     (0, "arrives from below\.", "move down"),
117     (0, "You exit downward\.", ""),
118     (1, "exits downward\.", "move down"),
119     (0, "arrives from above\.", ""),
120 )
121
122 test_actor_disappears = (
123     (1, "> ", "quit"),
124     (0, "You suddenly wonder where .* went\.", ""),
125 )
126
127 test_account1_teardown = (
128     (1, "What would you like to do?", "d"),
129     (1, "Whom would you like to delete?", ""),
130     (1, "What would you like to do?", "p"),
131     (1, "permanently delete your account?", "y"),
132     (1, "Disconnecting...", ""),
133 )
134
135 test_admin_setup = (
136     (2, "Identify yourself:", "testadmin"),
137     (2, "Enter your choice:", "n"),
138     (2, "Enter a new password for \"testadmin\":", "Test789"),
139     (2, "Enter the same new password again:", "Test789"),
140     (2, "What would you like to do\?", "c"),
141     (2, "Pick a birth gender for your new avatar:", "m"),
142     (2, "Choose a name for him:", "1"),
143     (2, "What would you like to do?", "a"),
144     (2, "Whom would you like to awaken?", ""),
145 )
146
147 test_admin_restriction = (
148     (0, "> ", "help halt"),
149     (0, "That is not an available command\.", "halt"),
150     (0, '(not sure what "halt" means|Arglebargle, glop-glyf)', ""),
151 )
152
153 test_admin_help = (
154     (2, "> ", "help"),
155     (2, "halt.*Shut down the world\.", "help halt"),
156     (2, "This will save all active accounts", ""),
157 )
158
159 dialogue = (
160     (test_account0_setup, "first account setup"),
161     (test_account1_setup, "second account setup"),
162     (test_actor_appears, "actor spontaneous appearance"),
163     (test_explicit_punctuation, " explicit punctuation"),
164     (test_implicit_punctuation, "implicit punctuation"),
165     (test_typo_replacement, "typo replacement"),
166     (test_sentence_capitalization, "sentence capitalization"),
167     (test_chat_mode, "chat mode"),
168     (test_movement, "movement"),
169     (test_actor_disappears, "actor spontaneous disappearance"),
170     (test_account1_teardown, "second account teardown"),
171     (test_admin_setup, "admin account setup"),
172     (test_admin_restriction, "restricted admin commands"),
173     (test_admin_help, "admin help"),
174 )
175
176 captures = ["", "", ""]
177 lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
178 success = True
179 start = time.time()
180 for luser in lusers:
181     luser.open("::1", 6669)
182 for test, description in dialogue:
183     print("\nTesting %s..." % description)
184     test_start = time.time()
185     for conversant, question, answer in test:
186         print("luser%s waiting for: %s" % (conversant, question))
187         index, match, received = lusers[conversant].expect(
188             [question.encode("utf-8")], 5)
189         captures[conversant] += received.decode("utf-8")
190         try:
191             captures[conversant] += lusers[
192                 conversant].read_very_eager().decode("utf-8")
193         except:
194             pass
195         if index is not 0:
196             print("ERROR: luser%s did not receive expected string:\n\n%s\n\n"
197                   "Check the end of capture_%s.log for received data."
198                   % (conversant, question, conversant))
199             success = False
200             break
201         print("luser%s sending: %s" % (conversant, answer))
202         lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
203         captures[conversant] += "%s\r\n" % answer
204     if not success:
205         break
206     print("Completed in %.3f seconds." % (time.time() - test_start))
207 duration = time.time() - start
208 print("")
209 for conversant in range(len(captures)):
210     try:
211         captures[conversant] += lusers[
212             conversant].read_very_eager().decode("utf-8")
213     except:
214         pass
215     lusers[conversant].close()
216     logfile = "capture_%s.log" % conversant
217     print("Recording session %s as %s." % (conversant, logfile))
218     log = open(logfile, "w")
219     log.write(captures[conversant])
220     log.close()
221 print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
222 if success:
223     print("SUCCESS")
224 else:
225     print("FAILURE")
226     sys.exit(1)