Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test script for the 'cmd' module |
| 3 | Original by Michael Schneider |
| 4 | """ |
| 5 | |
| 6 | |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 7 | import cmd |
| 8 | import sys |
Victor Stinner | c73a05f | 2010-04-27 23:51:16 +0000 | [diff] [blame] | 9 | from test import test_support |
R. David Murray | 58f15b6 | 2010-08-01 04:04:03 +0000 | [diff] [blame] | 10 | import re |
| 11 | import unittest |
| 12 | import StringIO |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 13 | |
| 14 | class 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") |
Georg Brandl | 8904053 | 2010-01-06 18:02:16 +0000 | [diff] [blame] | 63 | ['help'] |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 64 | |
| 65 | Test for the function complete_help(): |
| 66 | >>> mycmd.complete_help("a") |
| 67 | ['add'] |
| 68 | >>> mycmd.complete_help("he") |
Georg Brandl | 8904053 | 2010-01-06 18:02:16 +0000 | [diff] [blame] | 69 | ['help'] |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 70 | >>> mycmd.complete_help("12") |
| 71 | [] |
Georg Brandl | 8904053 | 2010-01-06 18:02:16 +0000 | [diff] [blame] | 72 | >>> sorted(mycmd.complete_help("")) |
| 73 | ['add', 'exit', 'help', 'shell'] |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 74 | |
| 75 | Test for the function do_help(): |
| 76 | >>> mycmd.do_help("testet") |
| 77 | *** No help on testet |
| 78 | >>> mycmd.do_help("add") |
| 79 | help text for add |
| 80 | >>> mycmd.onecmd("help add") |
| 81 | help text for add |
| 82 | >>> mycmd.do_help("") |
| 83 | <BLANKLINE> |
| 84 | Documented commands (type help <topic>): |
| 85 | ======================================== |
Raymond Hettinger | 66dd941 | 2012-07-16 00:11:05 -0700 | [diff] [blame] | 86 | add help |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 87 | <BLANKLINE> |
| 88 | Undocumented commands: |
| 89 | ====================== |
Raymond Hettinger | 66dd941 | 2012-07-16 00:11:05 -0700 | [diff] [blame] | 90 | exit shell |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 91 | <BLANKLINE> |
| 92 | |
| 93 | Test for the function print_topics(): |
| 94 | >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10) |
| 95 | header |
| 96 | ====== |
| 97 | command1 |
| 98 | command2 |
| 99 | <BLANKLINE> |
| 100 | |
| 101 | Test for the function columnize(): |
| 102 | >>> mycmd.columnize([str(i) for i in xrange(20)]) |
| 103 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
| 104 | >>> mycmd.columnize([str(i) for i in xrange(20)], 10) |
| 105 | 0 7 14 |
| 106 | 1 8 15 |
| 107 | 2 9 16 |
| 108 | 3 10 17 |
| 109 | 4 11 18 |
| 110 | 5 12 19 |
| 111 | 6 13 |
| 112 | |
| 113 | This is a interactive test, put some commands in the cmdqueue attribute |
| 114 | and let it execute |
| 115 | This test includes the preloop(), postloop(), default(), emptyline(), |
| 116 | parseline(), do_help() functions |
| 117 | >>> mycmd.use_rawinput=0 |
| 118 | >>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"] |
| 119 | >>> mycmd.cmdloop() |
| 120 | Hello from preloop |
| 121 | help text for add |
| 122 | *** invalid number of arguments |
| 123 | 9 |
| 124 | <BLANKLINE> |
| 125 | Documented commands (type help <topic>): |
| 126 | ======================================== |
Raymond Hettinger | 66dd941 | 2012-07-16 00:11:05 -0700 | [diff] [blame] | 127 | add help |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 128 | <BLANKLINE> |
| 129 | Undocumented commands: |
| 130 | ====================== |
Raymond Hettinger | 66dd941 | 2012-07-16 00:11:05 -0700 | [diff] [blame] | 131 | exit shell |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 132 | <BLANKLINE> |
| 133 | help text for add |
| 134 | Hello from postloop |
| 135 | """ |
| 136 | |
| 137 | def preloop(self): |
| 138 | print "Hello from preloop" |
| 139 | |
| 140 | def postloop(self): |
| 141 | print "Hello from postloop" |
| 142 | |
| 143 | def completedefault(self, *ignored): |
| 144 | print "This is the completedefault methode" |
| 145 | return |
| 146 | |
| 147 | def complete_command(self): |
| 148 | print "complete command" |
| 149 | return |
| 150 | |
Georg Brandl | 5089a38 | 2010-01-06 17:43:06 +0000 | [diff] [blame] | 151 | def do_shell(self, s): |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 152 | pass |
| 153 | |
| 154 | def do_add(self, s): |
| 155 | l = s.split() |
| 156 | if len(l) != 2: |
| 157 | print "*** invalid number of arguments" |
| 158 | return |
| 159 | try: |
| 160 | l = [int(i) for i in l] |
| 161 | except ValueError: |
| 162 | print "*** arguments should be numbers" |
| 163 | return |
| 164 | print l[0]+l[1] |
| 165 | |
| 166 | def help_add(self): |
| 167 | print "help text for add" |
| 168 | return |
| 169 | |
| 170 | def do_exit(self, arg): |
| 171 | return True |
| 172 | |
R. David Murray | 58f15b6 | 2010-08-01 04:04:03 +0000 | [diff] [blame] | 173 | |
| 174 | class TestAlternateInput(unittest.TestCase): |
| 175 | |
| 176 | class simplecmd(cmd.Cmd): |
| 177 | |
| 178 | def do_print(self, args): |
| 179 | print >>self.stdout, args |
| 180 | |
| 181 | def do_EOF(self, args): |
| 182 | return True |
| 183 | |
Jesus Cea | 6e25099 | 2012-02-19 03:54:08 +0100 | [diff] [blame] | 184 | |
| 185 | class simplecmd2(simplecmd): |
| 186 | |
| 187 | def do_EOF(self, args): |
Jesus Cea | f5f6af8 | 2012-02-19 04:20:45 +0100 | [diff] [blame] | 188 | print >>self.stdout, '*** Unknown syntax: EOF' |
Jesus Cea | 6e25099 | 2012-02-19 03:54:08 +0100 | [diff] [blame] | 189 | return True |
| 190 | |
| 191 | |
R. David Murray | 58f15b6 | 2010-08-01 04:04:03 +0000 | [diff] [blame] | 192 | def test_file_with_missing_final_nl(self): |
| 193 | input = StringIO.StringIO("print test\nprint test2") |
| 194 | output = StringIO.StringIO() |
| 195 | cmd = self.simplecmd(stdin=input, stdout=output) |
| 196 | cmd.use_rawinput = False |
| 197 | cmd.cmdloop() |
| 198 | self.assertMultiLineEqual(output.getvalue(), |
| 199 | ("(Cmd) test\n" |
| 200 | "(Cmd) test2\n" |
| 201 | "(Cmd) ")) |
| 202 | |
| 203 | |
Jesus Cea | 6e25099 | 2012-02-19 03:54:08 +0100 | [diff] [blame] | 204 | def test_input_reset_at_EOF(self): |
Jesus Cea | f5f6af8 | 2012-02-19 04:20:45 +0100 | [diff] [blame] | 205 | input = StringIO.StringIO("print test\nprint test2") |
| 206 | output = StringIO.StringIO() |
Jesus Cea | 6e25099 | 2012-02-19 03:54:08 +0100 | [diff] [blame] | 207 | cmd = self.simplecmd2(stdin=input, stdout=output) |
| 208 | cmd.use_rawinput = False |
| 209 | cmd.cmdloop() |
| 210 | self.assertMultiLineEqual(output.getvalue(), |
| 211 | ("(Cmd) test\n" |
| 212 | "(Cmd) test2\n" |
| 213 | "(Cmd) *** Unknown syntax: EOF\n")) |
Jesus Cea | f5f6af8 | 2012-02-19 04:20:45 +0100 | [diff] [blame] | 214 | input = StringIO.StringIO("print \n\n") |
| 215 | output = StringIO.StringIO() |
Jesus Cea | 6e25099 | 2012-02-19 03:54:08 +0100 | [diff] [blame] | 216 | cmd.stdin = input |
| 217 | cmd.stdout = output |
| 218 | cmd.cmdloop() |
| 219 | self.assertMultiLineEqual(output.getvalue(), |
| 220 | ("(Cmd) \n" |
| 221 | "(Cmd) \n" |
| 222 | "(Cmd) *** Unknown syntax: EOF\n")) |
| 223 | |
| 224 | |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 225 | def test_main(verbose=None): |
Victor Stinner | c73a05f | 2010-04-27 23:51:16 +0000 | [diff] [blame] | 226 | from test import test_cmd |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 227 | test_support.run_doctest(test_cmd, verbose) |
R. David Murray | 58f15b6 | 2010-08-01 04:04:03 +0000 | [diff] [blame] | 228 | test_support.run_unittest(TestAlternateInput) |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 229 | |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 230 | def test_coverage(coverdir): |
Victor Stinner | c73a05f | 2010-04-27 23:51:16 +0000 | [diff] [blame] | 231 | trace = test_support.import_module('trace') |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 232 | tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], |
| 233 | trace=0, count=1) |
| 234 | tracer.run('reload(cmd);test_main()') |
| 235 | r=tracer.results() |
| 236 | print "Writing coverage results..." |
| 237 | r.write_results(show_missing=True, summary=True, coverdir=coverdir) |
| 238 | |
| 239 | if __name__ == "__main__": |
| 240 | if "-c" in sys.argv: |
| 241 | test_coverage('/tmp/cmd.cover') |
Georg Brandl | 5089a38 | 2010-01-06 17:43:06 +0000 | [diff] [blame] | 242 | elif "-i" in sys.argv: |
| 243 | samplecmdclass().cmdloop() |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 244 | else: |
| 245 | test_main() |