Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Test script for the 'cmd' module |
| 4 | Original by Michael Schneider |
| 5 | """ |
| 6 | |
| 7 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 8 | import cmd |
| 9 | import sys |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 10 | import trace |
| 11 | import re |
R. David Murray | ea6ff81 | 2010-08-01 04:14:22 +0000 | [diff] [blame] | 12 | import unittest |
| 13 | import io |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 14 | |
| 15 | class samplecmdclass(cmd.Cmd): |
| 16 | """ |
| 17 | Instance the sampleclass: |
| 18 | >>> mycmd = samplecmdclass() |
| 19 | |
| 20 | Test for the function parseline(): |
| 21 | >>> mycmd.parseline("") |
| 22 | (None, None, '') |
| 23 | >>> mycmd.parseline("?") |
| 24 | ('help', '', 'help ') |
| 25 | >>> mycmd.parseline("?help") |
| 26 | ('help', 'help', 'help help') |
| 27 | >>> mycmd.parseline("!") |
| 28 | ('shell', '', 'shell ') |
| 29 | >>> mycmd.parseline("!command") |
| 30 | ('shell', 'command', 'shell command') |
| 31 | >>> mycmd.parseline("func") |
| 32 | ('func', '', 'func') |
| 33 | >>> mycmd.parseline("func arg1") |
| 34 | ('func', 'arg1', 'func arg1') |
| 35 | |
| 36 | |
| 37 | Test for the function onecmd(): |
| 38 | >>> mycmd.onecmd("") |
| 39 | >>> mycmd.onecmd("add 4 5") |
| 40 | 9 |
| 41 | >>> mycmd.onecmd("") |
| 42 | 9 |
| 43 | >>> mycmd.onecmd("test") |
| 44 | *** Unknown syntax: test |
| 45 | |
| 46 | Test for the function emptyline(): |
| 47 | >>> mycmd.emptyline() |
| 48 | *** Unknown syntax: test |
| 49 | |
| 50 | Test for the function default(): |
| 51 | >>> mycmd.default("default") |
| 52 | *** Unknown syntax: default |
| 53 | |
| 54 | Test for the function completedefault(): |
| 55 | >>> mycmd.completedefault() |
| 56 | This is the completedefault methode |
| 57 | >>> mycmd.completenames("a") |
| 58 | ['add'] |
| 59 | |
| 60 | Test for the function completenames(): |
| 61 | >>> mycmd.completenames("12") |
| 62 | [] |
| 63 | >>> mycmd.completenames("help") |
| 64 | ['help', 'help'] |
| 65 | |
| 66 | Test for the function complete_help(): |
| 67 | >>> mycmd.complete_help("a") |
| 68 | ['add'] |
| 69 | >>> mycmd.complete_help("he") |
| 70 | ['help', 'help'] |
| 71 | >>> mycmd.complete_help("12") |
| 72 | [] |
| 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 | ======================================== |
| 85 | add |
| 86 | <BLANKLINE> |
| 87 | Undocumented commands: |
| 88 | ====================== |
| 89 | exit help shell |
| 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 | |
| 112 | This is a interactive test, put some commands in the cmdqueue attribute |
| 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 | ======================================== |
| 126 | add |
| 127 | <BLANKLINE> |
| 128 | Undocumented commands: |
| 129 | ====================== |
| 130 | exit help shell |
| 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): |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 143 | print("This is the completedefault methode") |
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 | |
| 148 | def do_shell(self): |
| 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 | ea6ff81 | 2010-08-01 04:14:22 +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 | |
| 181 | def test_file_with_missing_final_nl(self): |
| 182 | input = io.StringIO("print test\nprint test2") |
| 183 | output = io.StringIO() |
| 184 | cmd = self.simplecmd(stdin=input, stdout=output) |
| 185 | cmd.use_rawinput = False |
| 186 | cmd.cmdloop() |
| 187 | self.assertMultiLineEqual(output.getvalue(), |
| 188 | ("(Cmd) test\n" |
| 189 | "(Cmd) test2\n" |
| 190 | "(Cmd) ")) |
| 191 | |
| 192 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 193 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 194 | from test import support, test_cmd |
| 195 | support.run_doctest(test_cmd, verbose) |
R. David Murray | ea6ff81 | 2010-08-01 04:14:22 +0000 | [diff] [blame] | 196 | support.run_unittest(TestAlternateInput) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 197 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 198 | def test_coverage(coverdir): |
| 199 | tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], |
| 200 | trace=0, count=1) |
| 201 | tracer.run('reload(cmd);test_main()') |
| 202 | r=tracer.results() |
Christian Heimes | 2137b6a | 2007-12-02 16:50:20 +0000 | [diff] [blame] | 203 | print("Writing coverage results...") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 204 | r.write_results(show_missing=True, summary=True, coverdir=coverdir) |
| 205 | |
| 206 | if __name__ == "__main__": |
| 207 | if "-c" in sys.argv: |
| 208 | test_coverage('/tmp/cmd.cover') |
| 209 | else: |
| 210 | test_main() |