2014-10-30 19:45:01 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.utils import inspect
|
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class Person:
|
2014-10-30 19:45:01 +00: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
|
|
|
|
|
|
|
|
|
|
|
|
class TestInspectMethods(unittest.TestCase):
|
|
|
|
def test_get_func_full_args_no_arguments(self):
|
|
|
|
self.assertEqual(inspect.get_func_full_args(Person.no_arguments), [])
|
|
|
|
|
|
|
|
def test_get_func_full_args_one_argument(self):
|
|
|
|
self.assertEqual(inspect.get_func_full_args(Person.one_argument), [('something',)])
|
|
|
|
|
|
|
|
def test_get_func_full_args_all_arguments(self):
|
|
|
|
arguments = [('name',), ('address', 'home'), ('age', 25), ('*args',), ('**kwargs',)]
|
|
|
|
self.assertEqual(inspect.get_func_full_args(Person.all_kinds), arguments)
|
|
|
|
|
|
|
|
def test_func_accepts_var_args_has_var_args(self):
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(inspect.func_accepts_var_args(Person.just_args), True)
|
2014-10-30 19:45:01 +00:00
|
|
|
|
|
|
|
def test_func_accepts_var_args_no_var_args(self):
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(inspect.func_accepts_var_args(Person.one_argument), False)
|
2018-08-07 03:12:51 +00:00
|
|
|
|
|
|
|
def test_method_has_no_args(self):
|
|
|
|
self.assertIs(inspect.method_has_no_args(Person.no_arguments), True)
|
|
|
|
self.assertIs(inspect.method_has_no_args(Person.one_argument), False)
|
2018-08-07 03:12:51 +00:00
|
|
|
self.assertIs(inspect.method_has_no_args(Person().no_arguments), True)
|
|
|
|
self.assertIs(inspect.method_has_no_args(Person().one_argument), False)
|
2019-12-23 10:56:51 +00:00
|
|
|
|
|
|
|
def test_func_supports_parameter(self):
|
|
|
|
self.assertIs(inspect.func_supports_parameter(Person.all_kinds, 'address'), True)
|
|
|
|
self.assertIs(inspect.func_supports_parameter(Person.all_kinds, 'zone'), False)
|
|
|
|
|
|
|
|
def test_func_accepts_kwargs(self):
|
|
|
|
self.assertIs(inspect.func_accepts_kwargs(Person.just_args), False)
|
|
|
|
self.assertIs(inspect.func_accepts_kwargs(Person.all_kinds), True)
|