|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from common.utils import apply_decorator_to_methods |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize( |
| 9 | + "apply_to_protected_methods", |
| 10 | + [ |
| 11 | + True, |
| 12 | + False, |
| 13 | + ], |
| 14 | +) |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "apply_to_private_methods", |
| 17 | + [ |
| 18 | + True, |
| 19 | + False, |
| 20 | + ], |
| 21 | +) |
| 22 | +async def test_class_decorator( |
| 23 | + apply_to_protected_methods: bool, |
| 24 | + apply_to_private_methods: bool, |
| 25 | +): |
| 26 | + def add_ten_decorator(func): |
| 27 | + def wrapper(*args, **kwargs): |
| 28 | + result = func(*args, **kwargs) |
| 29 | + return result + 10 |
| 30 | + |
| 31 | + async def async_wrapper(*args, **kwargs): |
| 32 | + result = await func(*args, **kwargs) |
| 33 | + return result + 10 |
| 34 | + |
| 35 | + return wrapper if not asyncio.iscoroutinefunction(func) else async_wrapper |
| 36 | + |
| 37 | + @apply_decorator_to_methods( |
| 38 | + decorator=add_ten_decorator, |
| 39 | + protected_methods=apply_to_protected_methods, |
| 40 | + private_methods=apply_to_private_methods, |
| 41 | + ) |
| 42 | + class MyClass: |
| 43 | + def get_public(self): |
| 44 | + return 10 |
| 45 | + |
| 46 | + def _get_protected(self): |
| 47 | + return 10 |
| 48 | + |
| 49 | + def __get_private(self): |
| 50 | + return 10 |
| 51 | + |
| 52 | + async def get_apublic(self): |
| 53 | + return 10 |
| 54 | + |
| 55 | + async def _get_aprotected(self): |
| 56 | + return 10 |
| 57 | + |
| 58 | + async def __get_aprivate(self): |
| 59 | + return 10 |
| 60 | + |
| 61 | + c = MyClass() |
| 62 | + assert c.get_public() == 20 |
| 63 | + assert c._get_protected() == 20 if apply_to_protected_methods else 10 |
| 64 | + assert c._MyClass__get_private() == 20 if apply_to_private_methods else 10 |
| 65 | + assert await c.get_apublic() == 20 |
| 66 | + assert await c._get_aprotected() == 20 if apply_to_protected_methods else 10 |
| 67 | + assert await c._MyClass__get_aprivate() == 20 if apply_to_private_methods else 10 |
0 commit comments