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 |
R. David Murray | 7905d61 | 2010-08-01 03:31:09 +0000 | [diff] [blame] | 9 | import unittest |
| 10 | import io |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 11 | from test import support |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 12 | |
| 13 | class samplecmdclass(cmd.Cmd): |
| 14 | """ |
| 15 | Instance the sampleclass: |
| 16 | >>> mycmd = samplecmdclass() |
| 17 | |
| 18 | Test for the function parseline(): |
| 19 | >>> mycmd.parseline("") |
| 20 | (None, None, '') |
| 21 | >>> mycmd.parseline("?") |
| 22 | ('help', '', 'help ') |
| 23 | >>> mycmd.parseline("?help") |
| 24 | ('help', 'help', 'help help') |
| 25 | >>> mycmd.parseline("!") |
| 26 | ('shell', '', 'shell ') |
| 27 | >>> mycmd.parseline("!command") |
| 28 | ('shell', 'command', 'shell command') |
| 29 | >>> mycmd.parseline("func") |
| 30 | ('func', '', 'func') |
| 31 | >>> mycmd.parseline("func arg1") |
| 32 | ('func', 'arg1', 'func arg1') |
| 33 | |
| 34 | |
| 35 | Test for the function onecmd(): |
| 36 | >>> mycmd.onecmd("") |
| 37 | >>> mycmd.onecmd("add 4 5") |
| 38 | 9 |
| 39 | >>> mycmd.onecmd("") |
| 40 | 9 |
| 41 | >>> mycmd.onecmd("test") |
| 42 | *** Unknown syntax: test |
| 43 | |
| 44 | Test for the function emptyline(): |
| 45 | >>> mycmd.emptyline() |
| 46 | *** Unknown syntax: test |
| 47 | |
| 48 | Test for the function default(): |
| 49 | >>> mycmd.default("default") |
| 50 | *** Unknown syntax: default |
| 51 | |
| 52 | Test for the function completedefault(): |
| 53 | >>> mycmd.completedefault() |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 54 | This is the completedefault method |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 55 | >>> mycmd.completenames("a") |
| 56 | ['add'] |
| 57 | |
| 58 | Test for the function completenames(): |
| 59 | >>> mycmd.completenames("12") |
| 60 | [] |
| 61 | >>> mycmd.completenames("help") |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 62 | ['help'] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 63 | |
| 64 | Test for the function complete_help(): |
| 65 | >>> mycmd.complete_help("a") |
| 66 | ['add'] |
| 67 | >>> mycmd.complete_help("he") |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 68 | ['help'] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 69 | >>> mycmd.complete_help("12") |
| 70 | [] |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 71 | >>> sorted(mycmd.complete_help("")) |
| 72 | ['add', 'exit', 'help', 'shell'] |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 73 | |
| 74 | Test for the function do_help(): |
| 75 | >>> mycmd.do_help("testet") |
| 76 | *** No help on testet |
| 77 | >>> mycmd.do_help("add") |
| 78 | help text for add |
| 79 | >>> mycmd.onecmd("help add") |
| 80 | help text for add |
| 81 | >>> mycmd.do_help("") |
| 82 | <BLANKLINE> |
| 83 | Documented commands (type help <topic>): |
| 84 | ======================================== |
Raymond Hettinger | 44d7b6a | 2010-09-09 03:53:22 +0000 | [diff] [blame] | 85 | add help |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 86 | <BLANKLINE> |
| 87 | Undocumented commands: |
| 88 | ====================== |
Raymond Hettinger | 44d7b6a | 2010-09-09 03:53:22 +0000 | [diff] [blame] | 89 | exit shell |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 90 | <BLANKLINE> |
| 91 | |
| 92 | Test for the function print_topics(): |
| 93 | >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10) |
| 94 | header |
| 95 | ====== |
| 96 | command1 |
| 97 | command2 |
| 98 | <BLANKLINE> |
| 99 | |
| 100 | Test for the function columnize(): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 101 | >>> mycmd.columnize([str(i) for i in range(20)]) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 102 | 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] | 103 | >>> mycmd.columnize([str(i) for i in range(20)], 10) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 104 | 0 7 14 |
| 105 | 1 8 15 |
| 106 | 2 9 16 |
| 107 | 3 10 17 |
| 108 | 4 11 18 |
| 109 | 5 12 19 |
| 110 | 6 13 |
| 111 | |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 112 | This is an interactive test, put some commands in the cmdqueue attribute |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 113 | and let it execute |
| 114 | This test includes the preloop(), postloop(), default(), emptyline(), |
| 115 | parseline(), do_help() functions |
| 116 | >>> mycmd.use_rawinput=0 |
| 117 | >>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"] |
| 118 | >>> mycmd.cmdloop() |
| 119 | Hello from preloop |
| 120 | help text for add |
| 121 | *** invalid number of arguments |
| 122 | 9 |
| 123 | <BLANKLINE> |
| 124 | Documented commands (type help <topic>): |
| 125 | ======================================== |
Raymond Hettinger | 44d7b6a | 2010-09-09 03:53:22 +0000 | [diff] [blame] | 126 | add help |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 127 | <BLANKLINE> |
| 128 | Undocumented commands: |
| 129 | ====================== |
Raymond Hettinger | 44d7b6a | 2010-09-09 03:53:22 +0000 | [diff] [blame] | 130 | exit shell |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 131 | <BLANKLINE> |
| 132 | help text for add |
| 133 | Hello from postloop |
| 134 | """ |
| 135 | |
| 136 | def preloop(self): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 137 | print("Hello from preloop") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 138 | |
| 139 | def postloop(self): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 140 | print("Hello from postloop") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 141 | |
| 142 | def completedefault(self, *ignored): |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 143 | print("This is the completedefault method") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 144 | |
| 145 | def complete_command(self): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 146 | print("complete command") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 147 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 148 | def do_shell(self, s): |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 149 | pass |
| 150 | |
| 151 | def do_add(self, s): |
| 152 | l = s.split() |
| 153 | if len(l) != 2: |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 154 | print("*** invalid number of arguments") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 155 | return |
| 156 | try: |
| 157 | l = [int(i) for i in l] |
| 158 | except ValueError: |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 159 | print("*** arguments should be numbers") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 160 | return |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 161 | print(l[0]+l[1]) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 162 | |
| 163 | def help_add(self): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 164 | print("help text for add") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 165 | return |
| 166 | |
| 167 | def do_exit(self, arg): |
| 168 | return True |
| 169 | |
R. David Murray | 7905d61 | 2010-08-01 03:31:09 +0000 | [diff] [blame] | 170 | |
| 171 | class TestAlternateInput(unittest.TestCase): |
| 172 | |
| 173 | class simplecmd(cmd.Cmd): |
| 174 | |
| 175 | def do_print(self, args): |
| 176 | print(args, file=self.stdout) |
| 177 | |
| 178 | def do_EOF(self, args): |
| 179 | return True |
| 180 | |
Jesus Cea | 14ed7f2 | 2012-02-19 03:52:23 +0100 | [diff] [blame] | 181 | |
| 182 | class simplecmd2(simplecmd): |
| 183 | |
| 184 | def do_EOF(self, args): |
| 185 | print('*** Unknown syntax: EOF', file=self.stdout) |
| 186 | return True |
| 187 | |
| 188 | |
R. David Murray | 7905d61 | 2010-08-01 03:31:09 +0000 | [diff] [blame] | 189 | def test_file_with_missing_final_nl(self): |
| 190 | input = io.StringIO("print test\nprint test2") |
| 191 | output = io.StringIO() |
| 192 | cmd = self.simplecmd(stdin=input, stdout=output) |
| 193 | cmd.use_rawinput = False |
| 194 | cmd.cmdloop() |
| 195 | self.assertMultiLineEqual(output.getvalue(), |
| 196 | ("(Cmd) test\n" |
| 197 | "(Cmd) test2\n" |
| 198 | "(Cmd) ")) |
| 199 | |
| 200 | |
Jesus Cea | 14ed7f2 | 2012-02-19 03:52:23 +0100 | [diff] [blame] | 201 | def test_input_reset_at_EOF(self): |
| 202 | input = io.StringIO("print test\nprint test2") |
| 203 | output = io.StringIO() |
| 204 | cmd = self.simplecmd2(stdin=input, stdout=output) |
| 205 | cmd.use_rawinput = False |
| 206 | cmd.cmdloop() |
| 207 | self.assertMultiLineEqual(output.getvalue(), |
| 208 | ("(Cmd) test\n" |
| 209 | "(Cmd) test2\n" |
| 210 | "(Cmd) *** Unknown syntax: EOF\n")) |
| 211 | input = io.StringIO("print \n\n") |
| 212 | output = io.StringIO() |
| 213 | cmd.stdin = input |
| 214 | cmd.stdout = output |
| 215 | cmd.cmdloop() |
| 216 | self.assertMultiLineEqual(output.getvalue(), |
| 217 | ("(Cmd) \n" |
| 218 | "(Cmd) \n" |
| 219 | "(Cmd) *** Unknown syntax: EOF\n")) |
| 220 | |
| 221 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 222 | def test_main(verbose=None): |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 223 | from test import test_cmd |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 224 | support.run_doctest(test_cmd, verbose) |
R. David Murray | 7905d61 | 2010-08-01 03:31:09 +0000 | [diff] [blame] | 225 | support.run_unittest(TestAlternateInput) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 226 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 227 | def test_coverage(coverdir): |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 228 | trace = support.import_module('trace') |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 229 | tracer=trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 230 | trace=0, count=1) |
Berker Peksag | 67ef591 | 2014-06-30 04:04:52 +0300 | [diff] [blame] | 231 | tracer.run('import importlib; importlib.reload(cmd); test_main()') |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 232 | r=tracer.results() |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 233 | print("Writing coverage results...") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 234 | r.write_results(show_missing=True, summary=True, coverdir=coverdir) |
| 235 | |
| 236 | if __name__ == "__main__": |
| 237 | if "-c" in sys.argv: |
| 238 | test_coverage('/tmp/cmd.cover') |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 239 | elif "-i" in sys.argv: |
| 240 | samplecmdclass().cmdloop() |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 241 | else: |
| 242 | test_main() |