home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / test_cmd.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  4.5 KB  |  186 lines

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