"""
Utilities for testing the modules and figures they generate.
"""
import os
import shutil
import pytest


class DummyFigure():  # pylint: disable=too-few-public-methods
    """
    Not really a figure. Mocks the ``savefig`` method from matplotlib figures.

    Pretends to be a matploblib figure so that we can use pytest-mpl to compare
    generated images.

    ``savefig`` renames the original figure file (``image``) to the given name
    and ignores all other arguments.
    """

    def __init__(self, image):
        self.image = image

    # pylint: disable=unused-argument
    def savefig(self, fname, *args, **kwargs):
        """
        Rename ``image`` to ``fname``. Ignores all other arguments.
        """
        shutil.move(self.image, fname)


def figure_comparison_test(test_func, **kwargs):
    """
    Make a test function compare the generated figure against a baseline.

    Use as a decorator::

        @figure_comparison_test
        def test_some_plot(prefix, fmt):
            "Tests some plotting command"
            begin(prefix=prefix, fmt=fmt)
            ...
            end()

    The test function must receive the ``prefix`` and ``fmt`` arguments and
    pass them along to ``gmt.begin`` or ``gmt.figure``.

    Uses ``pytest-mpl`` for the image comparison. You can pass any argument
    that ``pytest.mark.mpl_image_compare`` accepts to this decorator as well.

    Generate baseline images for a test by running::

        py.test --mpl-generate-path=baseline path/to/test_file.py

    Images will be in a ``baseline`` folder in the current directory.

    When you're satisfied with the baselines, copy them to the ``baseline``
    folder of the test folder.
    """

    if 'filename' not in kwargs:
        kwargs['filename'] = test_func.__name__ + '.png'

    @pytest.mark.mpl_image_compare(**kwargs)
    def new_test():
        "Test function with image comparison"
        prefix = test_func.__name__
        fmt = 'png'
        fname = '{}.{}'.format(prefix, fmt)
        test_func(prefix, fmt)
        assert os.path.exists(fname)
        # Return a dummy figure to fool pytest-mpl into thinking this is
        # maptlotlib.
        return DummyFigure(image=fname)

    return new_test

@@ -93,21 +93,65 @@
 %0Aimport 
-pytes
+functools%0A%0Aimport pytest%0A%0Afrom .. import psconver
 t%0A%0A%0Aclas
@@ -420,60 +420,58 @@
 g%60%60 
-renames the original figure file (%60%60image%60%60)
+calls %60%60psconvert%60%60 and moves the function
  to the 
 give
@@ -470,13 +470,12 @@
 the 
-given
+file
  nam
@@ -484,39 +484,13 @@
     
-and ignores all other arguments
+given
 .%0A  
@@ -524,16 +524,23 @@
 f, image
+_prefix
 ):%0A     
@@ -556,16 +556,23 @@
 mage
+_prefix
  = image
 %0A%0A  
@@ -567,16 +567,76 @@
  = image
+_prefix%0A        self.image_name = self.image_prefix + '.png'
 %0A%0A    # 
@@ -798,32 +798,141 @@
 ts.%0A        %22%22%22%0A
+        psconvert(F=self.image_prefix, T='g', A=True, P=True)%0A        assert os.path.exists(self.image_name)%0A
         shutil.m
@@ -945,16 +945,21 @@
 lf.image
+_name
 , fname)
@@ -997,26 +997,16 @@
 est_func
-, **kwargs
 ):%0A    %22
@@ -1168,27 +1168,16 @@
 me_plot(
-prefix, fmt
 ):%0A     
@@ -1229,358 +1229,109 @@
     
-begin(prefix=prefix, fmt=fmt)%0A            ...%0A            end()%0A%0A    The test function must receive the %60%60prefix%60%60 and %60%60fmt%60%60 arguments and%0A    pass them along to %60%60gmt.begin%60%60 or %60%60gmt.figure%60%60.%0A%0A    Uses %60%60pytest-mpl%60%60 for the image comparison. You can pass any argument%0A    that %60%60pytest.mark.mpl_image_compare%60%60 accepts to this decorator as well
+figure()%0A            psbasemap(...)%0A            ...%0A%0A    Uses %60%60pytest-mpl%60%60 for the image comparison
 .%0A%0A 
@@ -1640,66 +1640,68 @@
 %22%22%22%0A
-%0A
     
-if 'filename' not in kwargs:%0A        kwargs%5B'
+prefix = test_func.__name__%0A    fmt = 'png'%0A    
 filename
 '%5D =
@@ -1700,40 +1700,38 @@
 name
-'%5D
  = 
-test_func.__name__ + '.png'
+'%7B%7D.%7B%7D'.format(prefix, fmt)
 %0A%0A  
@@ -1763,24 +1763,65 @@
 compare(
-**kwargs
+filename=filename)%0A    @functools.wraps(test_func
 )%0A    de
@@ -1892,166 +1892,18 @@
     
-prefix = test_func.__name__%0A        fmt = 'png'%0A        fname = '%7B%7D.%7B%7D'.format(prefix, fmt)%0A        test_func(prefix, fmt)%0A        assert os.path.exists(fname
+test_func(
 )%0A  
@@ -2031,14 +2031,22 @@
 mage
-=fname
+_prefix=prefix
 )%0A%0A 

Use psconvert to mock figure for pytest-mpl