3 Test script for the 'cmd' module
4 Original by Michael Schneider
10 from test
import test_support
12 class samplecmdclass(cmd
.Cmd
):
14 Instance the sampleclass:
15 >>> mycmd = samplecmdclass()
17 Test for the function parseline():
18 >>> mycmd.parseline("")
20 >>> mycmd.parseline("?")
22 >>> mycmd.parseline("?help")
23 ('help', 'help', 'help help')
24 >>> mycmd.parseline("!")
25 ('shell', '', 'shell ')
26 >>> mycmd.parseline("!command")
27 ('shell', 'command', 'shell command')
28 >>> mycmd.parseline("func")
30 >>> mycmd.parseline("func arg1")
31 ('func', 'arg1', 'func arg1')
34 Test for the function onecmd():
36 >>> mycmd.onecmd("add 4 5")
40 >>> mycmd.onecmd("test")
41 *** Unknown syntax: test
43 Test for the function emptyline():
45 *** Unknown syntax: test
47 Test for the function default():
48 >>> mycmd.default("default")
49 *** Unknown syntax: default
51 Test for the function completedefault():
52 >>> mycmd.completedefault()
53 This is the completedefault methode
54 >>> mycmd.completenames("a")
57 Test for the function completenames():
58 >>> mycmd.completenames("12")
60 >>> mycmd.completenames("help")
63 Test for the function complete_help():
64 >>> mycmd.complete_help("a")
66 >>> mycmd.complete_help("he")
68 >>> mycmd.complete_help("12")
70 >>> sorted(mycmd.complete_help(""))
71 ['add', 'exit', 'help', 'shell']
73 Test for the function do_help():
74 >>> mycmd.do_help("testet")
76 >>> mycmd.do_help("add")
78 >>> mycmd.onecmd("help add")
82 Documented commands (type help <topic>):
83 ========================================
86 Undocumented commands:
87 ======================
91 Test for the function print_topics():
92 >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10)
99 Test for the function columnize():
100 >>> mycmd.columnize([str(i) for i in xrange(20)])
101 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
102 >>> mycmd.columnize([str(i) for i in xrange(20)], 10)
111 This is a interactive test, put some commands in the cmdqueue attribute
113 This test includes the preloop(), postloop(), default(), emptyline(),
114 parseline(), do_help() functions
115 >>> mycmd.use_rawinput=0
116 >>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"]
120 *** invalid number of arguments
123 Documented commands (type help <topic>):
124 ========================================
127 Undocumented commands:
128 ======================
136 print "Hello from preloop"
139 print "Hello from postloop"
141 def completedefault(self
, *ignored
):
142 print "This is the completedefault methode"
145 def complete_command(self
):
146 print "complete command"
149 def do_shell(self
, s
):
155 print "*** invalid number of arguments"
158 l
= [int(i
) for i
in l
]
160 print "*** arguments should be numbers"
165 print "help text for add"
168 def do_exit(self
, arg
):
171 def test_main(verbose
=None):
172 from test
import test_cmd
173 test_support
.run_doctest(test_cmd
, verbose
)
175 def test_coverage(coverdir
):
176 trace
= test_support
.import_module('trace')
177 tracer
=trace
.Trace(ignoredirs
=[sys
.prefix
, sys
.exec_prefix
,],
179 tracer
.run('reload(cmd);test_main()')
181 print "Writing coverage results..."
182 r
.write_results(show_missing
=True, summary
=True, coverdir
=coverdir
)
184 if __name__
== "__main__":
186 test_coverage('/tmp/cmd.cover')
187 elif "-i" in sys
.argv
:
188 samplecmdclass().cmdloop()