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

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

1 точки общо

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

 1def create_a_string_to_print(*types):
 2    string_to_print = ""
 3    index = 0
 4    for current_type in types:
 5        string_to_print += str(current_type)
 6        index += 1
 7        if index <= len(types) - 1:
 8            string_to_print += (", ")
 9        index += 1
10    return string_to_print
11def if_not_in_allowed_types_then_print(value, *allowed_types):
12    if type(value) not in allowed_types:
13        string_of_allowed_types = create_a_string_to_print(*allowed_types)
14        print(f"Invalid input arguments, expected {string_of_allowed_types}!")
15def type_check(in_or_out):
16    def types_given(*allowed_types):
17        def check(func):
18            def check_with(*args, **kwargs):
19                if in_or_out == "in":
20                    list_of_all_args = list(args) + list(kwargs.values())
21                    for current_value in list_of_all_args:
22                        if_not_in_allowed_types_then_print(current_value, *allowed_types)
23                decorated_func = func(*args, **kwargs)
24                if in_or_out == "out":
25                    if_not_in_allowed_types_then_print(decorated_func, *allowed_types)
26                return decorated_func
27            return check_with
28        return check
29    return types_given

F.FF
======================================================================
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 input arguments, expected <class 'float'>!"),
call("Invalid input arguments, expected <class 'float'>!"),
call("Invalid input arguments, expected <class 'float'>!"),
call("Invalid input arguments, expected <class 'int'>!")]

======================================================================
FAIL: test_check_in (test.TestTypeCheck.test_check_in)
The decorator should report invalid "in" arguments.
----------------------------------------------------------------------
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 43, in test_check_in
self.assertEqual(mock_print.call_count, 1)
AssertionError: 2 != 1

======================================================================
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!')]
Actual: [call('Invalid input arguments, expected my type!')]

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

FAILED (failures=3)

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