changeset 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
files cmd2.py example/example.py example/exampleSession.test example/exampleSession.txt
diffstat 4 files changed, 39 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Tue Mar 31 17:47:04 2009 -0400
+++ b/cmd2.py	Wed Apr 01 18:05:51 2009 -0400
@@ -21,9 +21,6 @@
 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com
 
 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2
-CHANGES:
-As of 0.3.0, options should be specified as `optparse` options.  See README.txt.
-flagReader.py options are still supported for backward compatibility
 """
 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest
 import unittest, string, datetime, urllib, glob
@@ -1115,18 +1112,17 @@
     transcriptFileName = ''
     transcriptExtension = ''
     def fetchTranscripts(self):
-        self.transcripts = {}
-        if self.transcriptExtension:
-            for fname in glob.glob('*.' + self.transcriptExtension):
-                tfile = open(fname)
-                self.transcripts[fname] = iter(tfile.readlines())
-                tfile.close()
+        self.transcripts = []
+        if self.CmdApp.testfile:
+            transcriptfilenames = [self.CmdApp.testfile]
+        elif self.transcriptExtension:
+            transcriptfilenames = glob.glob('*.' + self.transcriptExtension)
         elif self.transcriptFileName:
-            tfile = open(os.path.expanduser(self.transcriptFileName))
-            self.transcripts = {'transcript': iter(tfile.readlines())}
+            transcriptfilenames = [self.transcriptFileName]
+        for fname in transcriptfilenames:
+            tfile = open(fname)
+            self.transcripts.append(iter(tfile.readlines()))
             tfile.close()
-        else:
-            raise AttributeError, 'Cmd2TestCase must define transcriptFileName or transcriptExtension'
             
     def setUp(self):
         if self.CmdApp:
@@ -1143,18 +1139,10 @@
             self.assert_(re.match(matchme, linegot.strip()), message)
     def testall(self):
         if self.CmdApp:
-            setup = 'setup.' + self.transcriptExtension
-            teardown = 'teardown.' + self.transcriptExtension
-            transcripts = self.transcripts.copy()
-            transcriptfiles = []
-            if setup in transcripts:
-                transcriptfiles.append((setup, transcripts.pop(setup)))
-            transcriptfiles.extend(sorted((fname, transcripts[fname]) for fname in transcripts if fname != teardown))
-            if teardown in transcripts:
-                transcriptfiles.append((teardown, transcripts[teardown]))
-            for transcript in transcriptfiles:
-                self.testone(transcript)        
-    def testone(self, transcript):
+            self.fetchTranscripts()
+            for transcript in self.transcripts:
+                self._test_transcript(transcript)
+    def _test_transcript(self, transcript):
         lineNum = 0
         try:
             line = transcript.next()
@@ -1190,5 +1178,4 @@
             self.outputTrap.tearDown()
         
 if __name__ == '__main__':
-    doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE)
-    #c = Cmd()
+    doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE)
\ No newline at end of file
--- a/example/example.py	Tue Mar 31 17:47:04 2009 -0400
+++ b/example/example.py	Wed Apr 01 18:05:51 2009 -0400
@@ -32,14 +32,18 @@
 
 class TestMyAppCase(Cmd2TestCase):
     CmdApp = CmdLineApp
-    transcriptFileName = 'exampleSession.txt'
+    transcriptExtension = 'test'
 
 parser = optparse.OptionParser()
-parser.add_option('-t', '--test', dest='unittests', action='store_true', default=False, help='Run unit test suite')
+parser.add_option('-a', '--alltests', dest='test', action="store_true", 
+                  help='Run all transcript tests')
+parser.add_option('-t', '--testfile', dest='testfile', metavar='FILE',
+                  help='Run a single transcript from file FILE')
 (callopts, callargs) = parser.parse_args()
-if callopts.unittests:
+if callopts.testfile or callopts.test:
+    CmdLineApp.testfile = callopts.testfile
     sys.argv = [sys.argv[0]]  # the --test argument upsets unittest.main()
     unittest.main()
 else:
     app = CmdLineApp()
-    app.cmdloop()
+    app.cmdloop()
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/exampleSession.test	Wed Apr 01 18:05:51 2009 -0400
@@ -0,0 +1,17 @@
+(Cmd) say put this in a file > myfile.txt
+(Cmd) say < myfile.txt
+put this in a file
+(Cmd) # comments do nothing
+(Cmd) /* C-style comments */ say are invisible
+are invisible
+(Cmd) orate and they do not /* interfere;
+> with multiline */ commands;
+and they do not  commands
+(Cmd) speak the /* hidden comment */ text
+the  text
+(Cmd) speak the "unhidden quote" text
+the "unhidden quote" text
+(Cmd) # Quoted strings immediately following the command are NOT included in the arguments - 
+(Cmd) # this is due to a bug in pyparsing.  See:
+(Cmd) # https://sourceforge.net/tracker/index.php?func=detail&aid=2412233&group_id=97203&atid=617311
+(Cmd) # Test suite can't test changing prompts.
\ No newline at end of file
--- a/example/exampleSession.txt	Tue Mar 31 17:47:04 2009 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-(Cmd) say put this in a file > myfile.txt
-(Cmd) say < myfile.txt
-put this in a file
-(Cmd) # comments do nothing
-(Cmd) /* C-style comments */ say are invisible
-are invisible
-(Cmd) orate and they do not /* interfere;
-> with multiline */ commands;
-and they do not  commands
-(Cmd) speak the /* hidden comment */ text
-the  text
-(Cmd) speak the "unhidden quote" text
-the "unhidden quote" text
-(Cmd) # Quoted strings immediately following the command are NOT included in the arguments - 
-(Cmd) # this is due to a bug in pyparsing.  See:
-(Cmd) # https://sourceforge.net/tracker/index.php?func=detail&aid=2412233&group_id=97203&atid=617311
-(Cmd) # Test suite can't test changing prompts.
\ No newline at end of file