85aa3f3843143cf6001128c9617ca66a710b9e42
[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_element = (
161     (2, "> ", "show element internal:counters"),
162     (2, "These are the properties of the \"internal:counters\" element "
163         "\(in.*data/internal\.yaml\"\):.*   \x1b\[32melapsed: "
164         "\x1b\[31m[0-9]+\x1b\[0m", ""),
165 )
166
167 test_show_log = (
168     (2, "> ", "show log"),
169     (2, "There are [0-9]+ log lines in memory and [0-9]+ at or above level "
170         "[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
171 )
172
173 test_log_no_errors = (
174     (2, "> ", "show log 7"),
175     (2, "None of the [0-9]+ lines in memory matches your request\.", ""),
176 )
177
178 dialogue = (
179     (test_account0_setup, "first account setup"),
180     (test_account1_setup, "second account setup"),
181     (test_actor_appears, "actor spontaneous appearance"),
182     (test_explicit_punctuation, " explicit punctuation"),
183     (test_implicit_punctuation, "implicit punctuation"),
184     (test_typo_replacement, "typo replacement"),
185     (test_sentence_capitalization, "sentence capitalization"),
186     (test_chat_mode, "chat mode"),
187     (test_movement, "movement"),
188     (test_actor_disappears, "actor spontaneous disappearance"),
189     (test_account1_teardown, "second account teardown"),
190     (test_admin_setup, "admin account setup"),
191     (test_admin_restriction, "restricted admin commands"),
192     (test_admin_help, "admin help"),
193     (test_show_element, "show element"),
194     (test_show_log, "show log"),
195     (test_log_no_errors, "no errors logged"),
196 )
197
198 captures = ["", "", ""]
199 lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
200 success = True
201 start = time.time()
202 for luser in lusers:
203     luser.open("::1", 6669)
204 for test, description in dialogue:
205     print("\nTesting %s..." % description)
206     test_start = time.time()
207     for conversant, question, answer in test:
208         print("luser%s waiting for: %s" % (conversant, question))
209         index, match, received = lusers[conversant].expect(
210             [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
211         captures[conversant] += received.decode("utf-8")
212         try:
213             captures[conversant] += lusers[
214                 conversant].read_very_eager().decode("utf-8")
215         except:
216             pass
217         if index is not 0:
218             print("ERROR: luser%s did not receive expected string:\n\n%s\n\n"
219                   "Check the end of capture_%s.log for received data."
220                   % (conversant, question, conversant))
221             success = False
222             break
223         print("luser%s sending: %s" % (conversant, answer))
224         lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
225         captures[conversant] += "%s\r\n" % answer
226     if not success:
227         break
228     print("Completed in %.3f seconds." % (time.time() - test_start))
229 duration = time.time() - start
230 print("")
231 for conversant in range(len(captures)):
232     try:
233         captures[conversant] += lusers[
234             conversant].read_very_eager().decode("utf-8")
235     except:
236         pass
237     lusers[conversant].close()
238     logfile = "capture_%s.log" % conversant
239     print("Recording session %s as %s." % (conversant, logfile))
240     log = open(logfile, "w")
241     log.write(captures[conversant])
242     log.close()
243 print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
244 if success:
245     print("SUCCESS")
246 else:
247     print("FAILURE")
248     sys.exit(1)