Предизвикателства > Abomination decorator > Решения > Решението на Вяра Тодорова

Резултати
1 точки от тестове
0 точки от учител

1 точки общо

1 успешни теста
3 неуспешни теста
Код

 1def type_check(input):
 2    def type_check_types(*types):
 3        def decorator(func):
 4            def decorated(*args, **kwargs):
 5                list_args = list(args) + list(kwargs)
 6                for arg in list_args:
 7                    if not isinstance(arg, types):
 8                        types_str_result = ", ".join(str(type) for type in types)
 9                        if input == "in":
10                            print("Invalid input arguments, expected {}!".format(types_str_result))
11                        else:
12                            print("Invalid output value, expected {}!".format(types_str_result))
13                        break
14                return func(*args, **kwargs)
15            return decorated
16        return decorator
17    return type_check_types

FF.F
======================================================================
FAIL: test_check_both (test.TestTypeCheck.test_check_both)
The decorator should report invalid "in" and "out" together.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.12/unittest/mock.py", line 1390, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 98, in test_check_both
mock_print.assert_has_calls(
File "/usr/lib/python3.12/unittest/mock.py", line 981, in assert_has_calls
raise AssertionError(
AssertionError: Calls not found.
Expected: [call("Invalid input arguments, expected <class 'float'>!"),
call("Invalid output value, expected <class 'int'>!")]
Actual: [call("Invalid output value, expected <class 'int'>!"),
call("Invalid input arguments, expected <class 'float'>!")]

======================================================================
FAIL: test_check_decorated_exception (test.TestTypeCheck.test_check_decorated_exception)
The decorator should not supress any exceptions raised.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.12/unittest/mock.py", line 1390, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 112, in test_check_decorated_exception
self.assert_in_permutations(mock_print.mock_calls[0], types, self.BASE_STRING_IN)
File "/tmp/test.py", line 35, in assert_in_permutations
self.assertIn(call, call_permutations)
AssertionError: call("Invalid output value, expected <class 'list'>, <class 'tuple'>!") not found in [call("Invalid input arguments, expected <class 'list'>, <class 'tuple'>!"), call("Invalid input arguments, expected <class 'tuple'>, <class 'list'>!")]

======================================================================
FAIL: test_check_out (test.TestTypeCheck.test_check_out)
The decorator should report an invalid "out" value.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.12/unittest/mock.py", line 1390, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 72, in test_check_out
mock_print.assert_has_calls([call(self.BASE_STRING_OUT.format("my type"))])
File "/usr/lib/python3.12/unittest/mock.py", line 981, in assert_has_calls
raise AssertionError(
AssertionError: Calls not found.
Expected: [call('Invalid output value, expected my type!')]

----------------------------------------------------------------------
Ran 4 tests in 0.003s

FAILED (failures=3)

Дискусия
История
Това решение има само една версия.