comparison cmd2.py @ 181:24eff658997b

accepts wildcards in tests, maybe?
author catherine@Elli.myhome.westell.com
date Wed, 24 Dec 2008 16:03:46 -0500
parents 951c124b0404
children 1c21db096f49
comparison
equal deleted inserted replaced
180:951c124b0404 181:24eff658997b
888 tfile = open(self.transcriptFileName) 888 tfile = open(self.transcriptFileName)
889 self.transcript = iter(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 assertEqualWithWildcards(self, got, expected, message): 893 def assertEqualEnough(self, got, expected, message):
894 got = got.strip() 894 got = got.strip().splitlines()
895 expected = expected.strip() 895 expected = expected.strip().splitlines()
896 try: 896 self.assertEqual(len(got), len(expected), message)
897 self.assertEqual(got, expected, message) 897 for (linegot, lineexpected) in zip(got, expected):
898 except AssertionError: 898 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \
899 print 'ooh, this is bad' 899 replace('\\ ', ' ')
900 raise 900 self.assert_(re.match(matchme, linegot.strip()), message)
901 def testall(self): 901 def testall(self):
902 if self.CmdApp: 902 if self.CmdApp:
903 lineNum = 0 903 lineNum = 0
904 try: 904 try:
905 line = self.transcript.next() 905 line = self.transcript.next()
913 line = self.transcript.next() 913 line = self.transcript.next()
914 command = ''.join(command) 914 command = ''.join(command)
915 self.cmdapp.onecmd(command) 915 self.cmdapp.onecmd(command)
916 result = self.outputTrap.read() 916 result = self.outputTrap.read()
917 if line.startswith(self.cmdapp.prompt): 917 if line.startswith(self.cmdapp.prompt):
918 self.assertEqual(result.strip(), '', 918 self.assertEqualEnough(result.strip(), '',
919 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % 919 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' %
920 (self.transcriptFileName, lineNum, command, result)) 920 (self.transcriptFileName, lineNum, command, result))
921 continue 921 continue
922 expected = [] 922 expected = []
923 while not line.startswith(self.cmdapp.prompt): 923 while not line.startswith(self.cmdapp.prompt):
924 expected.append(line) 924 expected.append(line)
925 line = self.transcript.next() 925 line = self.transcript.next()
926 expected = ''.join(expected) 926 expected = ''.join(expected)
927 self.assertEqual(expected.strip(), result.strip(), 927 self.assertEqualEnough(expected.strip(), result.strip(),
928 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % 928 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' %
929 (self.transcriptFileName, lineNum, command, expected, result)) 929 (self.transcriptFileName, lineNum, command, expected, result))
930 # this needs to account for a line-by-line strip()ping 930 # this needs to account for a line-by-line strip()ping
931 except StopIteration: 931 except StopIteration:
932 pass 932 pass
933 # catch the final output? 933 # catch the final output?
934 def tearDown(self): 934 def tearDown(self):
935 if self.CmdApp: 935 if self.CmdApp:
936 self.tfile.close()
937 self.outputTrap.tearDown() 936 self.outputTrap.tearDown()
938 937
939 if __name__ == '__main__': 938 if __name__ == '__main__':
940 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) 939 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE)
941 #c = Cmd() 940 #c = Cmd()