.. image:: https://readthedocs.org/projects/assertionlib/badge/?version=latest
:target: https://assertionlib.readthedocs.io/en/latest/
.. image:: https://badge.fury.io/py/AssertionLib.svg
:target: https://badge.fury.io/py/AssertionLib
.. image:: https://github.com/nlesc-nano/AssertionLib/workflows/Python%20package/badge.svg
:target: https://github.com/nlesc-nano/AssertionLib/actions
.. image:: https://codecov.io/gh/nlesc-nano/AssertionLib/branch/master/graph/badge.svg
:target: https://codecov.io/gh/nlesc-nano/AssertionLib
.. image:: https://zenodo.org/badge/214183943.svg
:target: https://zenodo.org/badge/latestdoi/214183943
|
.. image:: https://img.shields.io/badge/python-3.6-blue.svg
:target: https://docs.python.org/3.6/
.. image:: https://img.shields.io/badge/python-3.7-blue.svg
:target: https://docs.python.org/3.7/
.. image:: https://img.shields.io/badge/python-3.8-blue.svg
:target: https://docs.python.org/3.8/
.. image:: https://img.shields.io/badge/python-3.9-blue.svg
:target: https://docs.python.org/3.9/
.. image:: https://img.shields.io/badge/python-3.10-blue.svg
:target: https://docs.python.org/3.10/
.. image:: https://img.shields.io/badge/python-3.11-blue.svg
:target: https://docs.python.org/3.11/
############
AssertionLib
############
A package for performing assertions and providing informative exception messages.
Installation
************
* PyPi: ``pip install AssertionLib``
* GitHub: ``pip install git+https://github.com/nlesc-nano/AssertionLib``
Usage
*****
A comprehensive overview of all available assertion methods is
provided in the documentation_.
A few examples of some basic assertion:
.. code:: python
>>> import numpy as np
>>> from assertionlib import assertion
# Assert the output of specific callables
>>> assertion.eq(5, 5) # 5 == 5
>>> assertion.lt(5, 6) # 5 < 6
>>> assertion.gt(6, 5) # 5 > 6
>>> assertion.isinstance(5, int)
>>> assertion.hasattr(5, '__init__')
>>> assertion.any([False, False, True])
>>> assertion.isfinite(1.0)
# Simply assert a value
>>> assertion(5 == 5)
>>> assertion(isinstance(5, int))
# Apply post-processing before conducting the assertion
>>> ar_large = np.ones(10)
>>> ar_small = np.zeros(10)
>>> assertion.gt(ar_large, ar_small, post_process=np.all) # all(ar_large > ar_small)
# Perform an assertion which will raise an AssertionError
>>> assertion.eq(5, 6, message='Fancy custom error message') # 5 == 6
Traceback (most recent call last):
...
AssertionError: output = eq(a, b); assert output
exception: AssertionError = AssertionError('Fancy custom error message')
output: bool = False
a: int = 5
b: int = 6
A few examples of AssertionErrors raised due to incorrect method signatures:
.. code:: python
>>> from assertionlib import assertion
>>> assertion.len(5)
Traceback (most recent call last):
...
AssertionError: output = len(obj); assert output
exception: TypeError = TypeError("object of type 'int' has no len()")
output: NoneType = None
obj: int = 5
.. code:: python
>>> from assertionlib import assertion
>>> assertion.eq(5, 5, 5, 5)
Traceback (most recent call last):
...
AssertionError: output = eq(a, b, _a, _b); assert output
exception: TypeError = TypeError('eq expected 2 arguments, got 4')
output: NoneType = None
a: int = 5
b: int = 5
_a: int = 5
_b: int = 5
A demonstration of the ``exception`` parameter.
Providing an exception type will assert that the provided exception is raised
during/before the assertion process:
.. code:: python
>>> from assertionlib import assertion
>>> len(5)
Traceback (most recent call last):
...
TypeError: object of type 'int' has no len()
.. code:: python
>>> from assertionlib import assertion
>>> assertion.len(5, exception=TypeError) # i.e. len(5) should raise a TypeError
>>> assertion.len([5], exception=TypeError)
Traceback (most recent call last):
...
AssertionError: output = len(obj); assert output
exception: AssertionError = AssertionError("Failed to raise 'TypeError'")
output: int = 1
obj: list = [5]
Lastly, the output of custom callables can be asserted in one of the following two ways,
supplying the callable to ``AssertionManager.assert()`` or creating a custom assertion
method and adding it to an instance with ``AssertionManager.add_to_instance()``:
.. code:: python
>>> from assertionlib import assertion
>>> def my_fancy_func(a: object) -> bool:
... return False
# Approach #1, supply to-be asserted callable to assertion.assert_()
>>> assertion.assert_(my_fancy_func, 5)
Traceback (most recent call last):
...
AssertionError: output = my_fancy_func(a); assert output
exception: AssertionError = AssertionError(None)
output: bool = False
a: int = 5
.. code:: python
>>> from assertionlib import assertion
# Approach #2, permanantly add a new bound method using assertion.add_to_instance()
>>> assertion.add_to_instance(my_fancy_func)
>>> assertion.my_fancy_func(5)
Traceback (most recent call last):
...
AssertionError: output = my_fancy_func(a); assert output
exception: AssertionError = AssertionError(None)
output: bool = False
a: int = 5
.. _documentation: https://assertionlib.readthedocs.io/en/latest/3_assertionmanager.html
Raw data
{
"_id": null,
"home_page": "https://github.com/nlesc-nano/AssertionLib",
"name": "AssertionLib",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "assertion,assertions,assertion-library,testing,unit-testing,python-3,python-3-6,python-3-7,python-3-8,python-3-9,python-3-10,python-3-11",
"author": "['B. F. van Beek']",
"author_email": "b.f.van.beek@vu.nl",
"download_url": "https://files.pythonhosted.org/packages/3d/73/fda51a469c141c8c70e4d664167198e12320d5d277072b27d6652d887232/AssertionLib-3.2.2.tar.gz",
"platform": null,
"description": ".. image:: https://readthedocs.org/projects/assertionlib/badge/?version=latest\n :target: https://assertionlib.readthedocs.io/en/latest/\n.. image:: https://badge.fury.io/py/AssertionLib.svg\n :target: https://badge.fury.io/py/AssertionLib\n.. image:: https://github.com/nlesc-nano/AssertionLib/workflows/Python%20package/badge.svg\n :target: https://github.com/nlesc-nano/AssertionLib/actions\n.. image:: https://codecov.io/gh/nlesc-nano/AssertionLib/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/nlesc-nano/AssertionLib\n.. image:: https://zenodo.org/badge/214183943.svg\n :target: https://zenodo.org/badge/latestdoi/214183943\n\n|\n\n.. image:: https://img.shields.io/badge/python-3.6-blue.svg\n :target: https://docs.python.org/3.6/\n.. image:: https://img.shields.io/badge/python-3.7-blue.svg\n :target: https://docs.python.org/3.7/\n.. image:: https://img.shields.io/badge/python-3.8-blue.svg\n :target: https://docs.python.org/3.8/\n.. image:: https://img.shields.io/badge/python-3.9-blue.svg\n :target: https://docs.python.org/3.9/\n.. image:: https://img.shields.io/badge/python-3.10-blue.svg\n :target: https://docs.python.org/3.10/\n.. image:: https://img.shields.io/badge/python-3.11-blue.svg\n :target: https://docs.python.org/3.11/\n\n############\nAssertionLib\n############\nA package for performing assertions and providing informative exception messages.\n\n\nInstallation\n************\n* PyPi: ``pip install AssertionLib``\n* GitHub: ``pip install git+https://github.com/nlesc-nano/AssertionLib``\n\n\nUsage\n*****\nA comprehensive overview of all available assertion methods is\nprovided in the documentation_.\nA few examples of some basic assertion:\n\n.. code:: python\n\n >>> import numpy as np\n >>> from assertionlib import assertion\n\n # Assert the output of specific callables\n >>> assertion.eq(5, 5) # 5 == 5\n >>> assertion.lt(5, 6) # 5 < 6\n >>> assertion.gt(6, 5) # 5 > 6\n >>> assertion.isinstance(5, int)\n >>> assertion.hasattr(5, '__init__')\n >>> assertion.any([False, False, True])\n >>> assertion.isfinite(1.0)\n\n # Simply assert a value\n >>> assertion(5 == 5)\n >>> assertion(isinstance(5, int))\n\n # Apply post-processing before conducting the assertion\n >>> ar_large = np.ones(10)\n >>> ar_small = np.zeros(10)\n >>> assertion.gt(ar_large, ar_small, post_process=np.all) # all(ar_large > ar_small)\n\n # Perform an assertion which will raise an AssertionError\n >>> assertion.eq(5, 6, message='Fancy custom error message') # 5 == 6\n Traceback (most recent call last):\n ...\n AssertionError: output = eq(a, b); assert output\n\n exception: AssertionError = AssertionError('Fancy custom error message')\n\n output: bool = False\n a: int = 5\n b: int = 6\n\nA few examples of AssertionErrors raised due to incorrect method signatures:\n\n.. code:: python\n\n >>> from assertionlib import assertion\n\n >>> assertion.len(5)\n Traceback (most recent call last):\n ...\n AssertionError: output = len(obj); assert output\n\n exception: TypeError = TypeError(\"object of type 'int' has no len()\")\n\n output: NoneType = None\n obj: int = 5\n\n\n.. code:: python\n\n >>> from assertionlib import assertion\n\n >>> assertion.eq(5, 5, 5, 5)\n Traceback (most recent call last):\n ...\n AssertionError: output = eq(a, b, _a, _b); assert output\n\n exception: TypeError = TypeError('eq expected 2 arguments, got 4')\n\n output: NoneType = None\n a: int = 5\n b: int = 5\n _a: int = 5\n _b: int = 5\n\nA demonstration of the ``exception`` parameter.\nProviding an exception type will assert that the provided exception is raised\nduring/before the assertion process:\n\n.. code:: python\n\n >>> from assertionlib import assertion\n\n >>> len(5)\n Traceback (most recent call last):\n ...\n TypeError: object of type 'int' has no len()\n\n\n.. code:: python\n\n >>> from assertionlib import assertion\n\n >>> assertion.len(5, exception=TypeError) # i.e. len(5) should raise a TypeError\n >>> assertion.len([5], exception=TypeError)\n Traceback (most recent call last):\n ...\n AssertionError: output = len(obj); assert output\n\n exception: AssertionError = AssertionError(\"Failed to raise 'TypeError'\")\n\n output: int = 1\n obj: list = [5]\n\nLastly, the output of custom callables can be asserted in one of the following two ways,\nsupplying the callable to ``AssertionManager.assert()`` or creating a custom assertion\nmethod and adding it to an instance with ``AssertionManager.add_to_instance()``:\n\n.. code:: python\n\n >>> from assertionlib import assertion\n\n >>> def my_fancy_func(a: object) -> bool:\n ... return False\n\n # Approach #1, supply to-be asserted callable to assertion.assert_()\n >>> assertion.assert_(my_fancy_func, 5)\n Traceback (most recent call last):\n ...\n AssertionError: output = my_fancy_func(a); assert output\n\n exception: AssertionError = AssertionError(None)\n\n output: bool = False\n a: int = 5\n\n\n.. code:: python\n\n >>> from assertionlib import assertion\n\n # Approach #2, permanantly add a new bound method using assertion.add_to_instance()\n >>> assertion.add_to_instance(my_fancy_func)\n >>> assertion.my_fancy_func(5)\n Traceback (most recent call last):\n ...\n AssertionError: output = my_fancy_func(a); assert output\n\n exception: AssertionError = AssertionError(None)\n\n output: bool = False\n a: int = 5\n\n.. _documentation: https://assertionlib.readthedocs.io/en/latest/3_assertionmanager.html\n\n\n",
"bugtrack_url": null,
"license": "Apache Software License",
"summary": "A package for performing assertions and providing informative exception messages.",
"version": "3.2.2",
"split_keywords": [
"assertion",
"assertions",
"assertion-library",
"testing",
"unit-testing",
"python-3",
"python-3-6",
"python-3-7",
"python-3-8",
"python-3-9",
"python-3-10",
"python-3-11"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cb62fce3b5daafe3e444af09bacf1805ba551e9b1667ace511de49cd577e0b3c",
"md5": "4156926d6804128b50ca0121d5e0d7af",
"sha256": "d0d08f778fc378bb2231072b9448e07d8274e0281729b984a816c40158055939"
},
"downloads": -1,
"filename": "AssertionLib-3.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4156926d6804128b50ca0121d5e0d7af",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 33105,
"upload_time": "2023-04-19T17:15:50",
"upload_time_iso_8601": "2023-04-19T17:15:50.581828Z",
"url": "https://files.pythonhosted.org/packages/cb/62/fce3b5daafe3e444af09bacf1805ba551e9b1667ace511de49cd577e0b3c/AssertionLib-3.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3d73fda51a469c141c8c70e4d664167198e12320d5d277072b27d6652d887232",
"md5": "e1e26169a8ca493ffa257392ce5d5741",
"sha256": "b018919ceb34c815cfc303cf48c9d10461f6e6bbe90cf31618ea57a336a33965"
},
"downloads": -1,
"filename": "AssertionLib-3.2.2.tar.gz",
"has_sig": false,
"md5_digest": "e1e26169a8ca493ffa257392ce5d5741",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 38592,
"upload_time": "2023-04-19T17:15:52",
"upload_time_iso_8601": "2023-04-19T17:15:52.237628Z",
"url": "https://files.pythonhosted.org/packages/3d/73/fda51a469c141c8c70e4d664167198e12320d5d277072b27d6652d887232/AssertionLib-3.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-19 17:15:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "nlesc-nano",
"github_project": "AssertionLib",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "assertionlib"
}