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