comparison cmd2.py @ 180:951c124b0404

test almost fixed
author catherine@Elli.myhome.westell.com
date Wed, 24 Dec 2008 11:15:03 -0500
parents 321e0cc35661
children 24eff658997b
comparison
equal deleted inserted replaced
179:321e0cc35661 180:951c124b0404
884 if self.CmdApp: 884 if self.CmdApp:
885 self.outputTrap = OutputTrap() 885 self.outputTrap = OutputTrap()
886 self.cmdapp = self.CmdApp() 886 self.cmdapp = self.CmdApp()
887 try: 887 try:
888 tfile = open(self.transcriptFileName) 888 tfile = open(self.transcriptFileName)
889 self.transcript = tfile.readlines() 889 self.transcript = iter(tfile.readlines())
890 tfile.close() 890 tfile.close()
891 except IOError: 891 except IOError:
892 self.transcript = [] 892 self.transcript = []
893 def divideTranscript(self):
894 self.dialogue = []
895 commandStart = None
896 response = []
897 command = []
898 prompt = self.cmdapp.prompt.rstrip()
899 continuationPrompt = self.cmdapp.continuationPrompt.rstrip()
900 for (lineNum, line) in enumerate(self.transcript):
901 if line.startswith(prompt):
902 if command:
903 self.dialogue.append((commandStart, ''.join(command), ''.join(response)))
904 command = [line[len(prompt)+1:]]
905 commandStart = lineNum
906 response = []
907 elif line.startswith(continuationPrompt):
908 command.append(line[len(continuationPrompt)+1:])
909 else:
910 response.append(line)
911 def assertEqualWithWildcards(self, got, expected, message): 893 def assertEqualWithWildcards(self, got, expected, message):
912 got = got.strip() 894 got = got.strip()
913 expected = expected.strip() 895 expected = expected.strip()
914 try: 896 try:
915 self.assertEqual(got, expected, message) 897 self.assertEqual(got, expected, message)
916 except AssertionError: 898 except AssertionError:
917 print 'ooh, this is bad' 899 print 'ooh, this is bad'
918 raise 900 raise
919 def testall(self): 901 def testall(self):
920 if self.CmdApp: 902 if self.CmdApp:
921 self.divideTranscript() 903 lineNum = 0
922 for (lineNum, command, expected) in self.dialogue: 904 try:
923 self.cmdapp.onecmd(command) 905 line = self.transcript.next()
924 result = self.outputTrap.read() 906 while True:
925 self.assertEqualWithWildcards(result, expected, 907 while not line.startswith(self.cmdapp.prompt):
926 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % 908 line = self.transcript.next()
927 (self.transcriptFileName, lineNum, command, expected, result)) 909 command = [line[len(self.cmdapp.prompt):]]
910 line = self.transcript.next()
911 while line.startswith(self.cmdapp.continuationPrompt):
912 command.append(line[len(self.cmdapp.continuationPrompt):])
913 line = self.transcript.next()
914 command = ''.join(command)
915 self.cmdapp.onecmd(command)
916 result = self.outputTrap.read()
917 if line.startswith(self.cmdapp.prompt):
918 self.assertEqual(result.strip(), '',
919 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' %
920 (self.transcriptFileName, lineNum, command, result))
921 continue
922 expected = []
923 while not line.startswith(self.cmdapp.prompt):
924 expected.append(line)
925 line = self.transcript.next()
926 expected = ''.join(expected)
927 self.assertEqual(expected.strip(), result.strip(),
928 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' %
929 (self.transcriptFileName, lineNum, command, expected, result))
930 # this needs to account for a line-by-line strip()ping
931 except StopIteration:
932 pass
933 # catch the final output?
928 def tearDown(self): 934 def tearDown(self):
929 if self.CmdApp: 935 if self.CmdApp:
936 self.tfile.close()
930 self.outputTrap.tearDown() 937 self.outputTrap.tearDown()
931 938
932 if __name__ == '__main__': 939 if __name__ == '__main__':
933 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) 940 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE)
934 #c = Cmd() 941 #c = Cmd()