3
Test script for the 'cmd' module
4
Original by Michael Schneider
12
from io import StringIO
14
class samplecmdclass(cmd.Cmd):
16
Instance the sampleclass:
17
>>> mycmd = samplecmdclass()
19
Test for the function parseline():
20
>>> mycmd.parseline("")
22
>>> mycmd.parseline("?")
24
>>> mycmd.parseline("?help")
25
('help', 'help', 'help help')
26
>>> mycmd.parseline("!")
27
('shell', '', 'shell ')
28
>>> mycmd.parseline("!command")
29
('shell', 'command', 'shell command')
30
>>> mycmd.parseline("func")
32
>>> mycmd.parseline("func arg1")
33
('func', 'arg1', 'func arg1')
36
Test for the function onecmd():
38
>>> mycmd.onecmd("add 4 5")
42
>>> mycmd.onecmd("test")
43
*** Unknown syntax: test
45
Test for the function emptyline():
47
*** Unknown syntax: test
49
Test for the function default():
50
>>> mycmd.default("default")
51
*** Unknown syntax: default
53
Test for the function completedefault():
54
>>> mycmd.completedefault()
55
This is the completedefault methode
56
>>> mycmd.completenames("a")
59
Test for the function completenames():
60
>>> mycmd.completenames("12")
62
>>> mycmd.completenames("help")
65
Test for the function complete_help():
66
>>> mycmd.complete_help("a")
68
>>> mycmd.complete_help("he")
70
>>> mycmd.complete_help("12")
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 range(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 range(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")
144
def complete_command(self):
145
print("complete command")
153
print("*** invalid number of arguments")
156
l = [int(i) for i in l]
158
print("*** arguments should be numbers")
163
print("help text for add")
166
def do_exit(self, arg):
169
def test_main(verbose=None):
170
from test import support, test_cmd
171
support.run_doctest(test_cmd, verbose)
173
def test_coverage(coverdir):
174
tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
176
tracer.run('reload(cmd);test_main()')
178
print("Writing coverage results...")
179
r.write_results(show_missing=True, summary=True, coverdir=coverdir)
181
if __name__ == "__main__":
183
test_coverage('/tmp/cmd.cover')