# HG changeset patch # User cat@eee # Date 1266884338 18000 # Node ID 0b77ca917fd2ed3265caa8bd773e4a1ab0915eba # Parent 70ca35d96c49c5b7208cda2d77a9a59f178c1b04 now attempting new outputtrap diff -r 70ca35d96c49 -r 0b77ca917fd2 cmd2.py --- a/cmd2.py Mon Feb 22 18:09:06 2010 -0500 +++ b/cmd2.py Mon Feb 22 19:18:58 2010 -0500 @@ -1445,7 +1445,21 @@ def tearDown(self): sys.stdout = self.old_stdout - +class OutputTrap(Borg): + '''Instantiate an OutputTrap to divert/capture ALL stdout output. For use in unit testing. + Call `tearDown()` to return to normal output.''' + def __init__(self): + self.contents = '' + self.old_stdout = sys.stdout + sys.stdout = self + def write(self, txt): + self.contents += txt + def read(self): + return self.contents + def tearDown(self): + sys.stdout = self.old_stdout + self.contents = '' + class Cmd2TestCase(unittest.TestCase): '''Subclass this, setting CmdApp, to make a unittest.TestCase class that will execute the commands in a transcript file and expect the results shown.