test_cmd.py 6.2 KB

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