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