Пример #1
0
    def test__is_method_and_is_bound(self):
        def f():
            raise NotImplementedError()

        def f_with_self_arg(self):
            raise NotImplementedError()

        self.assertFalse(FunctionSignature(f_with_self_arg).is_bound_method())
        self.assertFalse(FunctionSignature(f).is_bound_method())
        self.assertFalse(
            FunctionSignature(f_with_self_arg)._args[0].has_default())

        class SomeClass(object):
            def f_without_self():
                raise NotImplementedError()

            def f(self):
                raise NotImplementedError()

            def f_with_args(self, a, b, c):
                raise NotImplementedError()

            def f_with_first_argument_not_self(bla):
                raise NotImplementedError()

        class SomeOldStyleClass:
            def f_without_self():
                raise NotImplementedError()

            def f(self):
                raise NotImplementedError()

            def f_with_args(self, a, b, c):
                raise NotImplementedError()

            def f_with_first_argument_not_self(bla):
                raise NotImplementedError()

        for cls in (SomeClass, SomeOldStyleClass):
            self.assertFalse(FunctionSignature(cls.f).is_bound_method())
            self.assertFalse(
                FunctionSignature(cls.f_with_args).is_bound_method())
            self.assertFalse(
                FunctionSignature(
                    cls.f_with_first_argument_not_self).is_bound_method())
            self.assertTrue(FunctionSignature(cls().f).is_bound_method())
            self.assertTrue(
                FunctionSignature(cls().f_with_args).is_bound_method())
            self.assertTrue(
                FunctionSignature(
                    cls().f_with_first_argument_not_self).is_bound_method())
Пример #2
0
    def _test_function_signature(self,
                                 func,
                                 expected_signature,
                                 has_varargs=False,
                                 has_varkwargs=False):
        sig = FunctionSignature(func)

        self.assertEqual(len(expected_signature), len(sig._args))
        self.assertEqual(len(expected_signature), sig.get_num_args())
        for expected_arg, arg in zip(expected_signature, sig._args):
            if isinstance(expected_arg, tuple):
                expected_arg = ExpectedArg(*expected_arg)
            self.assertEqual(expected_arg.name, arg.name)
            self.assertEqual(expected_arg.has_default, arg.has_default())
            if expected_arg.has_default:
                self.assertEqual(expected_arg.default, arg.default)
        self.assertEqual(sig.has_variable_kwargs(), has_varkwargs)
        self.assertEqual(sig.has_variable_args(), has_varargs)
Пример #3
0
    def _test_function_signature(self,
        func,
        expected_signature,
        has_varargs=False,
        has_varkwargs=False
        ):
        sig = FunctionSignature(func)

        self.assertEquals(len(expected_signature), len(sig._args))
        self.assertEquals(len(expected_signature), sig.get_num_args())
        for expected_arg, arg in zip(expected_signature, sig._args):
            if isinstance(expected_arg, tuple):
                expected_arg = ExpectedArg(*expected_arg)
            self.assertEquals(expected_arg.name,
                              arg.name)
            self.assertEquals(expected_arg.has_default,
                              arg.has_default())
            if expected_arg.has_default:
                self.assertEquals(expected_arg.default, arg.default)
        self.assertEquals(sig.has_variable_kwargs(), has_varkwargs)
        self.assertEquals(sig.has_variable_args(), has_varargs)
Пример #4
0
    def test__is_class_method(self):
        class New(object):
            @classmethod
            def class_method(cls):
                raise NotImplementedError()

            def regular_method(self):
                raise NotImplementedError()

        class Old(object):
            @classmethod
            def class_method(cls):
                raise NotImplementedError()

            def regular_method(self):
                raise NotImplementedError()

        self.assertTrue(FunctionSignature(New.class_method).is_class_method())
        self.assertTrue(FunctionSignature(Old.class_method).is_class_method())
        self.assertFalse(
            FunctionSignature(New.regular_method).is_class_method())
        self.assertFalse(
            FunctionSignature(Old.regular_method).is_class_method())
Пример #5
0
 def test__normalizing_kwargs_with_numbers(self):
     # this is supported in python, but interferes with the arg-normalizing logic
     sig = FunctionSignature(lambda *args, **kwargs: None)
     with self.assertRaises(InvalidKeywordArgument):
         sig.get_normalized_args((), {1: 2})
Пример #6
0
    def test__object_method_placeholders(self):
        class SomeObject(object):
            pass

        sig = FunctionSignature(SomeObject.__ge__)
Пример #7
0
 def test__binary_global_function(self):
     sig = FunctionSignature(time.time)
     self.assertEqual(sig._args, [])
     self.assertTrue(sig.has_variable_args())
     self.assertTrue(sig.has_variable_kwargs())
Пример #8
0
 def test__copy(self):
     f = FunctionSignature(lambda a, b: 2)
     f2 = f.copy()
     self.assertIsNot(f, f2)
     self.assertIsNot(f._args, f2._args)
Пример #9
0
 def test__normalizing_kwargs_with_numbers(self):
     # this is supported in python, but interferes with the arg-normalizing logic
     sig = FunctionSignature(lambda *args, **kwargs: None)
     with self.assertRaises(InvalidKeywordArgument):
         sig.get_normalized_args((), {1:2})
Пример #10
0
 def test__binary_global_function(self):
     sig = FunctionSignature(time.time)
     self.assertEquals(sig._args, [])
     self.assertTrue(sig.has_variable_args())
     self.assertTrue(sig.has_variable_kwargs())
Пример #11
0
 def test__copy(self):
     f = FunctionSignature(lambda a, b: 2)
     f2 = f.copy()
     self.assertIsNot(f, f2)
     self.assertIsNot(f._args, f2._args)