Cmd2
|
Public Member Functions | |
def | fetchTranscripts |
def | runTest |
def | setUp |
def | tearDown |
Public Attributes | |
cmdapp | |
outputTrap | |
transcripts | |
Static Public Attributes | |
tuple | anyWhitespace = re.compile(r'\s', re.DOTALL | re.MULTILINE) |
CmdApp = None | |
expectationParser = regexPattern|notRegexPattern | |
tuple | notRegexPattern = pyparsing.Word(pyparsing.printables) |
tuple | regexPattern = pyparsing.QuotedString(quoteChar=r'/', escChar='\\', multiline=True, unquoteResults=True) |
Private Member Functions | |
def | _test_transcript |
Subclass this, setting CmdApp, to make a unittest.TestCase class that will execute the commands in a transcript file and expect the results shown. See example.py
def cmd2::Cmd2TestCase::_test_transcript | ( | self, | |
fname, | |||
transcript | |||
) | [private] |
Definition at line 1506 of file cmd2.py.
References cmdapp.
Referenced by runTest().
01506 01507 def _test_transcript(self, fname, transcript): 01508 lineNum = 0 01509 finished = False 01510 line = transcript.next() 01511 lineNum += 1 01512 tests_run = 0 01513 while not finished: 01514 # Scroll forward to where actual commands begin 01515 while not line.startswith(self.cmdapp.prompt): 01516 try: 01517 line = transcript.next() 01518 except StopIteration: 01519 finished = True 01520 break 01521 lineNum += 1 01522 command = [line[len(self.cmdapp.prompt):]] 01523 line = transcript.next() 01524 # Read the entirety of a multi-line command 01525 while line.startswith(self.cmdapp.continuation_prompt): 01526 command.append(line[len(self.cmdapp.continuation_prompt):]) 01527 try: 01528 line = transcript.next() 01529 except StopIteration: 01530 raise (StopIteration, 01531 'Transcript broke off while reading command beginning at line %d with\n%s' 01532 % (command[0])) 01533 lineNum += 1 01534 command = ''.join(command) 01535 # Send the command into the application and capture the resulting output 01536 stop = self.cmdapp.onecmd_plus_hooks(command) 01537 #TODO: should act on ``stop`` 01538 result = self.outputTrap.read() 01539 # Read the expected result from transcript 01540 if line.startswith(self.cmdapp.prompt): 01541 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\ 01542 (fname, lineNum, command, result) 01543 self.assert_(not(result.strip()), message) 01544 continue 01545 expected = [] 01546 while not line.startswith(self.cmdapp.prompt): 01547 expected.append(line) 01548 try: 01549 line = transcript.next() 01550 except StopIteration: 01551 finished = True 01552 break 01553 lineNum += 1 01554 expected = ''.join(expected) 01555 # Compare actual result to expected 01556 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\ 01557 (fname, lineNum, command, expected, result) 01558 expected = self.expectationParser.transformString(expected) 01559 # checking whitespace is a pain - let's skip it 01560 expected = self.anyWhitespace.sub('', expected) 01561 result = self.anyWhitespace.sub('', result) 01562 self.assert_(re.match(expected, result, re.MULTILINE | re.DOTALL), message)
def cmd2::Cmd2TestCase::fetchTranscripts | ( | self | ) |
Definition at line 1481 of file cmd2.py.
Referenced by setUp().
01481 01482 def fetchTranscripts(self): 01483 self.transcripts = {} 01484 for fileset in self.CmdApp.testfiles: 01485 for fname in glob.glob(fileset): 01486 tfile = open(fname) 01487 self.transcripts[fname] = iter(tfile.readlines()) 01488 tfile.close() 01489 if not len(self.transcripts): raise (StandardError,), "No test files found - nothing to test."
def cmd2::Cmd2TestCase::runTest | ( | self | ) |
Definition at line 1495 of file cmd2.py.
References _test_transcript(), and CmdApp.
01495 01496 def runTest(self): # was testall 01497 if self.CmdApp: 01498 its = sorted(self.transcripts.items()) 01499 for (fname, transcript) in its: self._test_transcript(fname, transcript)
def cmd2::Cmd2TestCase::setUp | ( | self | ) |
Definition at line 1490 of file cmd2.py.
References CmdApp, cmdapp, fetchTranscripts(), and outputTrap.
01490 01491 def setUp(self): 01492 if self.CmdApp: 01493 self.outputTrap = OutputTrap() 01494 self.cmdapp = self.CmdApp() self.fetchTranscripts()
def cmd2::Cmd2TestCase::tearDown | ( | self | ) |
tuple cmd2::Cmd2TestCase::anyWhitespace = re.compile(r'\s', re.DOTALL | re.MULTILINE) [static] |
cmd2::Cmd2TestCase::CmdApp = None [static] |
Definition at line 1480 of file cmd2.py.
Referenced by runTest(), setUp(), and tearDown().
Definition at line 1490 of file cmd2.py.
Referenced by _test_transcript(), and setUp().
tuple cmd2::Cmd2TestCase::notRegexPattern = pyparsing.Word(pyparsing.printables) [static] |
tuple cmd2::Cmd2TestCase::regexPattern = pyparsing.QuotedString(quoteChar=r'/', escChar='\\', multiline=True, unquoteResults=True) [static] |