blob: e2b3022a2a5884ed5bf8478f65ed809c1daa35fb [file] [log] [blame]
Christian Heimesd8654cf2007-12-02 15:22:16 +00001#!/usr/bin/env python
2"""
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 trace
11import re
12from io import StringIO
Christian Heimesd8654cf2007-12-02 15:22:16 +000013
14class samplecmdclass(cmd.Cmd):
15 """
16 Instance the sampleclass:
17 >>> mycmd = samplecmdclass()
18
19 Test for the function parseline():
20 >>> mycmd.parseline("")
21 (None, None, '')
22 >>> mycmd.parseline("?")
23 ('help', '', 'help ')
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")
31 ('func', '', 'func')
32 >>> mycmd.parseline("func arg1")
33 ('func', 'arg1', 'func arg1')
34
35
36 Test for the function onecmd():
37 >>> mycmd.onecmd("")
38 >>> mycmd.onecmd("add 4 5")
39 9
40 >>> mycmd.onecmd("")
41 9
42 >>> mycmd.onecmd("test")
43 *** Unknown syntax: test
44
45 Test for the function emptyline():
46 >>> mycmd.emptyline()
47 *** Unknown syntax: test
48
49 Test for the function default():
50 >>> mycmd.default("default")
51 *** Unknown syntax: default
52
53 Test for the function completedefault():
54 >>> mycmd.completedefault()
55 This is the completedefault methode
56 >>> mycmd.completenames("a")
57 ['add']
58
59 Test for the function completenames():
60 >>> mycmd.completenames("12")
61 []
62 >>> mycmd.completenames("help")
63 ['help', 'help']
64
65 Test for the function complete_help():
66 >>> mycmd.complete_help("a")
67 ['add']
68 >>> mycmd.complete_help("he")
69 ['help', 'help']
70 >>> mycmd.complete_help("12")
71 []
72
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>
90
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>
98
99 Test for the function columnize():
Christian Heimes2137b6a2007-12-02 16:50:20 +0000100 >>> mycmd.columnize([str(i) for i in range(20)])
Christian Heimesd8654cf2007-12-02 15:22:16 +0000101 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 +0000102 >>> mycmd.columnize([str(i) for i in range(20)], 10)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000103 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
110
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
121 9
122 <BLANKLINE>
123 Documented commands (type help <topic>):
124 ========================================
125 add
126 <BLANKLINE>
127 Undocumented commands:
128 ======================
129 exit help shell
130 <BLANKLINE>
131 help text for add
132 Hello from postloop
133 """
134
135 def preloop(self):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000136 print("Hello from preloop")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000137
138 def postloop(self):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000139 print("Hello from postloop")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000140
141 def completedefault(self, *ignored):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000142 print("This is the completedefault methode")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000143
144 def complete_command(self):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000145 print("complete command")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000146
147 def do_shell(self):
148 pass
149
150 def do_add(self, s):
151 l = s.split()
152 if len(l) != 2:
Christian Heimes2137b6a2007-12-02 16:50:20 +0000153 print("*** invalid number of arguments")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000154 return
155 try:
156 l = [int(i) for i in l]
157 except ValueError:
Christian Heimes2137b6a2007-12-02 16:50:20 +0000158 print("*** arguments should be numbers")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000159 return
Christian Heimes2137b6a2007-12-02 16:50:20 +0000160 print(l[0]+l[1])
Christian Heimesd8654cf2007-12-02 15:22:16 +0000161
162 def help_add(self):
Christian Heimes2137b6a2007-12-02 16:50:20 +0000163 print("help text for add")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000164 return
165
166 def do_exit(self, arg):
167 return True
168
169def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000170 from test import support, test_cmd
171 support.run_doctest(test_cmd, verbose)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000172
Christian Heimesd8654cf2007-12-02 15:22:16 +0000173def test_coverage(coverdir):
174 tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
175 trace=0, count=1)
176 tracer.run('reload(cmd);test_main()')
177 r=tracer.results()
Christian Heimes2137b6a2007-12-02 16:50:20 +0000178 print("Writing coverage results...")
Christian Heimesd8654cf2007-12-02 15:22:16 +0000179 r.write_results(show_missing=True, summary=True, coverdir=coverdir)
180
181if __name__ == "__main__":
182 if "-c" in sys.argv:
183 test_coverage('/tmp/cmd.cover')
184 else:
185 test_main()