/Lib/test/test_cmd.py

http://unladen-swallow.googlecode.com/ · Python · 185 lines · 183 code · 0 blank · 2 comment · 0 complexity · 552ec73153ddb536c60ff8077835623b MD5 · raw file

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