Mercurial > python-cmd2
comparison cmd2.py @ 254:07dff0ba981e
multiple test files
author | Catherine Devlin <catherine.devlin@gmail.com> |
---|---|
date | Wed, 01 Apr 2009 18:05:51 -0400 |
parents | 24289178b367 |
children | 573bf9c2a043 |
comparison
equal
deleted
inserted
replaced
253:24289178b367 | 254:07dff0ba981e |
---|---|
19 written to use `self.stdout.write()`, | 19 written to use `self.stdout.write()`, |
20 | 20 |
21 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com | 21 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com |
22 | 22 |
23 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2 | 23 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2 |
24 CHANGES: | |
25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. | |
26 flagReader.py options are still supported for backward compatibility | |
27 """ | 24 """ |
28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest | 25 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
29 import unittest, string, datetime, urllib, glob | 26 import unittest, string, datetime, urllib, glob |
30 from code import InteractiveConsole, InteractiveInterpreter, softspace | 27 from code import InteractiveConsole, InteractiveInterpreter, softspace |
31 from optparse import make_option | 28 from optparse import make_option |
1113 See example.py''' | 1110 See example.py''' |
1114 CmdApp = None | 1111 CmdApp = None |
1115 transcriptFileName = '' | 1112 transcriptFileName = '' |
1116 transcriptExtension = '' | 1113 transcriptExtension = '' |
1117 def fetchTranscripts(self): | 1114 def fetchTranscripts(self): |
1118 self.transcripts = {} | 1115 self.transcripts = [] |
1119 if self.transcriptExtension: | 1116 if self.CmdApp.testfile: |
1120 for fname in glob.glob('*.' + self.transcriptExtension): | 1117 transcriptfilenames = [self.CmdApp.testfile] |
1121 tfile = open(fname) | 1118 elif self.transcriptExtension: |
1122 self.transcripts[fname] = iter(tfile.readlines()) | 1119 transcriptfilenames = glob.glob('*.' + self.transcriptExtension) |
1123 tfile.close() | |
1124 elif self.transcriptFileName: | 1120 elif self.transcriptFileName: |
1125 tfile = open(os.path.expanduser(self.transcriptFileName)) | 1121 transcriptfilenames = [self.transcriptFileName] |
1126 self.transcripts = {'transcript': iter(tfile.readlines())} | 1122 for fname in transcriptfilenames: |
1123 tfile = open(fname) | |
1124 self.transcripts.append(iter(tfile.readlines())) | |
1127 tfile.close() | 1125 tfile.close() |
1128 else: | |
1129 raise AttributeError, 'Cmd2TestCase must define transcriptFileName or transcriptExtension' | |
1130 | 1126 |
1131 def setUp(self): | 1127 def setUp(self): |
1132 if self.CmdApp: | 1128 if self.CmdApp: |
1133 self.outputTrap = OutputTrap() | 1129 self.outputTrap = OutputTrap() |
1134 self.cmdapp = self.CmdApp() | 1130 self.cmdapp = self.CmdApp() |
1141 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \ | 1137 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \ |
1142 replace('\\ ', ' ') | 1138 replace('\\ ', ' ') |
1143 self.assert_(re.match(matchme, linegot.strip()), message) | 1139 self.assert_(re.match(matchme, linegot.strip()), message) |
1144 def testall(self): | 1140 def testall(self): |
1145 if self.CmdApp: | 1141 if self.CmdApp: |
1146 setup = 'setup.' + self.transcriptExtension | 1142 self.fetchTranscripts() |
1147 teardown = 'teardown.' + self.transcriptExtension | 1143 for transcript in self.transcripts: |
1148 transcripts = self.transcripts.copy() | 1144 self._test_transcript(transcript) |
1149 transcriptfiles = [] | 1145 def _test_transcript(self, transcript): |
1150 if setup in transcripts: | |
1151 transcriptfiles.append((setup, transcripts.pop(setup))) | |
1152 transcriptfiles.extend(sorted((fname, transcripts[fname]) for fname in transcripts if fname != teardown)) | |
1153 if teardown in transcripts: | |
1154 transcriptfiles.append((teardown, transcripts[teardown])) | |
1155 for transcript in transcriptfiles: | |
1156 self.testone(transcript) | |
1157 def testone(self, transcript): | |
1158 lineNum = 0 | 1146 lineNum = 0 |
1159 try: | 1147 try: |
1160 line = transcript.next() | 1148 line = transcript.next() |
1161 while True: | 1149 while True: |
1162 while not line.startswith(self.cmdapp.prompt): | 1150 while not line.startswith(self.cmdapp.prompt): |
1189 if self.CmdApp: | 1177 if self.CmdApp: |
1190 self.outputTrap.tearDown() | 1178 self.outputTrap.tearDown() |
1191 | 1179 |
1192 if __name__ == '__main__': | 1180 if __name__ == '__main__': |
1193 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) | 1181 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) |
1194 #c = Cmd() |