cpython/Lib/test/test_cmd.py

188 lines
4.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
Test script for the 'cmd' module
Original by Michael Schneider
"""
import cmd
import sys
class samplecmdclass(cmd.Cmd):
"""
Instance the sampleclass:
>>> mycmd = samplecmdclass()
Test for the function parseline():
>>> mycmd.parseline("")
(None, None, '')
>>> mycmd.parseline("?")
('help', '', 'help ')
>>> mycmd.parseline("?help")
('help', 'help', 'help help')
>>> mycmd.parseline("!")
('shell', '', 'shell ')
>>> mycmd.parseline("!command")
('shell', 'command', 'shell command')
>>> mycmd.parseline("func")
('func', '', 'func')
>>> mycmd.parseline("func arg1")
('func', 'arg1', 'func arg1')
Test for the function onecmd():
>>> mycmd.onecmd("")
>>> mycmd.onecmd("add 4 5")
9
>>> mycmd.onecmd("")
9
>>> mycmd.onecmd("test")
*** Unknown syntax: test
Test for the function emptyline():
>>> mycmd.emptyline()
*** Unknown syntax: test
Test for the function default():
>>> mycmd.default("default")
*** Unknown syntax: default
Test for the function completedefault():
>>> mycmd.completedefault()
This is the completedefault methode
>>> mycmd.completenames("a")
['add']
Test for the function completenames():
>>> mycmd.completenames("12")
[]
>>> mycmd.completenames("help")
['help', 'help']
Test for the function complete_help():
>>> mycmd.complete_help("a")
['add']
>>> mycmd.complete_help("he")
['help', 'help']
>>> mycmd.complete_help("12")
[]
Test for the function do_help():
>>> mycmd.do_help("testet")
*** No help on testet
>>> mycmd.do_help("add")
help text for add
>>> mycmd.onecmd("help add")
help text for add
>>> mycmd.do_help("")
<BLANKLINE>
Documented commands (type help <topic>):
========================================
add
<BLANKLINE>
Undocumented commands:
======================
exit help shell
<BLANKLINE>
Test for the function print_topics():
>>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10)
header
======
command1
command2
<BLANKLINE>
Test for the function columnize():
>>> mycmd.columnize([str(i) for i in xrange(20)])
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
>>> mycmd.columnize([str(i) for i in xrange(20)], 10)
0 7 14
1 8 15
2 9 16
3 10 17
4 11 18
5 12 19
6 13
This is a interactive test, put some commands in the cmdqueue attribute
and let it execute
This test includes the preloop(), postloop(), default(), emptyline(),
parseline(), do_help() functions
>>> mycmd.use_rawinput=0
>>> mycmd.cmdqueue=["", "add", "add 4 5", "help", "help add","exit"]
>>> mycmd.cmdloop()
Hello from preloop
help text for add
*** invalid number of arguments
9
<BLANKLINE>
Documented commands (type help <topic>):
========================================
add
<BLANKLINE>
Undocumented commands:
======================
exit help shell
<BLANKLINE>
help text for add
Hello from postloop
"""
def preloop(self):
print "Hello from preloop"
def postloop(self):
print "Hello from postloop"
def completedefault(self, *ignored):
print "This is the completedefault methode"
return
def complete_command(self):
print "complete command"
return
Merged revisions 77120,77151,77155,77209,77229,77256,77317,77331,77333,77359-77360,77382,77561,77570 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77120 | georg.brandl | 2009-12-29 22:09:17 +0100 (Di, 29 Dez 2009) | 1 line #7595: fix typo in argument default constant. ........ r77151 | georg.brandl | 2009-12-30 19:32:50 +0100 (Mi, 30 Dez 2009) | 1 line #7487: update Pygments version. ........ r77155 | georg.brandl | 2009-12-30 20:03:00 +0100 (Mi, 30 Dez 2009) | 1 line We only support Windows NT derivatives now. ........ r77209 | georg.brandl | 2010-01-01 14:07:05 +0100 (Fr, 01 Jan 2010) | 1 line More yearly updates. ........ r77229 | georg.brandl | 2010-01-02 13:35:01 +0100 (Sa, 02 Jan 2010) | 1 line Fix casing. ........ r77256 | georg.brandl | 2010-01-02 23:55:55 +0100 (Sa, 02 Jan 2010) | 1 line Fix typo. ........ r77317 | georg.brandl | 2010-01-05 19:14:52 +0100 (Di, 05 Jan 2010) | 1 line Add Stefan. ........ r77331 | georg.brandl | 2010-01-06 18:43:06 +0100 (Mi, 06 Jan 2010) | 1 line Small fixes to test_cmd: fix signature of do_shell, remove duplicate import, add option to run the custom Cmd class. ........ r77333 | georg.brandl | 2010-01-06 19:26:08 +0100 (Mi, 06 Jan 2010) | 1 line #5950: document that zip files with comments are unsupported in zipimport. ........ r77359 | georg.brandl | 2010-01-07 21:54:45 +0100 (Do, 07 Jan 2010) | 1 line Fix description for Py_GetPath(); it sounded like it always returned sys.path. ........ r77360 | georg.brandl | 2010-01-07 22:48:47 +0100 (Do, 07 Jan 2010) | 1 line #7653: clarify how the PythonPath registry key should look like. ........ r77382 | georg.brandl | 2010-01-09 10:47:11 +0100 (Sa, 09 Jan 2010) | 1 line #7422: make it clear that getargspec() only works on Python functions. ........ r77561 | georg.brandl | 2010-01-17 09:42:30 +0100 (So, 17 Jan 2010) | 1 line #7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such. ........ r77570 | georg.brandl | 2010-01-17 13:14:42 +0100 (So, 17 Jan 2010) | 1 line Add note about usage of STRINGLIB_EMPTY. ........
2010-03-21 16:16:28 -03:00
def do_shell(self, s):
pass
def do_add(self, s):
l = s.split()
if len(l) != 2:
print "*** invalid number of arguments"
return
try:
l = [int(i) for i in l]
except ValueError:
print "*** arguments should be numbers"
return
print l[0]+l[1]
def help_add(self):
print "help text for add"
return
def do_exit(self, arg):
return True
def test_main(verbose=None):
from test import test_support, test_cmd
test_support.run_doctest(test_cmd, verbose)
def test_coverage(coverdir):
Merged revisions 77120,77151,77155,77209,77229,77256,77317,77331,77333,77359-77360,77382,77561,77570 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77120 | georg.brandl | 2009-12-29 22:09:17 +0100 (Di, 29 Dez 2009) | 1 line #7595: fix typo in argument default constant. ........ r77151 | georg.brandl | 2009-12-30 19:32:50 +0100 (Mi, 30 Dez 2009) | 1 line #7487: update Pygments version. ........ r77155 | georg.brandl | 2009-12-30 20:03:00 +0100 (Mi, 30 Dez 2009) | 1 line We only support Windows NT derivatives now. ........ r77209 | georg.brandl | 2010-01-01 14:07:05 +0100 (Fr, 01 Jan 2010) | 1 line More yearly updates. ........ r77229 | georg.brandl | 2010-01-02 13:35:01 +0100 (Sa, 02 Jan 2010) | 1 line Fix casing. ........ r77256 | georg.brandl | 2010-01-02 23:55:55 +0100 (Sa, 02 Jan 2010) | 1 line Fix typo. ........ r77317 | georg.brandl | 2010-01-05 19:14:52 +0100 (Di, 05 Jan 2010) | 1 line Add Stefan. ........ r77331 | georg.brandl | 2010-01-06 18:43:06 +0100 (Mi, 06 Jan 2010) | 1 line Small fixes to test_cmd: fix signature of do_shell, remove duplicate import, add option to run the custom Cmd class. ........ r77333 | georg.brandl | 2010-01-06 19:26:08 +0100 (Mi, 06 Jan 2010) | 1 line #5950: document that zip files with comments are unsupported in zipimport. ........ r77359 | georg.brandl | 2010-01-07 21:54:45 +0100 (Do, 07 Jan 2010) | 1 line Fix description for Py_GetPath(); it sounded like it always returned sys.path. ........ r77360 | georg.brandl | 2010-01-07 22:48:47 +0100 (Do, 07 Jan 2010) | 1 line #7653: clarify how the PythonPath registry key should look like. ........ r77382 | georg.brandl | 2010-01-09 10:47:11 +0100 (Sa, 09 Jan 2010) | 1 line #7422: make it clear that getargspec() only works on Python functions. ........ r77561 | georg.brandl | 2010-01-17 09:42:30 +0100 (So, 17 Jan 2010) | 1 line #7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such. ........ r77570 | georg.brandl | 2010-01-17 13:14:42 +0100 (So, 17 Jan 2010) | 1 line Add note about usage of STRINGLIB_EMPTY. ........
2010-03-21 16:16:28 -03:00
import trace
tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
trace=0, count=1)
tracer.run('reload(cmd);test_main()')
r=tracer.results()
print "Writing coverage results..."
r.write_results(show_missing=True, summary=True, coverdir=coverdir)
if __name__ == "__main__":
if "-c" in sys.argv:
test_coverage('/tmp/cmd.cover')
Merged revisions 77120,77151,77155,77209,77229,77256,77317,77331,77333,77359-77360,77382,77561,77570 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77120 | georg.brandl | 2009-12-29 22:09:17 +0100 (Di, 29 Dez 2009) | 1 line #7595: fix typo in argument default constant. ........ r77151 | georg.brandl | 2009-12-30 19:32:50 +0100 (Mi, 30 Dez 2009) | 1 line #7487: update Pygments version. ........ r77155 | georg.brandl | 2009-12-30 20:03:00 +0100 (Mi, 30 Dez 2009) | 1 line We only support Windows NT derivatives now. ........ r77209 | georg.brandl | 2010-01-01 14:07:05 +0100 (Fr, 01 Jan 2010) | 1 line More yearly updates. ........ r77229 | georg.brandl | 2010-01-02 13:35:01 +0100 (Sa, 02 Jan 2010) | 1 line Fix casing. ........ r77256 | georg.brandl | 2010-01-02 23:55:55 +0100 (Sa, 02 Jan 2010) | 1 line Fix typo. ........ r77317 | georg.brandl | 2010-01-05 19:14:52 +0100 (Di, 05 Jan 2010) | 1 line Add Stefan. ........ r77331 | georg.brandl | 2010-01-06 18:43:06 +0100 (Mi, 06 Jan 2010) | 1 line Small fixes to test_cmd: fix signature of do_shell, remove duplicate import, add option to run the custom Cmd class. ........ r77333 | georg.brandl | 2010-01-06 19:26:08 +0100 (Mi, 06 Jan 2010) | 1 line #5950: document that zip files with comments are unsupported in zipimport. ........ r77359 | georg.brandl | 2010-01-07 21:54:45 +0100 (Do, 07 Jan 2010) | 1 line Fix description for Py_GetPath(); it sounded like it always returned sys.path. ........ r77360 | georg.brandl | 2010-01-07 22:48:47 +0100 (Do, 07 Jan 2010) | 1 line #7653: clarify how the PythonPath registry key should look like. ........ r77382 | georg.brandl | 2010-01-09 10:47:11 +0100 (Sa, 09 Jan 2010) | 1 line #7422: make it clear that getargspec() only works on Python functions. ........ r77561 | georg.brandl | 2010-01-17 09:42:30 +0100 (So, 17 Jan 2010) | 1 line #7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such. ........ r77570 | georg.brandl | 2010-01-17 13:14:42 +0100 (So, 17 Jan 2010) | 1 line Add note about usage of STRINGLIB_EMPTY. ........
2010-03-21 16:16:28 -03:00
elif "-i" in sys.argv:
samplecmdclass().cmdloop()
else:
test_main()