Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Test script for the 'cmd' module |
| 4 | Original by Michael Schneider |
| 5 | """ |
| 6 | |
| 7 | |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 8 | import cmd |
| 9 | import sys |
| 10 | |
| 11 | class samplecmdclass(cmd.Cmd): |
| 12 | """ |
| 13 | Instance the sampleclass: |
| 14 | >>> mycmd = samplecmdclass() |
| 15 | |
| 16 | Test for the function parseline(): |
| 17 | >>> mycmd.parseline("") |
| 18 | (None, None, '') |
| 19 | >>> mycmd.parseline("?") |
| 20 | ('help', '', 'help ') |
| 21 | >>> mycmd.parseline("?help") |
| 22 | ('help', 'help', 'help help') |
| 23 | >>> mycmd.parseline("!") |
| 24 | ('shell', '', 'shell ') |
| 25 | >>> mycmd.parseline("!command") |
| 26 | ('shell', 'command', 'shell command') |
| 27 | >>> mycmd.parseline("func") |
| 28 | ('func', '', 'func') |
| 29 | >>> mycmd.parseline("func arg1") |
| 30 | ('func', 'arg1', 'func arg1') |
| 31 | |
| 32 | |
| 33 | Test for the function onecmd(): |
| 34 | >>> mycmd.onecmd("") |
| 35 | >>> mycmd.onecmd("add 4 5") |
| 36 | 9 |
| 37 | >>> mycmd.onecmd("") |
| 38 | 9 |
| 39 | >>> mycmd.onecmd("test") |
| 40 | *** Unknown syntax: test |
| 41 | |
| 42 | Test for the function emptyline(): |
| 43 | >>> mycmd.emptyline() |
| 44 | *** Unknown syntax: test |
| 45 | |
| 46 | Test for the function default(): |
| 47 | >>> mycmd.default("default") |
| 48 | *** Unknown syntax: default |
| 49 | |
| 50 | Test for the function completedefault(): |
| 51 | >>> mycmd.completedefault() |
| 52 | This is the completedefault methode |
| 53 | >>> mycmd.completenames("a") |
| 54 | ['add'] |
| 55 | |
| 56 | Test for the function completenames(): |
| 57 | >>> mycmd.completenames("12") |
| 58 | [] |
| 59 | >>> mycmd.completenames("help") |
Georg Brandl | 8904053 | 2010-01-06 18:02:16 +0000 | [diff] [blame] | 60 | ['help'] |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 61 | |
| 62 | Test for the function complete_help(): |
| 63 | >>> mycmd.complete_help("a") |
| 64 | ['add'] |
| 65 | >>> mycmd.complete_help("he") |
Georg Brandl | 8904053 | 2010-01-06 18:02:16 +0000 | [diff] [blame] | 66 | ['help'] |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 67 | >>> mycmd.complete_help("12") |
| 68 | [] |
Georg Brandl | 8904053 | 2010-01-06 18:02:16 +0000 | [diff] [blame] | 69 | >>> sorted(mycmd.complete_help("")) |
| 70 | ['add', 'exit', 'help', 'shell'] |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 71 | |
| 72 | Test for the function do_help(): |
| 73 | >>> mycmd.do_help("testet") |
| 74 | *** No help on testet |
| 75 | >>> mycmd.do_help("add") |
| 76 | help text for add |
| 77 | >>> mycmd.onecmd("help add") |
| 78 | help text for add |
| 79 | >>> mycmd.do_help("") |
| 80 | <BLANKLINE> |
| 81 | Documented commands (type help <topic>): |
| 82 | ======================================== |
| 83 | add |
| 84 | <BLANKLINE> |
| 85 | Undocumented commands: |
| 86 | ====================== |
| 87 | exit help shell |
| 88 | <BLANKLINE> |
| 89 | |
| 90 | Test for the function print_topics(): |
| 91 | >>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10) |
| 92 | header |
| 93 | ====== |
| 94 | command1 |
| 95 | command2 |
| 96 | <BLANKLINE> |
| 97 | |
| 98 | Test for the function columnize(): |
| 99 | >>> mycmd.columnize([str(i) for i in xrange(20)]) |
| 100 | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
| 101 | >>> mycmd.columnize([str(i) for i in xrange(20)], 10) |
| 102 | 0 7 14 |
| 103 | 1 8 15 |
| 104 | 2 9 16 |
| 105 | 3 10 17 |
| 106 | 4 11 18 |
| 107 | 5 12 19 |
| 108 | 6 13 |
| 109 | |
| 110 | This is a interactive test, put some commands in the cmdqueue attribute |
| 111 | and let it execute |
| 112 | This test includes the preloop(), postloop(), default(), emptyline(), |
| 113 | parseline(), do_help() functions |
| 114 | >>> mycmd.use_rawinput=0 |
| 115 | >>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"] |
| 116 | >>> mycmd.cmdloop() |
| 117 | Hello from preloop |
| 118 | help text for add |
| 119 | *** invalid number of arguments |
| 120 | 9 |
| 121 | <BLANKLINE> |
| 122 | Documented commands (type help <topic>): |
| 123 | ======================================== |
| 124 | add |
| 125 | <BLANKLINE> |
| 126 | Undocumented commands: |
| 127 | ====================== |
| 128 | exit help shell |
| 129 | <BLANKLINE> |
| 130 | help text for add |
| 131 | Hello from postloop |
| 132 | """ |
| 133 | |
| 134 | def preloop(self): |
| 135 | print "Hello from preloop" |
| 136 | |
| 137 | def postloop(self): |
| 138 | print "Hello from postloop" |
| 139 | |
| 140 | def completedefault(self, *ignored): |
| 141 | print "This is the completedefault methode" |
| 142 | return |
| 143 | |
| 144 | def complete_command(self): |
| 145 | print "complete command" |
| 146 | return |
| 147 | |
Georg Brandl | 5089a38 | 2010-01-06 17:43:06 +0000 | [diff] [blame] | 148 | def do_shell(self, s): |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 149 | pass |
| 150 | |
| 151 | def do_add(self, s): |
| 152 | l = s.split() |
| 153 | if len(l) != 2: |
| 154 | print "*** invalid number of arguments" |
| 155 | return |
| 156 | try: |
| 157 | l = [int(i) for i in l] |
| 158 | except ValueError: |
| 159 | print "*** arguments should be numbers" |
| 160 | return |
| 161 | print l[0]+l[1] |
| 162 | |
| 163 | def help_add(self): |
| 164 | print "help text for add" |
| 165 | return |
| 166 | |
| 167 | def do_exit(self, arg): |
| 168 | return True |
| 169 | |
| 170 | def test_main(verbose=None): |
| 171 | from test import test_support, test_cmd |
| 172 | test_support.run_doctest(test_cmd, verbose) |
| 173 | |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 174 | def test_coverage(coverdir): |
Georg Brandl | 5089a38 | 2010-01-06 17:43:06 +0000 | [diff] [blame] | 175 | import trace |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 176 | tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], |
| 177 | trace=0, count=1) |
| 178 | tracer.run('reload(cmd);test_main()') |
| 179 | r=tracer.results() |
| 180 | print "Writing coverage results..." |
| 181 | r.write_results(show_missing=True, summary=True, coverdir=coverdir) |
| 182 | |
| 183 | if __name__ == "__main__": |
| 184 | if "-c" in sys.argv: |
| 185 | test_coverage('/tmp/cmd.cover') |
Georg Brandl | 5089a38 | 2010-01-06 17:43:06 +0000 | [diff] [blame] | 186 | elif "-i" in sys.argv: |
| 187 | samplecmdclass().cmdloop() |
Georg Brandl | e4317fa | 2007-12-01 22:38:48 +0000 | [diff] [blame] | 188 | else: |
| 189 | test_main() |