예제 #1
0
def test_export_method_args_call():

    # Check that *args/**kwargs are handled correctly for methods;
    # should perhaps be included above to avoid repeated calls
    # to export_method?
    #
    c = C()
    meth = utils.export_method(c.margs)
    assert meth(3, 7, "a", "b") == 23

    meth = utils.export_method(c.bargs)
    assert meth(3, 7, 14, 15, foo=None) == 25
예제 #2
0
def test_export_method_non_method():

    # Non-method argument
    def f(x):
        return 2 * x

    assert utils.export_method(f) is f
예제 #3
0
    def test_export_method(self):
        class C(object):
            def m(self, x, y=2):
                'Instance method m()'
                return x * y

            @classmethod
            def cm(klass, x, y=2):
                'Class method cm()'
                return x * y

            @staticmethod
            def sm(x, y=2):
                'Static method sm()'
                return x * y

        c = C()

        # Basic usage
        for meth in (c.m, c.cm, c.sm):
            m = utils.export_method(meth)
            self.assertEqual(m.__name__, meth.__name__)
            self.assertTrue(m.__doc__ is not None)
            self.assertEqual(m.__doc__, meth.__doc__)
            self.assertEqual(m(3), 6)
            self.assertEqual(m(3, 7), 21)
            e = None
            try:
                m()
            except TypeError, e:
                pass
            self.assertEqual(str(e),
                             '%s() takes at least 1 argument (0 given)' %
                             meth.__name__)
예제 #4
0
def test_export_method_basic():

    c = C()

    # Basic usage. The error message depends on
    #  a) Python version
    #  b) what method is being wrapped
    # (before Python 3.10 it didn't).
    #
    for meth in (c.m, c.margs, c.kwargs, c.bargs, c.cm, c.sm):
        m = utils.export_method(meth)

        assert m.__name__ == meth.__name__
        assert m.__doc__ is not None
        assert m.__doc__ == meth.__doc__

        assert m(3) == 6
        assert m(3, 7) == 21

        with pytest.raises(TypeError) as exc:
            m()

        emsg = f"{meth.__name__}() " + \
            "missing 1 required positional argument: 'x'"

        if meth.__name__ == 'sm':
            # In Python 3.10 we see C.sm rather than sm
            # so we search for both. We could be more explicit
            # and check on the Python version (e.g. for >= 3.10)
            # but it doesn't seem worth it. It's interesting it's
            # only for the static method.
            #
            assert str(exc.value) in [emsg, 'C.' + emsg]
        else:
            assert str(exc.value) == emsg
예제 #5
0
    def test_export_method(self):
        class C(object):
            def m(self, x, y=2):
                'Instance method m()'
                return x * y

            @classmethod
            def cm(klass, x, y=2):
                'Class method cm()'
                return x * y

            @staticmethod
            def sm(x, y=2):
                'Static method sm()'
                return x * y

        c = C()

        # Basic usage
        for meth in (c.m, c.cm, c.sm):
            m = utils.export_method(meth)
            self.assertEqual(m.__name__, meth.__name__)
            self.assertTrue(m.__doc__ is not None)
            self.assertEqual(m.__doc__, meth.__doc__)
            self.assertEqual(m(3), 6)
            self.assertEqual(m(3, 7), 21)
            e = None
            try:
                m()
            except TypeError, e:
                pass
            self.assertEqual(
                str(e),
                '%s() takes at least 1 argument (0 given)' % meth.__name__)
예제 #6
0
def test_export_method_names():

    # Name and module name
    c = C()
    m = utils.export_method(c.m, 'foo', 'bar')
    assert m.__name__ == 'foo'
    assert m.__module__ == 'bar'
    assert m(3) == 6
    assert m(3, 7) == 21
예제 #7
0
def test_export_method_args_errors():

    c = C()
    meth = utils.export_method(c.margs)

    with pytest.raises(TypeError) as exc:
        meth(12, dummy=None)

    emsg = "margs() got an unexpected keyword argument 'dummy'"
    assert str(exc.value) == emsg

    meth = utils.export_method(c.kwargs)
    assert meth(3, 7, foo="a", bar="b" == 25)

    with pytest.raises(TypeError) as exc:
        meth(12, 14, 15)

    emsg = "kwargs() takes from 1 to 2 positional arguments " + \
        "but 3 were given"
    assert str(exc.value) in emsg
예제 #8
0
파일: test_utils.py 프로젝트: mirca/sherpa
    def test_export_method(self):
        class C(object):
            def m(self, x, y=2):
                'Instance method m()'
                return x * y

            def margs(self, x, y=2, *args):
                'Instance method margs() with *args'
                return x * y + len(args)

            def kwargs(self, x, y=2, **kwargs):
                'Instance method kwargs() with **kwargs'
                return x * y + 2 * len(kwargs)

            def bargs(self, x, y=2, *args, **kwargs):
                'Instance method bargs() with *args and **kwargs'
                return x * y + len(args) + 2 * len(kwargs)

            @classmethod
            def cm(klass, x, y=2):
                'Class method cm()'
                return x * y

            @staticmethod
            def sm(x, y=2):
                'Static method sm()'
                return x * y

        c = C()

        # Basic usage
        for meth in (c.m, c.margs, c.kwargs, c.bargs, c.cm, c.sm):
            m = utils.export_method(meth)
            self.assertEqual(m.__name__, meth.__name__)
            self.assertTrue(m.__doc__ is not None)
            self.assertEqual(m.__doc__, meth.__doc__)
            self.assertEqual(m(3), 6)
            self.assertEqual(m(3, 7), 21)
            e = None
            try:
                m()
            except TypeError as e:
                emsg2 = "{}() ".format(meth.__name__) + \
                        "takes at least 1 argument (0 given)"
                emsg3 = "{}() ".format(meth.__name__) + \
                        "missing 1 required positional argument: 'x'"
                self.assertIn(str(e), [emsg2, emsg3])

        # Check that *args/**kwargs are handled correctly for methods;
        # should perhaps be included above to avoid repeated calls
        # to export_method?
        #
        meth = utils.export_method(c.margs)
        self.assertTrue(meth(3, 7, "a", "b"), 23)
        try:
            meth(12, dummy=None)
        except TypeError as e:
            emsg = "margs() got an unexpected keyword argument 'dummy'"
            self.assertEqual(str(e), emsg)

        meth = utils.export_method(c.kwargs)
        self.assertTrue(meth(3, 7, foo="a", bar="b"), 25)
        try:
            meth(12, 14, 15)
        except TypeError as e:
            emsg2 = "kwargs() takes at most 2 arguments (3 given)"
            emsg3 = "kwargs() takes from 1 to 2 positional arguments " + \
                    "but 3 were given"
            self.assertIn(str(e), [emsg2, emsg3])

        meth = utils.export_method(c.bargs)
        self.assertTrue(meth(3, 7, 14, 15, foo=None), 25)

        # Non-method argument
        def f(x):
            return 2 * x
        self.assertTrue(utils.export_method(f) is f)

        # Name and module name
        m = utils.export_method(c.m, 'foo', 'bar')
        self.assertEqual(m.__name__, 'foo')
        self.assertEqual(m.__module__, 'bar')
        self.assertEqual(m(3), 6)
        self.assertEqual(m(3, 7), 21)
예제 #9
0
    def test_export_method(self):
        class C():
            def m(self, x, y=2):
                'Instance method m()'
                return x * y

            def margs(self, x, y=2, *args):
                'Instance method margs() with *args'
                return x * y + len(args)

            def kwargs(self, x, y=2, **kwargs):
                'Instance method kwargs() with **kwargs'
                return x * y + 2 * len(kwargs)

            def bargs(self, x, y=2, *args, **kwargs):
                'Instance method bargs() with *args and **kwargs'
                return x * y + len(args) + 2 * len(kwargs)

            @classmethod
            def cm(klass, x, y=2):
                'Class method cm()'
                return x * y

            @staticmethod
            def sm(x, y=2):
                'Static method sm()'
                return x * y

        c = C()

        # Basic usage
        for meth in (c.m, c.margs, c.kwargs, c.bargs, c.cm, c.sm):
            m = utils.export_method(meth)
            self.assertEqual(m.__name__, meth.__name__)
            self.assertTrue(m.__doc__ is not None)
            self.assertEqual(m.__doc__, meth.__doc__)
            self.assertEqual(m(3), 6)
            self.assertEqual(m(3, 7), 21)
            e = None
            try:
                m()
            except TypeError as e:
                emsg2 = "{}() ".format(meth.__name__) + \
                        "takes at least 1 argument (0 given)"
                emsg3 = "{}() ".format(meth.__name__) + \
                        "missing 1 required positional argument: 'x'"
                self.assertIn(str(e), [emsg2, emsg3])

        # Check that *args/**kwargs are handled correctly for methods;
        # should perhaps be included above to avoid repeated calls
        # to export_method?
        #
        meth = utils.export_method(c.margs)
        self.assertTrue(meth(3, 7, "a", "b"), 23)
        try:
            meth(12, dummy=None)
        except TypeError as e:
            emsg = "margs() got an unexpected keyword argument 'dummy'"
            self.assertEqual(str(e), emsg)

        meth = utils.export_method(c.kwargs)
        self.assertTrue(meth(3, 7, foo="a", bar="b"), 25)
        try:
            meth(12, 14, 15)
        except TypeError as e:
            emsg2 = "kwargs() takes at most 2 arguments (3 given)"
            emsg3 = "kwargs() takes from 1 to 2 positional arguments " + \
                    "but 3 were given"
            self.assertIn(str(e), [emsg2, emsg3])

        meth = utils.export_method(c.bargs)
        self.assertTrue(meth(3, 7, 14, 15, foo=None), 25)

        # Non-method argument
        def f(x):
            return 2 * x
        self.assertTrue(utils.export_method(f) is f)

        # Name and module name
        m = utils.export_method(c.m, 'foo', 'bar')
        self.assertEqual(m.__name__, 'foo')
        self.assertEqual(m.__module__, 'bar')
        self.assertEqual(m(3), 6)
        self.assertEqual(m(3, 7), 21)
예제 #10
0
class test_utils(SherpaTestCase):
    def setUp(self):
        def f1(a, b, c):
            pass

        self.f1 = f1

        def f2(a, b=1, c=2, d=3, e=4):
            pass

        self.f2 = f2

    def test_NoNewAttributesAfterInit(self):
        class C(NoNewAttributesAfterInit):
            def __init__(self):
                self.x = 1
                self.y = 2
                self.z = 3
                del self.z
                NoNewAttributesAfterInit.__init__(self)

        c = C()
        self.assertEqual(c.x, 1)
        self.assertEqual(c.y, 2)
        self.assert_(not hasattr(c, 'z'))
        self.assertRaises(AttributeError, delattr, c, 'x')
        self.assertRaises(AttributeError, delattr, c, 'z')
        self.assertRaises(AttributeError, setattr, c, 'z', 5)
        c.x = 4
        self.assertEqual(c.x, 4)

    def test_export_method(self):
        class C(object):
            def m(self, x, y=2):
                'Instance method m()'
                return x * y

            @classmethod
            def cm(klass, x, y=2):
                'Class method cm()'
                return x * y

            @staticmethod
            def sm(x, y=2):
                'Static method sm()'
                return x * y

        c = C()

        # Basic usage
        for meth in (c.m, c.cm, c.sm):
            m = utils.export_method(meth)
            self.assertEqual(m.__name__, meth.__name__)
            self.assertTrue(m.__doc__ is not None)
            self.assertEqual(m.__doc__, meth.__doc__)
            self.assertEqual(m(3), 6)
            self.assertEqual(m(3, 7), 21)
            e = None
            try:
                m()
            except TypeError, e:
                pass
            self.assertEqual(
                str(e),
                '%s() takes at least 1 argument (0 given)' % meth.__name__)

        # Non-method argument
        def f(x):
            return 2 * x

        self.assertTrue(utils.export_method(f) is f)

        # Name and module name
        m = utils.export_method(c.m, 'foo', 'bar')
        self.assertEqual(m.__name__, 'foo')
        self.assertEqual(m.__module__, 'bar')
        self.assertEqual(m(3), 6)
        self.assertEqual(m(3, 7), 21)
예제 #11
0
def test_export_method():
    class C():

        def m(self, x, y=2):
            'Instance method m()'
            return x * y

        def margs(self, x, y=2, *args):
            'Instance method margs() with *args'
            return x * y + len(args)

        def kwargs(self, x, y=2, **kwargs):
            'Instance method kwargs() with **kwargs'
            return x * y + 2 * len(kwargs)

        def bargs(self, x, y=2, *args, **kwargs):
            'Instance method bargs() with *args and **kwargs'
            return x * y + len(args) + 2 * len(kwargs)

        @classmethod
        def cm(klass, x, y=2):
            'Class method cm()'
            return x * y

        @staticmethod
        def sm(x, y=2):
            'Static method sm()'
            return x * y

    c = C()

    # Basic usage
    for meth in (c.m, c.margs, c.kwargs, c.bargs, c.cm, c.sm):
        m = utils.export_method(meth)

        assert m.__name__ == meth.__name__
        assert m.__doc__ is not None
        assert m.__doc__ == meth.__doc__

        assert m(3) == 6
        assert m(3, 7) == 21

        with pytest.raises(TypeError) as exc:
            m()

        emsg = "{}() ".format(meth.__name__) + \
            "missing 1 required positional argument: 'x'"
        assert str(exc.value) == emsg

    # Check that *args/**kwargs are handled correctly for methods;
    # should perhaps be included above to avoid repeated calls
    # to export_method?
    #
    meth = utils.export_method(c.margs)
    assert meth(3, 7, "a", "b") == 23

    with pytest.raises(TypeError) as exc:
        meth(12, dummy=None)

    emsg = "margs() got an unexpected keyword argument 'dummy'"
    assert str(exc.value) == emsg

    meth = utils.export_method(c.kwargs)
    assert meth(3, 7, foo="a", bar="b" == 25)

    with pytest.raises(TypeError) as exc:
        meth(12, 14, 15)

    emsg = "kwargs() takes from 1 to 2 positional arguments " + \
        "but 3 were given"
    assert str(exc.value) in emsg

    meth = utils.export_method(c.bargs)
    assert meth(3, 7, 14, 15, foo=None) == 25

    # Non-method argument
    def f(x):
        return 2 * x

    assert utils.export_method(f) is f

    # Name and module name
    m = utils.export_method(c.m, 'foo', 'bar')
    assert m.__name__ == 'foo'
    assert m.__module__ == 'bar'
    assert m(3) == 6
    assert m(3, 7) == 21