1 | #!/usr/bin/env python
|
---|
2 | """
|
---|
3 | Test script for the 'cmd' module
|
---|
4 | Original by Michael Schneider
|
---|
5 | """
|
---|
6 |
|
---|
7 |
|
---|
8 | import cmd
|
---|
9 | import sys
|
---|
10 | from test import test_support
|
---|
11 | import re
|
---|
12 | import unittest
|
---|
13 | import StringIO
|
---|
14 |
|
---|
15 | class 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")
|
---|
64 | ['help']
|
---|
65 |
|
---|
66 | Test for the function complete_help():
|
---|
67 | >>> mycmd.complete_help("a")
|
---|
68 | ['add']
|
---|
69 | >>> mycmd.complete_help("he")
|
---|
70 | ['help']
|
---|
71 | >>> mycmd.complete_help("12")
|
---|
72 | []
|
---|
73 | >>> sorted(mycmd.complete_help(""))
|
---|
74 | ['add', 'exit', 'help', 'shell']
|
---|
75 |
|
---|
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 | ========================================
|
---|
87 | add help
|
---|
88 | <BLANKLINE>
|
---|
89 | Undocumented commands:
|
---|
90 | ======================
|
---|
91 | exit shell
|
---|
92 | <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():
|
---|
103 | >>> mycmd.columnize([str(i) for i in xrange(20)])
|
---|
104 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
---|
105 | >>> mycmd.columnize([str(i) for i in xrange(20)], 10)
|
---|
106 | 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 | ========================================
|
---|
128 | add help
|
---|
129 | <BLANKLINE>
|
---|
130 | Undocumented commands:
|
---|
131 | ======================
|
---|
132 | exit shell
|
---|
133 | <BLANKLINE>
|
---|
134 | help text for add
|
---|
135 | Hello from postloop
|
---|
136 | """
|
---|
137 |
|
---|
138 | def preloop(self):
|
---|
139 | print "Hello from preloop"
|
---|
140 |
|
---|
141 | def postloop(self):
|
---|
142 | print "Hello from postloop"
|
---|
143 |
|
---|
144 | def completedefault(self, *ignored):
|
---|
145 | print "This is the completedefault methode"
|
---|
146 | return
|
---|
147 |
|
---|
148 | def complete_command(self):
|
---|
149 | print "complete command"
|
---|
150 | return
|
---|
151 |
|
---|
152 | def do_shell(self, s):
|
---|
153 | pass
|
---|
154 |
|
---|
155 | def do_add(self, s):
|
---|
156 | l = s.split()
|
---|
157 | if len(l) != 2:
|
---|
158 | print "*** invalid number of arguments"
|
---|
159 | return
|
---|
160 | try:
|
---|
161 | l = [int(i) for i in l]
|
---|
162 | except ValueError:
|
---|
163 | print "*** arguments should be numbers"
|
---|
164 | return
|
---|
165 | print l[0]+l[1]
|
---|
166 |
|
---|
167 | def help_add(self):
|
---|
168 | print "help text for add"
|
---|
169 | return
|
---|
170 |
|
---|
171 | def do_exit(self, arg):
|
---|
172 | return True
|
---|
173 |
|
---|
174 |
|
---|
175 | class TestAlternateInput(unittest.TestCase):
|
---|
176 |
|
---|
177 | class simplecmd(cmd.Cmd):
|
---|
178 |
|
---|
179 | def do_print(self, args):
|
---|
180 | print >>self.stdout, args
|
---|
181 |
|
---|
182 | def do_EOF(self, args):
|
---|
183 | return True
|
---|
184 |
|
---|
185 |
|
---|
186 | class simplecmd2(simplecmd):
|
---|
187 |
|
---|
188 | def do_EOF(self, args):
|
---|
189 | print >>self.stdout, '*** Unknown syntax: EOF'
|
---|
190 | return True
|
---|
191 |
|
---|
192 |
|
---|
193 | def test_file_with_missing_final_nl(self):
|
---|
194 | input = StringIO.StringIO("print test\nprint test2")
|
---|
195 | output = StringIO.StringIO()
|
---|
196 | cmd = self.simplecmd(stdin=input, stdout=output)
|
---|
197 | cmd.use_rawinput = False
|
---|
198 | cmd.cmdloop()
|
---|
199 | self.assertMultiLineEqual(output.getvalue(),
|
---|
200 | ("(Cmd) test\n"
|
---|
201 | "(Cmd) test2\n"
|
---|
202 | "(Cmd) "))
|
---|
203 |
|
---|
204 |
|
---|
205 | def test_input_reset_at_EOF(self):
|
---|
206 | input = StringIO.StringIO("print test\nprint test2")
|
---|
207 | output = StringIO.StringIO()
|
---|
208 | cmd = self.simplecmd2(stdin=input, stdout=output)
|
---|
209 | cmd.use_rawinput = False
|
---|
210 | cmd.cmdloop()
|
---|
211 | self.assertMultiLineEqual(output.getvalue(),
|
---|
212 | ("(Cmd) test\n"
|
---|
213 | "(Cmd) test2\n"
|
---|
214 | "(Cmd) *** Unknown syntax: EOF\n"))
|
---|
215 | input = StringIO.StringIO("print \n\n")
|
---|
216 | output = StringIO.StringIO()
|
---|
217 | cmd.stdin = input
|
---|
218 | cmd.stdout = output
|
---|
219 | cmd.cmdloop()
|
---|
220 | self.assertMultiLineEqual(output.getvalue(),
|
---|
221 | ("(Cmd) \n"
|
---|
222 | "(Cmd) \n"
|
---|
223 | "(Cmd) *** Unknown syntax: EOF\n"))
|
---|
224 |
|
---|
225 |
|
---|
226 | def test_main(verbose=None):
|
---|
227 | from test import test_cmd
|
---|
228 | test_support.run_doctest(test_cmd, verbose)
|
---|
229 | test_support.run_unittest(TestAlternateInput)
|
---|
230 |
|
---|
231 | def test_coverage(coverdir):
|
---|
232 | trace = test_support.import_module('trace')
|
---|
233 | tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
|
---|
234 | trace=0, count=1)
|
---|
235 | tracer.run('reload(cmd);test_main()')
|
---|
236 | r=tracer.results()
|
---|
237 | print "Writing coverage results..."
|
---|
238 | r.write_results(show_missing=True, summary=True, coverdir=coverdir)
|
---|
239 |
|
---|
240 | if __name__ == "__main__":
|
---|
241 | if "-c" in sys.argv:
|
---|
242 | test_coverage('/tmp/cmd.cover')
|
---|
243 | elif "-i" in sys.argv:
|
---|
244 | samplecmdclass().cmdloop()
|
---|
245 | else:
|
---|
246 | test_main()
|
---|