Revision: 7843
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7843&view=rev
Author:   jouni
Date:     2009-10-04 18:55:18 +0000 (Sun, 04 Oct 2009)

Log Message:
-----------
@image_comparison now makes a generator that yields a test case for each file 
format (currently png, pdf)

Modified Paths:
--------------
    trunk/matplotlib/lib/matplotlib/testing/decorators.py

Modified: trunk/matplotlib/lib/matplotlib/testing/decorators.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/testing/decorators.py       2009-10-03 
18:11:09 UTC (rev 7842)
+++ trunk/matplotlib/lib/matplotlib/testing/decorators.py       2009-10-04 
18:55:18 UTC (rev 7843)
@@ -42,66 +42,84 @@
     compare images generated by the test with those specified in
     *baseline_images*, which must correspond else an
     ImageComparisonFailure exception will be raised.
-
     """
 
     if baseline_images is None:
         raise ValueError('baseline_images must be specified')
+
+    # The multiple layers of defs are required because of how
+    # parameterized decorators work, and because we want to turn the
+    # single test_foo function to a generator that generates a
+    # separate test case for each file format.
     def compare_images_decorator(func):
-        def decorated_compare_images(*args,**kwargs):
+        baseline_dir, result_dir = _image_directories(func)
+        # Only run the test if there is a baseline image in the
+        # correct format for comparison.
+        extensions = [ext for ext in ['png', 'pdf']
+                      if os.path.exists(os.path.join(
+                          baseline_dir, baseline_images[0] + '.' + ext))]
 
-            # compute baseline image directory
-            module_name = func.__module__
-            if module_name=='__main__':
-                # FIXME: this won't work for nested packages in 
matplotlib.tests
-                import warnings
-                warnings.warn('test module run as script. guessing baseline 
image locations')
-                script_name = sys.argv[0]
-                basedir = os.path.abspath(os.path.dirname(script_name))
-                subdir = os.path.splitext(os.path.split(script_name)[1])[0]
-            else:
-                mods = module_name.split('.')
-                assert mods.pop(0)=='matplotlib'
-                assert mods.pop(0)=='tests'
-                subdir = os.path.join(*mods)
-                basedir = os.path.dirname(matplotlib.tests.__file__)
-            baseline_dir = os.path.join(basedir,'baseline_images',subdir)
-            result_dir = os.path.join(basedir,'current_images',subdir)
-            if not os.path.exists(result_dir):
-                try:
-                    # make the current_images directory first
-                    os.mkdir(os.path.join(basedir,'current_images'))
-                except OSError:
-                    pass # probably exists already
-                os.mkdir(result_dir)
+        def compare_images_generator():
+            for extension in extensions:
+                @knownfailureif(extension not in comparable_formats(),
+                                'Cannot compare %s files on this system' % 
extension)
+                def decorated_compare_images():
+                    # set the default format of savefig
+                    matplotlib.rc('savefig', extension=extension)
+                    # change to the result directory for the duration of the 
test
+                    old_dir = os.getcwd()
+                    os.chdir(result_dir)
+                    try:
+                        result = func() # actually call the test function
+                    finally:
+                        os.chdir(old_dir)
+                    for fname in baseline_images:
+                        actual = os.path.join(result_dir, fname) + '.' + 
extension
+                        expected = os.path.join(baseline_dir,fname) + '.' + 
extension
 
-            for extension in ['png', 'pdf']:
-                # set the default format of savefig
-                matplotlib.rc('savefig', extension=extension)
-                # change to the result directory for the duration of the test
-                old_dir = os.getcwd()
-                os.chdir(result_dir)
-                try:
-                    last_result = func(*args,**kwargs) # actually call the 
test function
-                finally:
-                    os.chdir(old_dir)
-                for fname in baseline_images:
-                    actual = os.path.join(result_dir, fname) + '.' + extension
-                    expected = os.path.join(baseline_dir,fname) + '.' + 
extension
-                    if (extension not in comparable_formats()
-                        or not os.path.exists(expected)):
-                        # FIXME: Should it be a known fail if this format
-                        # cannot be compared in this environment?
-                        continue
+                        # compare the images
+                        tol=1e-3 # default tolerance
+                        err = compare_images( expected, actual, tol,
+                                              in_decorator=True )
+                        if err:
+                            raise ImageComparisonFailure(
+                                'images not close: %(actual)s vs. %(expected)s 
'
+                                '(RMS %(rms).3f)'%err)
+                    return result
+                yield (decorated_compare_images,)
+        return nose.tools.make_decorator(func)(compare_images_generator)
+    return compare_images_decorator
 
-                    # compare the images
-                    tol=1e-3 # default tolerance
-                    err = compare_images( expected, actual, tol,
-                                          in_decorator=True )
-                    if err:
-                        raise ImageComparisonFailure(
-                            'images not close: %(actual)s vs. %(expected)s '
-                            '(RMS %(rms).3f)'%err)
-            return last_result
-        return nose.tools.make_decorator(func)(decorated_compare_images)
-    return compare_images_decorator
+def _image_directories(func):
+    """
+    Compute the baseline and result image directories for testing *func*.
+    Create the result directory if it doesn't exist.
+    """
+    module_name = func.__module__
+    if module_name=='__main__':
+        # FIXME: this won't work for nested packages in matplotlib.tests
+        import warnings
+        warnings.warn('test module run as script. guessing baseline image 
locations')
+        script_name = sys.argv[0]
+        basedir = os.path.abspath(os.path.dirname(script_name))
+        subdir = os.path.splitext(os.path.split(script_name)[1])[0]
+    else:
+        mods = module_name.split('.')
+        assert mods.pop(0)=='matplotlib'
+        assert mods.pop(0)=='tests'
+        subdir = os.path.join(*mods)
+        basedir = os.path.dirname(matplotlib.tests.__file__)
+
+    baseline_dir = os.path.join(basedir,'baseline_images',subdir)
+    result_dir = os.path.join(basedir,'current_images',subdir)
+
+    if not os.path.exists(result_dir):
+        try:
+            # make the current_images directory first
+            os.mkdir(os.path.join(basedir,'current_images'))
+        except OSError:
+            pass # probably exists already
+        os.mkdir(result_dir)
+
+    return baseline_dir, result_dir
+


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to