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