move sections
[python/dscho.git] / Lib / test / test_cmd.py
blob19ee52e0901bacc482c4bae741e3625dfe39ee0b
1 #!/usr/bin/env python
2 """
3 Test script for the 'cmd' module
4 Original by Michael Schneider
5 """
8 import cmd
9 import sys
10 from test import test_support
12 class samplecmdclass(cmd.Cmd):
13 """
14 Instance the sampleclass:
15 >>> mycmd = samplecmdclass()
17 Test for the function parseline():
18 >>> mycmd.parseline("")
19 (None, None, '')
20 >>> mycmd.parseline("?")
21 ('help', '', 'help ')
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")
29 ('func', '', 'func')
30 >>> mycmd.parseline("func arg1")
31 ('func', 'arg1', 'func arg1')
34 Test for the function onecmd():
35 >>> mycmd.onecmd("")
36 >>> mycmd.onecmd("add 4 5")
38 >>> mycmd.onecmd("")
40 >>> mycmd.onecmd("test")
41 *** Unknown syntax: test
43 Test for the function emptyline():
44 >>> mycmd.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")
55 ['add']
57 Test for the function completenames():
58 >>> mycmd.completenames("12")
60 >>> mycmd.completenames("help")
61 ['help']
63 Test for the function complete_help():
64 >>> mycmd.complete_help("a")
65 ['add']
66 >>> mycmd.complete_help("he")
67 ['help']
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")
75 *** No help on testet
76 >>> mycmd.do_help("add")
77 help text for add
78 >>> mycmd.onecmd("help add")
79 help text for add
80 >>> mycmd.do_help("")
81 <BLANKLINE>
82 Documented commands (type help <topic>):
83 ========================================
84 add
85 <BLANKLINE>
86 Undocumented commands:
87 ======================
88 exit help shell
89 <BLANKLINE>
91 Test for the function print_topics():
92 >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10)
93 header
94 ======
95 command1
96 command2
97 <BLANKLINE>
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)
103 0 7 14
104 1 8 15
105 2 9 16
106 3 10 17
107 4 11 18
108 5 12 19
109 6 13
111 This is a interactive test, put some commands in the cmdqueue attribute
112 and let it execute
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"]
117 >>> mycmd.cmdloop()
118 Hello from preloop
119 help text for add
120 *** invalid number of arguments
122 <BLANKLINE>
123 Documented commands (type help <topic>):
124 ========================================
126 <BLANKLINE>
127 Undocumented commands:
128 ======================
129 exit help shell
130 <BLANKLINE>
131 help text for add
132 Hello from postloop
135 def preloop(self):
136 print "Hello from preloop"
138 def postloop(self):
139 print "Hello from postloop"
141 def completedefault(self, *ignored):
142 print "This is the completedefault methode"
143 return
145 def complete_command(self):
146 print "complete command"
147 return
149 def do_shell(self, s):
150 pass
152 def do_add(self, s):
153 l = s.split()
154 if len(l) != 2:
155 print "*** invalid number of arguments"
156 return
157 try:
158 l = [int(i) for i in l]
159 except ValueError:
160 print "*** arguments should be numbers"
161 return
162 print l[0]+l[1]
164 def help_add(self):
165 print "help text for add"
166 return
168 def do_exit(self, arg):
169 return True
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,],
178 trace=0, count=1)
179 tracer.run('reload(cmd);test_main()')
180 r=tracer.results()
181 print "Writing coverage results..."
182 r.write_results(show_missing=True, summary=True, coverdir=coverdir)
184 if __name__ == "__main__":
185 if "-c" in sys.argv:
186 test_coverage('/tmp/cmd.cover')
187 elif "-i" in sys.argv:
188 samplecmdclass().cmdloop()
189 else:
190 test_main()