comparison cmd2/cmd2.py @ 105:130340609e19

testing beginning to work?
author catherine@dellzilla
date Fri, 24 Oct 2008 14:32:23 -0400
parents bd91925d813c
children 2d3232693807
comparison
equal deleted inserted replaced
104:bd91925d813c 105:130340609e19
686 return obj 686 return obj
687 687
688 class OutputTrap(Borg): 688 class OutputTrap(Borg):
689 '''Instantiate an OutputTrap to divert/capture ALL stdout output. For use in unit testing. 689 '''Instantiate an OutputTrap to divert/capture ALL stdout output. For use in unit testing.
690 Call `tearDown()` to return to normal output.''' 690 Call `tearDown()` to return to normal output.'''
691 old_stdout = sys.stdout
692 def __init__(self): 691 def __init__(self):
693 #self.old_stdout = sys.stdout 692 self.old_stdout = sys.stdout
694 self.trap = tempfile.TemporaryFile() 693 self.trap = tempfile.TemporaryFile()
695 sys.stdout = self.trap 694 sys.stdout = self.trap
696 def dump(self): 695 def read(self):
697 'Reads trapped stdout output.'
698 self.trap.seek(0) 696 self.trap.seek(0)
699 result = self.trap.read() 697 result = self.trap.read()
700 self.trap.close() 698 self.trap.truncate(0)
701 self.trap = tempfile.TemporaryFile() 699 return result.strip('\x00')
702 sys.stdout = self.trap
703 return result
704 def tearDown(self): 700 def tearDown(self):
705 sys.stdout = self.old_stdout 701 sys.stdout = self.old_stdout
706 702
707 class TranscriptReader(object): 703 class TranscriptReader(object):
708 def __init__(self, cmdapp, filename='test_cmd2.txt'): 704 def __init__(self, cmdapp, filename='test_cmd2.txt'):
742 # problem: this (raw) case gets called by unittest.main - we don't want it to be. hmm 738 # problem: this (raw) case gets called by unittest.main - we don't want it to be. hmm
743 CmdApp = None 739 CmdApp = None
744 transcriptFileName = '' 740 transcriptFileName = ''
745 def setUp(self): 741 def setUp(self):
746 if self.CmdApp: 742 if self.CmdApp:
743 self.outputTrap = OutputTrap()
747 self.cmdapp = self.CmdApp() 744 self.cmdapp = self.CmdApp()
748 self.outputTrap = OutputTrap()
749 self.transcriptReader = TranscriptReader(self.cmdapp, self.transcriptFileName) 745 self.transcriptReader = TranscriptReader(self.cmdapp, self.transcriptFileName)
750 def testall(self): 746 def testall(self):
751 if self.CmdApp: 747 if self.CmdApp:
752 for (cmdInput, lineNum) in self.transcriptReader.inputGenerator(): 748 for (cmdInput, lineNum) in self.transcriptReader.inputGenerator():
753 self.cmdapp.onecmd(cmdInput) 749 self.cmdapp.onecmd(cmdInput)
754 result = self.outputTrap.dump() 750 result = self.outputTrap.read()
755 expected = self.transcriptReader.nextExpected() 751 expected = self.transcriptReader.nextExpected()
756 self.assertEqual(result.strip(), expected.strip(), 752 self.assertEqual(result.strip(), expected.strip(),
757 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % 753 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' %
758 (self.transcriptFileName, lineNum, cmdInput, expected, result)) 754 (self.transcriptFileName, lineNum, cmdInput, expected, result))
759 def tearDown(self): 755 def tearDown(self):