summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZearin <zearin@gonk.net>2011-10-10 09:40:59 -0400
committerZearin <zearin@gonk.net>2011-10-10 09:40:59 -0400
commit5339cdce11d5955c07bebb61eb01a85f7877145c (patch)
treecc7d4a7758eef69c11556cb267b01c49b007fa99
parentfc26bbc3fdafdbb26a6ad055c85bf572c620554a (diff)
downloadcmd2-5339cdce11d5955c07bebb61eb01a85f7877145c.tar.gz
Added runTranscriptTests() to tests.py.
-rw-r--r--tests.py66
1 files changed, 41 insertions, 25 deletions
diff --git a/tests.py b/tests.py
index 3594c73..ec3b413 100644
--- a/tests.py
+++ b/tests.py
@@ -3,15 +3,16 @@ This file should contain all tests for Cmd2.
'''
+
class Cmd2TestCase(unittest.TestCase):
'''
Subclass this (setting CmdApp) to make a "unittest.TestCase" class
that executes commands from a transcript file and expects the results shown.
-
+
See example.py.
'''
CmdApp = None
-
+
def fetchTranscripts(self):
self.transcripts = {}
for fileset in self.CmdApp.testfiles:
@@ -21,35 +22,35 @@ class Cmd2TestCase(unittest.TestCase):
tfile.close()
if not len(self.transcripts):
raise (StandardError,), "No test files found...nothing to test."
-
+
def setUp(self):
if self.CmdApp:
self.outputTrap = OutputTrap()
self.cmdapp = self.CmdApp()
self.fetchTranscripts()
-
+
def runTest(self): # was testall
if self.CmdApp:
its = sorted(self.transcripts.items())
for (fname, transcript) in its:
self._test_transcript(fname, transcript)
-
+
regexPattern = pyparsing.QuotedString(quoteChar=r'/', escChar='\\', multiline=True, unquoteResults=True)
regexPattern.ignore(pyparsing.cStyleComment)
-
+
notRegexPattern = pyparsing.Word(pyparsing.printables)
notRegexPattern.setParseAction(lambda t: re.escape(t[0]))
-
+
expectationParser = regexPattern | notRegexPattern
anyWhitespace = re.compile(r'\s', re.DOTALL | re.MULTILINE)
-
+
def _test_transcript(self, fname, transcript):
lineNum = 0
finished = False
line = transcript.next()
lineNum += 1
tests_run = 0
-
+
while not finished:
# Scroll forward to where actual commands begin
while not line.startswith(self.cmdapp.prompt):
@@ -61,51 +62,51 @@ class Cmd2TestCase(unittest.TestCase):
lineNum += 1
command = [line[len(self.cmdapp.prompt):]]
line = transcript.next()
-
+
# Read the entirety of a multi-line command
while line.startswith(self.cmdapp.continuation_prompt):
command.append(line[len(self.cmdapp.continuation_prompt):])
try:
line = transcript.next()
except StopIteration:
- raise (StopIteration,
- 'Transcript broke off while reading command beginning at line %d with\n%s'
+ raise (StopIteration,
+ 'Transcript broke off while reading command beginning at line %d with\n%s'
% (command[0]))
lineNum += 1
-
- command = ''.join(command)
-
+
+ command = ''.join(command)
+
# Send the command into the application and capture the resulting output
stop = self.cmdapp.onecmd_plus_hooks(command)
-
+
#TODO: should act on ``stop``
result = self.outputTrap.read()
-
+
# Read the expected result from transcript
if line.startswith(self.cmdapp.prompt):
message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\
- (fname, lineNum, command, result)
+ (fname, lineNum, command, result)
self.assert_(not(result.strip()), message)
continue
-
+
expected = []
-
+
while not line.startswith(self.cmdapp.prompt):
expected.append(line)
try:
line = transcript.next()
except StopIteration:
- finished = True
+ finished = True
break
lineNum += 1
-
+
expected = ''.join(expected)
-
+
# Compare actual result to expected
message = '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n'%\
- (fname, lineNum, command, expected, result)
+ (fname, lineNum, command, expected, result)
expected = self.expectationParser.transformString(expected)
-
+
# checking whitespace is a pain - let's skip it
expected = self.anyWhitespace.sub('', expected)
result = self.anyWhitespace.sub('', result)
@@ -114,3 +115,18 @@ class Cmd2TestCase(unittest.TestCase):
def tearDown(self):
if self.CmdApp:
self.outputTrap.tearDown()
+
+
+def runTranscriptTests(self, callargs):
+ # @FIXME
+ # Add docstring
+ class TestMyAppCase(Cmd2TestCase):
+ CmdApp = self.__class__
+ self.__class__.testfiles = callargs
+ sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
+ testcase = TestMyAppCase()
+ runner = unittest.TextTestRunner()
+ result = runner.run(testcase)
+ result.printErrors()
+
+