changeset 378:0b77ca917fd2

now attempting new outputtrap
author cat@eee
date Mon, 22 Feb 2010 19:18:58 -0500
parents 70ca35d96c49
children 1babd3df22d1
files cmd2.py
diffstat 1 files changed, 15 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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.