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

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

1 точки общо

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

 1"""This task got more than 79 chars per line."""
 2
 3
 4
 5def type_check(in_or_out):
 6    def types(*abominational_types):
 7        def the_function(functioning):
 8            def function_args(*args, **kwargs):
 9                if in_or_out == "in":
10                    for arg in abominational_types:
11                        if not all(isinstance(arg, abominational_types)
12                                   for arg in args):
13
14                            print(f"Invalid input arguments, "
15                                  f"expected {', '.join(map(str, abominational_types))}!")
16
17                result = functioning(*args, **kwargs)
18                if in_or_out == "out":
19                    for arg in abominational_types:
20                        if not all(isinstance(arg, abominational_types)
21                                   for arg in args):
22                            print(f"Invalid output arguments, "
23                                  f"expected {', '.join(map(str, abominational_types))}!")
24
25                return result
26            return function_args
27        return the_function
28    return types

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 output 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 53, in test_check_in
self.assertEqual(mock_print.call_count, 1)
AssertionError: 4 != 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!')]

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

FAILED (failures=3)

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