# HG changeset patch # User catherine@dellzilla # Date 1265930537 18000 # Node ID e9eea93c777c1b47d563a58dfd0e18881508009f # Parent 237a89d5a4a91ea133daac349ac817058db8316b initial args working diff -r 237a89d5a4a9 -r e9eea93c777c cmd2.py --- a/cmd2.py Thu Feb 11 17:47:29 2010 -0500 +++ b/cmd2.py Thu Feb 11 18:22:17 2010 -0500 @@ -1478,13 +1478,13 @@ self.outputTrap.tearDown() def run(app): - class TestMyAppCase(Cmd2TestCase): - CmdApp = app.__class__ parser = optparse.OptionParser() parser.add_option('-t', '--test', dest='test', action="store_true", help='Test against transcript(s) in FILE (wildcards OK)') (callopts, callargs) = parser.parse_args() if callopts.test: + class TestMyAppCase(Cmd2TestCase): + CmdApp = app.__class__ app.__class__.testfiles = callargs sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() testcase = TestMyAppCase() @@ -1492,31 +1492,37 @@ result = runner.run(testcase) result.printErrors() else: + # hold onto the args and run .onecmd with them + # in sqlpython, first arg has implied \connect + for initial_command in callargs: + if app.onecmd(initial_command + '\n') == app._STOP_AND_EXIT: + return app.cmdloop() if __name__ == '__main__': doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) ''' -To make your application transcript-testable, add text like this to your .py file -(replacing CmdLineApp with your own application class's name). Then, a cut-and-pasted -version of a successful session with your application, saved as a text file, can serve -as a test for future +To make your application transcript-testable, replace + +:: + + app = MyApp() + app.cmdloop() + +with + +:: -Invoke the test later with `python myapplication.py --test mytranscripttestfile.ext` + app = MyApp() + cmd2.run(app) + +Then run a session of your application and paste the entire screen contents +into a file, ``transcript.test``, and invoke the test like:: + + python myapp.py --test transcript.test + Wildcards can be used to test against multiple transcript files. +''' -class TestMyAppCase(Cmd2TestCase): - CmdApp = CmdLineApp -parser = optparse.OptionParser() -parser.add_option('-t', '--test', dest='test', action="store_true", - help='Test against transcript(s) in FILE (wildcards OK)') -(callopts, callargs) = parser.parse_args() -if callopts.test: - CmdLineApp.testfiles = callargs - sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() - unittest.main() -else: - CmdLineApp().cmdloop() -''' diff -r 237a89d5a4a9 -r e9eea93c777c example/example.py --- a/example/example.py Thu Feb 11 17:47:29 2010 -0500 +++ b/example/example.py Thu Feb 11 18:22:17 2010 -0500 @@ -1,6 +1,6 @@ '''A sample application for cmd2.''' -from cmd2 import Cmd, make_option, options, Cmd2TestCase, run +from cmd2 import Cmd, make_option, options, run import unittest, optparse, sys class CmdLineApp(Cmd): @@ -32,17 +32,3 @@ c = CmdLineApp() run(c) - -''' -class TestMyAppCase(Cmd2TestCase): - CmdApp = CmdLineApp -parser = optparse.OptionParser() -parser.add_option('-t', '--test', dest='test', action="store_true", - help='Test against transcript(s) in FILE (wildcards OK)') -(callopts, callargs) = parser.parse_args() -if callopts.test: - CmdLineApp.testfiles = callargs - sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() - unittest.main() -else: - CmdLineApp().cmdloop()''' diff -r 237a89d5a4a9 -r e9eea93c777c example/exampleSession.txt --- a/example/exampleSession.txt Thu Feb 11 17:47:29 2010 -0500 +++ b/example/exampleSession.txt Thu Feb 11 18:22:17 2010 -0500 @@ -45,7 +45,7 @@ maxrepeats - was: 3 now: 5 (Cmd) say -ps --repeat=5 goodnight, Gracie -OODNIGHT, GRACIEGAYo +OODNIGHT, GRACIEGAY OODNIGHT, GRACIEGAY OODNIGHT, GRACIEGAY OODNIGHT, GRACIEGAY