Skip to content

Commit d9fd530

Browse files
Add Tests for StringUtils (#16)
* Add Tests for StringUtils * Fix documentation * Fix code format * Fix type issues * Fix imports, Increase coverage threshold * Fix Code format
1 parent e2669cb commit d9fd530

File tree

15 files changed

+252
-166
lines changed

15 files changed

+252
-166
lines changed

docs/package/base/.pages

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/package/base/char.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

pycommons/lang/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33

44
from importlib_metadata import PackageNotFoundError, version
55

6+
from .arrayutils import ArrayUtils
7+
from .charutils import CharUtils
8+
from .exceptionutils import ExceptionUtils
9+
from .stringutils import StringUtils
10+
11+
__all__ = [
12+
"__author__",
13+
"__email__",
14+
"__version__",
15+
"ArrayUtils",
16+
"CharUtils",
17+
"ExceptionUtils",
18+
"StringUtils",
19+
]
20+
621
__author__ = "Shashank Sharma"
722
__email__ = "shashankrnr32@gmail.com"
823

File renamed without changes.
File renamed without changes.

pycommons/lang/exceptionutils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import traceback
2+
from contextlib import AbstractContextManager
3+
from types import TracebackType
4+
from typing import TypeVar, Type, Optional, Union, Generic
5+
6+
from pycommons.base.utils.utils import UtilityClass
7+
8+
_E = TypeVar("_E", Exception, BaseException)
9+
10+
11+
class ExceptionUtils(UtilityClass):
12+
@classmethod
13+
def get_cause(cls, exception: _E) -> Optional[BaseException]:
14+
return exception.__cause__
15+
16+
@classmethod
17+
def ignored(cls, exception_type: Type[_E] = Exception) -> "IgnoredExceptionContext[_E]":
18+
return IgnoredExceptionContext(exception_type)
19+
20+
21+
class IgnoredExceptionContext(AbstractContextManager, Generic[_E]): # type: ignore
22+
def __init__(self, exception_type: Type[BaseException]):
23+
self._expected_exc_type: Type[BaseException] = exception_type
24+
self._exception: Optional[Union[_E, BaseException]] = None
25+
26+
@property
27+
def exception(self) -> Optional[Union[_E, BaseException]]:
28+
return self._exception
29+
30+
def __exit__(
31+
self,
32+
__exc_type: Optional[Type[BaseException]],
33+
__exc_value: Optional[BaseException],
34+
__traceback: Optional[TracebackType],
35+
) -> Optional[bool]:
36+
self._exception = __exc_value
37+
if (
38+
__exc_type is not None
39+
and (
40+
__exc_type == self._expected_exc_type
41+
or issubclass(__exc_type, self._expected_exc_type)
42+
)
43+
and __traceback is not None
44+
):
45+
traceback.clear_frames(__traceback)
46+
return True
47+
48+
if __exc_type is not None and __exc_type != self._expected_exc_type:
49+
return False
50+
51+
return None
File renamed without changes.

pycommons/lang/utils/__init__.py

Whitespace-only changes.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ max-locals = 20
5757
min-public-method = 0
5858

5959
[tool.pytest.ini_options]
60-
addopts = "--cov=pycommons --cov-branch --cov-report term-missing --cov-report xml -vv --color=yes --cov-fail-under 10"
60+
addopts = "--cov=pycommons --cov-branch --cov-report term-missing --cov-report xml -vv --color=yes --cov-fail-under 40"
6161
python_files = "tests.py test_*.py *_tests.py *Test.py"
6262

6363
[build-system]

tests/parametrized.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dataclasses
22
import functools
3-
from typing import Any, Optional, List
3+
from typing import Any, Optional
44

55

66
@dataclasses.dataclass
@@ -17,7 +17,7 @@ def get_message(self):
1717
)
1818

1919

20-
def cases(testcases: List[TestData]):
20+
def cases(*testcases: TestData):
2121
def decorator(f):
2222
@functools.wraps(f)
2323
def wrapped(self):

0 commit comments

Comments
 (0)