2014-10-31 03:45:01 +08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.utils import inspect
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Person:
|
2014-10-31 03:45:01 +08:00
|
|
|
def no_arguments(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def one_argument(self, something):
|
|
|
|
return something
|
|
|
|
|
|
|
|
def just_args(self, *args):
|
|
|
|
return args
|
|
|
|
|
|
|
|
def all_kinds(self, name, address="home", age=25, *args, **kwargs):
|
|
|
|
return kwargs
|
|
|
|
|
2021-03-22 17:08:35 +08:00
|
|
|
@classmethod
|
|
|
|
def cls_all_kinds(cls, name, address="home", age=25, *args, **kwargs):
|
|
|
|
return kwargs
|
|
|
|
|
2014-10-31 03:45:01 +08:00
|
|
|
|
|
|
|
class TestInspectMethods(unittest.TestCase):
|
2021-03-19 19:23:28 +08:00
|
|
|
def test_get_callable_parameters(self):
|
|
|
|
self.assertIs(
|
|
|
|
inspect._get_callable_parameters(Person.no_arguments),
|
|
|
|
inspect._get_callable_parameters(Person.no_arguments),
|
|
|
|
)
|
|
|
|
self.assertIs(
|
|
|
|
inspect._get_callable_parameters(Person().no_arguments),
|
|
|
|
inspect._get_callable_parameters(Person().no_arguments),
|
|
|
|
)
|
|
|
|
|
2014-10-31 03:45:01 +08:00
|
|
|
def test_get_func_full_args_no_arguments(self):
|
|
|
|
self.assertEqual(inspect.get_func_full_args(Person.no_arguments), [])
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertEqual(inspect.get_func_full_args(Person().no_arguments), [])
|
2014-10-31 03:45:01 +08:00
|
|
|
|
|
|
|
def test_get_func_full_args_one_argument(self):
|
|
|
|
self.assertEqual(
|
|
|
|
inspect.get_func_full_args(Person.one_argument), [("something",)]
|
|
|
|
)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertEqual(
|
|
|
|
inspect.get_func_full_args(Person().one_argument),
|
|
|
|
[("something",)],
|
|
|
|
)
|
2014-10-31 03:45:01 +08:00
|
|
|
|
2021-03-22 17:08:35 +08:00
|
|
|
def test_get_func_full_args_all_arguments_method(self):
|
2014-10-31 03:45:01 +08:00
|
|
|
arguments = [
|
|
|
|
("name",),
|
|
|
|
("address", "home"),
|
|
|
|
("age", 25),
|
|
|
|
("*args",),
|
|
|
|
("**kwargs",),
|
|
|
|
]
|
|
|
|
self.assertEqual(inspect.get_func_full_args(Person.all_kinds), arguments)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertEqual(inspect.get_func_full_args(Person().all_kinds), arguments)
|
|
|
|
|
|
|
|
def test_get_func_full_args_all_arguments_classmethod(self):
|
|
|
|
arguments = [
|
|
|
|
("name",),
|
|
|
|
("address", "home"),
|
|
|
|
("age", 25),
|
|
|
|
("*args",),
|
|
|
|
("**kwargs",),
|
|
|
|
]
|
|
|
|
self.assertEqual(inspect.get_func_full_args(Person.cls_all_kinds), arguments)
|
|
|
|
self.assertEqual(inspect.get_func_full_args(Person().cls_all_kinds), arguments)
|
2014-10-31 03:45:01 +08:00
|
|
|
|
|
|
|
def test_func_accepts_var_args_has_var_args(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(inspect.func_accepts_var_args(Person.just_args), True)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertIs(inspect.func_accepts_var_args(Person().just_args), True)
|
2014-10-31 03:45:01 +08:00
|
|
|
|
|
|
|
def test_func_accepts_var_args_no_var_args(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(inspect.func_accepts_var_args(Person.one_argument), False)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertIs(inspect.func_accepts_var_args(Person().one_argument), False)
|
2018-08-07 11:12:51 +08:00
|
|
|
|
|
|
|
def test_method_has_no_args(self):
|
|
|
|
self.assertIs(inspect.method_has_no_args(Person.no_arguments), True)
|
2018-08-07 11:12:51 +08:00
|
|
|
self.assertIs(inspect.method_has_no_args(Person().no_arguments), True)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertIs(inspect.method_has_no_args(Person.one_argument), False)
|
2018-08-07 11:12:51 +08:00
|
|
|
self.assertIs(inspect.method_has_no_args(Person().one_argument), False)
|
2019-12-23 18:56:51 +08:00
|
|
|
|
|
|
|
def test_func_supports_parameter(self):
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertIs(
|
2019-12-23 18:56:51 +08:00
|
|
|
inspect.func_supports_parameter(Person.all_kinds, "address"), True
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertIs(
|
|
|
|
inspect.func_supports_parameter(Person().all_kinds, "address"),
|
|
|
|
True,
|
|
|
|
)
|
2019-12-23 18:56:51 +08:00
|
|
|
self.assertIs(inspect.func_supports_parameter(Person.all_kinds, "zone"), False)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertIs(
|
|
|
|
inspect.func_supports_parameter(Person().all_kinds, "zone"),
|
|
|
|
False,
|
|
|
|
)
|
2019-12-23 18:56:51 +08:00
|
|
|
|
|
|
|
def test_func_accepts_kwargs(self):
|
|
|
|
self.assertIs(inspect.func_accepts_kwargs(Person.just_args), False)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False)
|
2019-12-23 18:56:51 +08:00
|
|
|
self.assertIs(inspect.func_accepts_kwargs(Person.all_kinds), True)
|
2021-03-22 17:08:35 +08:00
|
|
|
self.assertIs(inspect.func_accepts_kwargs(Person().just_args), False)
|