Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test script for the 'cmd' module |
| 3 | Original by Michael Schneider |
| 4 | """ |
| 5 | |
| 6 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 7 | import cmd |
| 8 | import sys |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 9 | import re |
R. David Murray | 7905d61 | 2010-08-01 03:31:09 +0000 | [diff] [blame] | 10 | import unittest |
| 11 | import io |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 12 | from test import support |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +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") |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 63 | ['help'] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 64 | |
| 65 | Test for the function complete_help(): |
| 66 | >>> mycmd.complete_help("a") |
| 67 | ['add'] |
| 68 | >>> mycmd.complete_help("he") |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 69 | ['help'] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 70 | >>> mycmd.complete_help("12") |
| 71 | [] |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 72 | >>> sorted(mycmd.complete_help("")) |
| 73 | ['add', 'exit', 'help', 'shell'] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +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 | 44d7b6a | 2010-09-09 03:53:22 +0000 | [diff] [blame] | 86 | add help |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 87 | <BLANKLINE> |
| 88 | Undocumented commands: |
| 89 | ====================== |
Raymond Hettinger | 44d7b6a | 2010-09-09 03:53:22 +0000 | [diff] [blame] | 90 | exit shell |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +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(): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 102 | >>> mycmd.columnize([str(i) for i in range(20)]) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 103 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 104 | >>> mycmd.columnize([str(i) for i in range(20)], 10) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 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 | |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 113 | This is an interactive test, put some commands in the cmdqueue attribute |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 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 | 44d7b6a | 2010-09-09 03:53:22 +0000 | [diff] [blame] | 127 | add help |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 128 | <BLANKLINE> |
| 129 | Undocumented commands: |
| 130 | ====================== |
Raymond Hettinger | 44d7b6a | 2010-09-09 03:53:22 +0000 | [diff] [blame] | 131 | exit shell |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 132 | <BLANKLINE> |
| 133 | help text for add |
| 134 | Hello from postloop |
| 135 | """ |
| 136 | |
| 137 | def preloop(self): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 138 | print("Hello from preloop") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 139 | |
| 140 | def postloop(self): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 141 | print("Hello from postloop") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 142 | |
| 143 | def completedefault(self, *ignored): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 144 | print("This is the completedefault methode") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 145 | |
| 146 | def complete_command(self): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 147 | print("complete command") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 148 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 149 | def do_shell(self, s): |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 150 | pass |
| 151 | |
| 152 | def do_add(self, s): |
| 153 | l = s.split() |
| 154 | if len(l) != 2: |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 155 | print("*** invalid number of arguments") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 156 | return |
| 157 | try: |
| 158 | l = [int(i) for i in l] |
| 159 | except ValueError: |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 160 | print("*** arguments should be numbers") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 161 | return |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 162 | print(l[0]+l[1]) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 163 | |
| 164 | def help_add(self): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 165 | print("help text for add") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 166 | return |
| 167 | |
| 168 | def do_exit(self, arg): |
| 169 | return True |
| 170 | |
R. David Murray | 7905d61 | 2010-08-01 03:31:09 +0000 | [diff] [blame] | 171 | |
| 172 | class TestAlternateInput(unittest.TestCase): |
| 173 | |
| 174 | class simplecmd(cmd.Cmd): |
| 175 | |
| 176 | def do_print(self, args): |
| 177 | print(args, file=self.stdout) |
| 178 | |
| 179 | def do_EOF(self, args): |
| 180 | return True |
| 181 | |
Jesus Cea | 14ed7f2 | 2012-02-19 03:52:23 +0100 | [diff] [blame] | 182 | |
| 183 | class simplecmd2(simplecmd): |
| 184 | |
| 185 | def do_EOF(self, args): |
| 186 | print('*** Unknown syntax: EOF', file=self.stdout) |
| 187 | return True |
| 188 | |
| 189 | |
R. David Murray | 7905d61 | 2010-08-01 03:31:09 +0000 | [diff] [blame] | 190 | def test_file_with_missing_final_nl(self): |
| 191 | input = io.StringIO("print test\nprint test2") |
| 192 | output = io.StringIO() |
| 193 | cmd = self.simplecmd(stdin=input, stdout=output) |
| 194 | cmd.use_rawinput = False |
| 195 | cmd.cmdloop() |
| 196 | self.assertMultiLineEqual(output.getvalue(), |
| 197 | ("(Cmd) test\n" |
| 198 | "(Cmd) test2\n" |
| 199 | "(Cmd) ")) |
| 200 | |
| 201 | |
Jesus Cea | 14ed7f2 | 2012-02-19 03:52:23 +0100 | [diff] [blame] | 202 | def test_input_reset_at_EOF(self): |
| 203 | input = io.StringIO("print test\nprint test2") |
| 204 | output = io.StringIO() |
| 205 | cmd = self.simplecmd2(stdin=input, stdout=output) |
| 206 | cmd.use_rawinput = False |
| 207 | cmd.cmdloop() |
| 208 | self.assertMultiLineEqual(output.getvalue(), |
| 209 | ("(Cmd) test\n" |
| 210 | "(Cmd) test2\n" |
| 211 | "(Cmd) *** Unknown syntax: EOF\n")) |
| 212 | input = io.StringIO("print \n\n") |
| 213 | output = io.StringIO() |
| 214 | cmd.stdin = input |
| 215 | cmd.stdout = output |
| 216 | cmd.cmdloop() |
| 217 | self.assertMultiLineEqual(output.getvalue(), |
| 218 | ("(Cmd) \n" |
| 219 | "(Cmd) \n" |
| 220 | "(Cmd) *** Unknown syntax: EOF\n")) |
| 221 | |
| 222 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 223 | def test_main(verbose=None): |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 224 | from test import test_cmd |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 225 | support.run_doctest(test_cmd, verbose) |
R. David Murray | 7905d61 | 2010-08-01 03:31:09 +0000 | [diff] [blame] | 226 | support.run_unittest(TestAlternateInput) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 227 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 228 | def test_coverage(coverdir): |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 229 | trace = support.import_module('trace') |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 230 | tracer=trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 231 | trace=0, count=1) |
Berker Peksag | 67ef591 | 2014-06-30 04:04:52 +0300 | [diff] [blame] | 232 | tracer.run('import importlib; importlib.reload(cmd); test_main()') |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 233 | r=tracer.results() |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 234 | print("Writing coverage results...") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 235 | r.write_results(show_missing=True, summary=True, coverdir=coverdir) |
| 236 | |
| 237 | if __name__ == "__main__": |
| 238 | if "-c" in sys.argv: |
| 239 | test_coverage('/tmp/cmd.cover') |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 240 | elif "-i" in sys.argv: |
| 241 | samplecmdclass().cmdloop() |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 242 | else: |
| 243 | test_main() |