Пример #1
0
    def test_monkeypatch_callable_non_callable(self):
        tester = self

        class MyClass:
            pass

        @monkeypatch(MyClass, methodname='prop1')
        @property
        def meth1(self):
            return 12

        class XXX(object):
            def __call__(self, other):
                tester.assertTrue(isinstance(other, MyClass))
                return 12

        try:
            monkeypatch(MyClass)(XXX())
        except AttributeError as err:
            self.assertTrue(
                str(err).endswith(
                    'has no __name__ attribute: you should provide an explicit `methodname`'
                ))
        monkeypatch(MyClass, 'foo')(XXX())
        self.assertTrue(isinstance(MyClass.prop1, property))
        self.assertTrue(isinstance(MyClass.foo, collections.Callable))
        self.assertEqual(MyClass().prop1, 12)
        self.assertEqual(MyClass().foo(), 12)
Пример #2
0
 def test_monkeypatch_arbitrary_callable(self):
     class MyClass: pass
     class ArbitraryCallable(object):
         def __call__(self):
             return 12
     # ensure it complains about missing __name__
     with self.assertRaises(AttributeError) as cm:
         monkeypatch(MyClass)(ArbitraryCallable())
     self.assertTrue(str(cm.exception).endswith('has no __name__ attribute: you should provide an explicit `methodname`'))
     # ensure no black magic under the hood
     monkeypatch(MyClass, 'foo')(ArbitraryCallable())
     self.assertTrue(callable(MyClass.foo))
     self.assertEqual(MyClass().foo(), 12)
 def test_monkeypatch_callable_non_callable(self):
     tester = self
     class MyClass: pass
     @monkeypatch(MyClass, methodname='prop1')
     @property
     def meth1(self):
         return 12
     class XXX(object):
         def __call__(self, other):
             tester.assertTrue(isinstance(other, MyClass))
             return 12
     try:
         monkeypatch(MyClass)(XXX())
     except AttributeError, err:
         self.assertTrue(str(err).endswith('has no __name__ attribute: you should provide an explicit `methodname`'))
    def test_monkeypatch_arbitrary_callable(self):
        class MyClass:
            pass

        class ArbitraryCallable(object):
            def __call__(self):
                return 12

        # ensure it complains about missing __name__
        with self.assertRaises(AttributeError) as cm:
            monkeypatch(MyClass)(ArbitraryCallable())
        self.assertTrue(
            str(cm.exception).endswith(
                'has no __name__ attribute: you should provide an explicit `methodname`'
            ))
        # ensure no black magic under the hood
        monkeypatch(MyClass, 'foo')(ArbitraryCallable())
        self.assertTrue(callable(MyClass.foo))
        self.assertEqual(MyClass().foo(), 12)
Пример #5
0
class DecoratorsTC(TestCase):
    def test_monkeypatch_instance_method(self):
        class MyClass:
            pass

        @monkeypatch(MyClass)
        def meth1(self):
            return 12

        class XXX(object):
            @monkeypatch(MyClass)
            def meth2(self):
                return 12

        self.assertTrue(isinstance(MyClass.meth1, types.MethodType))
        self.assertTrue(isinstance(MyClass.meth2, types.MethodType))

    def test_monkeypatch_callable_non_callable(self):
        tester = self

        class MyClass:
            pass

        @monkeypatch(MyClass, methodname='prop1')
        @property
        def meth1(self):
            return 12

        class XXX(object):
            def __call__(self, other):
                tester.assertTrue(isinstance(other, MyClass))
                return 12

        try:
            monkeypatch(MyClass)(XXX())
        except AttributeError, err:
            self.assertTrue(
                str(err).endswith(
                    'has no __name__ attribute: you should provide an explicit `methodname`'
                ))
        monkeypatch(MyClass, 'foo')(XXX())
        self.assertTrue(isinstance(MyClass.prop1, property))
        self.assertTrue(callable(MyClass.foo))
        self.assertEqual(MyClass().prop1, 12)
        self.assertEqual(MyClass().foo(), 12)