Repository: matplotlib/matplotlib
File: lib/matplotlib/tests/test_scale.py (Python)

Code before:
        assert isinstance(ax5.fields['major_formatter'],
                          LogFormatterSciNotation)

    def test_bad_scale(self):
        fig, ax = plt.subplots()

        with pytest.raises(ValueError):
            AsinhScale(axis=None, linear_width=0)
        with pytest.raises(ValueError):
            AsinhScale(axis=None, linear_width=-1)
        s0 = AsinhScale(axis=None, )
        s1 = AsinhScale(axis=None, linear_width=3.0)


def test_custom_scale_without_axis():
    """
    Test that one can register and use custom scales that don't take an *axis* param.
    """
    class CustomTransform(IdentityTransform):
        pass

    class CustomScale(mscale.ScaleBase):
        name = "custom"

        def __init__(self):
            self._transform = CustomTransform()

        def get_transform(self):
            return self._transform

        def set_default_locators_and_formatters(self, axis):
            axis.set_major_locator(AutoLocator())
            axis.set_major_formatter(ScalarFormatter())
            axis.set_minor_locator(NullLocator())
            axis.set_minor_formatter(NullFormatter())

    try:
        mscale.register_scale(CustomScale)
        fig, ax = plt.subplots()
        ax.set_xscale('custom')
        assert isinstance(ax.xaxis.get_transform(), CustomTransform)
    finally:
        # cleanup - there's no public unregister_scale()
        del mscale._scale_mapping["custom"]
        del mscale._scale_has_axis_parameter["custom"]


def test_custom_scale_with_axis():
    """
    Test that one can still register and use custom scales with an *axis*
    parameter, but that registering issues a pending-deprecation warning.

Review comment (suggestion):
```suggestion
        # Important: __init__ has no "scale" parameter
        def __init__(self):
```

Code after:
        assert isinstance(ax5.fields['major_formatter'],
                          LogFormatterSciNotation)

    def test_bad_scale(self):
        fig, ax = plt.subplots()

        with pytest.raises(ValueError):
            AsinhScale(axis=None, linear_width=0)
        with pytest.raises(ValueError):
            AsinhScale(axis=None, linear_width=-1)
        s0 = AsinhScale(axis=None, )
        s1 = AsinhScale(axis=None, linear_width=3.0)


def test_custom_scale_without_axis():
    """
    Test that one can register and use custom scales that don't take an *axis* param.
    """
    class CustomTransform(IdentityTransform):
        pass

    class CustomScale(mscale.ScaleBase):
        name = "custom"

        # Important: __init__ has no *axis* parameter
        def __init__(self):
            self._transform = CustomTransform()

        def get_transform(self):
            return self._transform

        def set_default_locators_and_formatters(self, axis):
            axis.set_major_locator(AutoLocator())
            axis.set_major_formatter(ScalarFormatter())
            axis.set_minor_locator(NullLocator())
            axis.set_minor_formatter(NullFormatter())

    try:
        mscale.register_scale(CustomScale)
        fig, ax = plt.subplots()
        ax.set_xscale('custom')
        assert isinstance(ax.xaxis.get_transform(), CustomTransform)
    finally:
        # cleanup - there's no public unregister_scale()
        del mscale._scale_mapping["custom"]
        del mscale._scale_has_axis_parameter["custom"]


def test_custom_scale_with_axis():
    """
    Test that one can still register and use custom scales with an *axis*