Add timing data to test output
[mudpy.git] / bin / test
1 #!/usr/bin/env python
2 """Regression test script for the mudpy engine."""
3
4 # Copyright (c) 2004-2015 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, '> \(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_actor_disappears = (
101     (1, "> ", "quit"),
102     (0, "You suddenly wonder where .* went\.", ""),
103 )
104
105 test_account0_teardown = (
106     (0, "> ", "quit"),
107     (0, "What would you like to do?", "d"),
108     (0, "Whom would you like to delete?", ""),
109     (0, "What would you like to do?", "p"),
110     (0, "permanently delete your account?", "y"),
111     (0, "Disconnecting...", ""),
112 )
113
114 test_account1_teardown = (
115     (1, "What would you like to do?", "d"),
116     (1, "Whom would you like to delete?", ""),
117     (1, "What would you like to do?", "p"),
118     (1, "permanently delete your account?", "y"),
119     (1, "Disconnecting...", ""),
120 )
121
122 dialogue = (
123     (test_account0_setup, "first account setup"),
124     (test_account1_setup, "second account setup"),
125     (test_actor_appears, "actor spontaneous appearance"),
126     (test_explicit_punctuation, " explicit punctuation"),
127     (test_implicit_punctuation, "implicit punctuation"),
128     (test_typo_replacement, "typo replacement"),
129     (test_sentence_capitalization, "sentence capitalization"),
130     (test_chat_mode, "chat mode"),
131     (test_actor_disappears, "actor spontaneous disappearance"),
132     (test_account0_teardown, "first account teardown"),
133     (test_account1_teardown, "second account teardown"),
134 )
135
136 captures = ["", ""]
137 lusers = [telnetlib.Telnet(), telnetlib.Telnet()]
138 success = True
139 start = time.time()
140 for luser in lusers:
141     luser.open("::1", 6669)
142 for test, description in dialogue:
143     print("\nTesting %s..." % description)
144     test_start = time.time()
145     for conversant, question, answer in test:
146         print("luser%s waiting for: %s" % (conversant, question))
147         index, match, received = lusers[conversant].expect(
148             [question.encode("utf-8")], 5)
149         captures[conversant] += received.decode("utf-8")
150         try:
151             captures[conversant] += lusers[
152                 conversant].read_very_eager().decode("utf-8")
153         except:
154             pass
155         if index is not 0:
156             print("ERROR: luser%s did not receive expected string:\n\n%s\n\n"
157                   "Check the end of capture_%s.log for received data."
158                   % (conversant, question, conversant))
159             success = False
160             break
161         print("luser%s sending: %s" % (conversant, answer))
162         lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
163         captures[conversant] += "%s\r\n" % answer
164     if not success:
165         break
166     print("Completed in %.3f seconds." % (time.time() - test_start))
167 duration = time.time() - start
168 print("")
169 for conversant in range(len(captures)):
170     try:
171         captures[conversant] += lusers[
172             conversant].read_very_eager().decode("utf-8")
173     except:
174         pass
175     lusers[conversant].close()
176     log = open("capture_%s.log" % conversant, "w")
177     log.write(captures[conversant])
178     log.close()
179 print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
180 if success:
181     print("SUCCESS")
182 else:
183     print("FAILURE")
184     sys.exit(1)