blob: 3a463558fb78e121f9f1b77ba4742735f29dc3b6 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Christian Heimesd8654cf2007-12-02 15:22:16 +00002"""
3Test script for the 'cmd' module
4Original by Michael Schneider
5"""
6
7
Christian Heimesd8654cf2007-12-02 15:22:16 +00008import cmd
9import sys
Christian Heimes2137b6a2007-12-02 16:50:20 +000010import re
R. David Murray7905d612010-08-01 03:31:09 +000011import unittest
12import io
Victor Stinner45df8202010-04-28 22:31:17 +000013from test import support
Christian Heimesd8654cf2007-12-02 15:22:16 +000014
15class samplecmdclass(cmd.Cmd):
16 """
17 Instance the sampleclass:
18 >>> mycmd = samplecmdclass()
19
20 Test for the function parseline():
21 >>> mycmd.parseline("")
22 (None, None, '')
23 >>> mycmd.parseline("?")
24 ('help', '', 'help ')
25 >>> mycmd.parseline("?help")
26 ('help', 'help', 'help help')
27 >>> mycmd.parseline("!")
28 ('shell', '', 'shell ')
29 >>> mycmd.parseline("!command")
30 ('shell', 'command', 'shell command')
31 >>> mycmd.parseline("func")
32 ('func', '', 'func')
33 >>> mycmd.parseline("func arg1")
34 ('func', 'arg1', 'func arg1')
35
36
37 Test for the function onecmd():
38 >>> mycmd.onecmd("")
39 >>> mycmd.onecmd("add 4 5")
40 9
41 >>> mycmd.onecmd("")
42 9
43 >>> mycmd.onecmd("test")
44 *** Unknown syntax: test
45
46 Test for the function emptyline():
47 >>> mycmd.emptyline()
48 *** Unknown syntax: test
49
50 Test for the function default():
51 >>> mycmd.default("default")
52 *** Unknown syntax: default
53
54 Test for the function completedefault():
55 >>> mycmd.completedefault()
56 This is the completedefault methode
57 >>> mycmd.completenames("a")
58 ['add']
59
60 Test for the function completenames():
61 >>> mycmd.completenames("12")
62 []
63 >>> mycmd.completenames("help")
Benjamin Petersona28e7022010-01-09 18:53:06 +000064 ['help']
Christian Heimesd8654cf2007-12-02 15:22:16 +000065
66 Test for the function complete_help():
67 >>> mycmd.complete_help("a")
68 ['add']
69 >>> mycmd.complete_help("he")
Benjamin Petersona28e7022010-01-09 18:53:06 +000070 ['help']
Christian Heimesd8654cf2007-12-02 15:22:16 +000071 >>> mycmd.complete_help("12")
72 []
Benjamin Petersona28e7022010-01-09 18:53:06 +000073 >>> sorted(mycmd.complete_help(""))
74 ['add', 'exit', 'help', 'shell']
Christian Heimesd8654cf2007-12-02 15:22:16 +000075
76 Test for the function do_help():
77 >>> mycmd.do_help("testet")
78 *** No help on testet
79 >>> mycmd.do_help("add")
80 help text for add
81 >>> mycmd.onecmd("help add")
82 help text for add
83 >>> mycmd.do_help("")
84 <BLANKLINE>
85 Documented commands (type help <topic>):
86 ========================================
Raymond Hettinger44d7b6a2010-09-09 03:53:22 +000087 add help
Christian Heimesd8654cf2007-12-02 15:22:16 +000088 <BLANKLINE>
89 Undocumented commands:
90 ======================
Raymond Hettinger44d7b6a2010-09-09 03:53:22 +000091 exit shell
Christian Heimesd8654cf2007-12-02 15:22:16 +000092 <BLANKLINE>
93
94 Test for the function print_topics():
95 >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10)
96 header
97 ======
98 command1
99 command2
100 <BLANKLINE>
101
102 Test for the function columnize():
Christian Heimes2137b6a2007-12-02 16:50:20 +0000103 >>> mycmd.columnize([str(i) for i in range(20)])
Christian Heimesd8654cf2007-12-02 15:22:16 +0000104 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Christian Heimes2137b6a2007-12-02 16:50:20 +0000105 >>> mycmd.columnize([str(i) for i in range(20)], 10)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000106 0 7 14
107 1 8 15
108 2 9 16
109 3 10 17
110 4 11 18
111 5 12 19
112 6 13
113
114 This is a interactive test, put some commands in the cmdqueue attribute
115 and let it execute
116 This test includes the preloop(), postloop(), default(), emptyline(),
117 parseline(), do_help() functions
118 >>> mycmd.use_rawinput=0
119 >>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"]
120 >>> mycmd.cmdloop()
121 Hello from preloop
122 help text for add
123 *** invalid number of arguments
124 9
125 <BLANKLINE>
126 Documented commands (type help <topic>):
127 ========================================
Raymond Hettinger44d7b6a2010-09-09 03:53:22 +0000128 add help
Christian Heimesd8654cf2007-12-02 15:22:16 +0000129 <BLANKLINE>
130 Undocumented commands:
131 ======================
Raymond Hettinger44d7b6a2010-09-09 03:53:22 +0000132 exit shell
Christian Heimesd8654cf2007-12-02 15:22:16 +0000133 <BLANKLINE>
134 help text for add
135 Hello from postloop
136 """
137
138 def preloop(self):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000139 print("Hello from preloop")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000140
141 def postloop(self):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000142 print("Hello from postloop")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000143
144 def completedefault(self, *ignored):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000145 print("This is the completedefault methode")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000146
147 def complete_command(self):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000148 print("complete command")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000149
Benjamin Petersona28e7022010-01-09 18:53:06 +0000150 def do_shell(self, s):
Christian Heimesd8654cf2007-12-02 15:22:16 +0000151 pass
152
153 def do_add(self, s):
154 l = s.split()
155 if len(l) != 2:
Christian Heimes2137b6a2007-12-02 16:50:20 +0000156 print("*** invalid number of arguments")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000157 return
158 try:
159 l = [int(i) for i in l]
160 except ValueError:
Christian Heimes2137b6a2007-12-02 16:50:20 +0000161 print("*** arguments should be numbers")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000162 return
Christian Heimes2137b6a2007-12-02 16:50:20 +0000163 print(l[0]+l[1])
Christian Heimesd8654cf2007-12-02 15:22:16 +0000164
165 def help_add(self):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000166 print("help text for add")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000167 return
168
169 def do_exit(self, arg):
170 return True
171
R. David Murray7905d612010-08-01 03:31:09 +0000172
173class TestAlternateInput(unittest.TestCase):
174
175 class simplecmd(cmd.Cmd):
176
177 def do_print(self, args):
178 print(args, file=self.stdout)
179
180 def do_EOF(self, args):
181 return True
182
Jesus Cea14ed7f22012-02-19 03:52:23 +0100183
184 class simplecmd2(simplecmd):
185
186 def do_EOF(self, args):
187 print('*** Unknown syntax: EOF', file=self.stdout)
188 return True
189
190
R. David Murray7905d612010-08-01 03:31:09 +0000191 def test_file_with_missing_final_nl(self):
192 input = io.StringIO("print test\nprint test2")
193 output = io.StringIO()
194 cmd = self.simplecmd(stdin=input, stdout=output)
195 cmd.use_rawinput = False
196 cmd.cmdloop()
197 self.assertMultiLineEqual(output.getvalue(),
198 ("(Cmd) test\n"
199 "(Cmd) test2\n"
200 "(Cmd) "))
201
202
Jesus Cea14ed7f22012-02-19 03:52:23 +0100203 def test_input_reset_at_EOF(self):
204 input = io.StringIO("print test\nprint test2")
205 output = io.StringIO()
206 cmd = self.simplecmd2(stdin=input, stdout=output)
207 cmd.use_rawinput = False
208 cmd.cmdloop()
209 self.assertMultiLineEqual(output.getvalue(),
210 ("(Cmd) test\n"
211 "(Cmd) test2\n"
212 "(Cmd) *** Unknown syntax: EOF\n"))
213 input = io.StringIO("print \n\n")
214 output = io.StringIO()
215 cmd.stdin = input
216 cmd.stdout = output
217 cmd.cmdloop()
218 self.assertMultiLineEqual(output.getvalue(),
219 ("(Cmd) \n"
220 "(Cmd) \n"
221 "(Cmd) *** Unknown syntax: EOF\n"))
222
223
Christian Heimesd8654cf2007-12-02 15:22:16 +0000224def test_main(verbose=None):
Victor Stinner45df8202010-04-28 22:31:17 +0000225 from test import test_cmd
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000226 support.run_doctest(test_cmd, verbose)
R. David Murray7905d612010-08-01 03:31:09 +0000227 support.run_unittest(TestAlternateInput)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000228
Christian Heimesd8654cf2007-12-02 15:22:16 +0000229def test_coverage(coverdir):
Victor Stinner45df8202010-04-28 22:31:17 +0000230 trace = support.import_module('trace')
Christian Heimesd8654cf2007-12-02 15:22:16 +0000231 tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
232 trace=0, count=1)
233 tracer.run('reload(cmd);test_main()')
234 r=tracer.results()
Christian Heimes2137b6a2007-12-02 16:50:20 +0000235 print("Writing coverage results...")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000236 r.write_results(show_missing=True, summary=True, coverdir=coverdir)
237
238if __name__ == "__main__":
239 if "-c" in sys.argv:
240 test_coverage('/tmp/cmd.cover')
Benjamin Petersona28e7022010-01-09 18:53:06 +0000241 elif "-i" in sys.argv:
242 samplecmdclass().cmdloop()
Christian Heimesd8654cf2007-12-02 15:22:16 +0000243 else:
244 test_main()