A pythonic assertion library.
poetry add conjecture
pip install conjecture
A basic assertion.
>>> import conjecture
>>> assert 5 == conjecture.has(lambda v: v < 10)
>>> assert 5 == conjecture.has(lambda v: v > 10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
Matching none.
>>> import conjecture
>>> assert None == conjecture.none()
Matching anything.
>>> import conjecture
>>> assert None == conjecture.anything()
>>> assert 123 == conjecture.anything()
>>> assert "abc" == conjecture.anything()
Matching keys.
>>> import conjecture
>>> assert {"a": 1} == conjecture.has_key("a")
>>> assert {"a": 1} == conjecture.has_key("a", of=1)
>>> assert {"a": 1} == conjecture.has_key("a", of=conjecture.less_than(5))
Matching instances of a class.
>>> import conjecture
>>> assert 123 == conjecture.instance_of(int)
>>> assert "abc" == conjecture.instance_of((str, bytes))
Matching values.
>>> import conjecture
>>> assert 123 == conjecture.equal_to(123)
>>> assert "abc" == conjecture.equal_to("abc")
Matching attributes.
>>> import conjecture
>>> assert 1 == conjecture.has_attribute("__class__")
>>> assert 1 == conjecture.has_attribute("__class__", of=int)
>>> assert 1 == conjecture.has_attribute("__class__", of=conjecture.instance_of(type))
Matching lesser values.
>>> import conjecture
>>> assert 5 == conjecture.greater_than(1)
>>> assert 5 == conjecture.greater_than_or_equal_to(1)
Matching greater values.
>>> import conjecture
>>> assert 1 == conjecture.less_than(5)
>>> assert 1 == conjecture.less_than_or_equal_to(5)
Matching empty collections.
>>> import conjecture
>>> assert list() == conjecture.empty()
>>> assert set() == conjecture.empty()
>>> assert tuple() == conjecture.empty()
>>> assert dict() == conjecture.empty()
Matching length of collections.
>>> import conjecture
>>> assert [1, 2, 3, 4, 5] == conjecture.length_of(5)
>>> assert [1, 2, 3] == conjecture.length_of(conjecture.less_than(5))
Matching string prefixes.
>>> import conjecture
>>> assert "foo bar baz" == conjecture.prefixed_with("foo")
>>> assert b"foo bar baz" == conjecture.prefixed_with(b"foo")
Matching string suffixes.
>>> import conjecture
>>> assert "foo bar baz" == conjecture.suffixed_with("baz")
>>> assert b"foo bar baz" == conjecture.suffixed_with(b"baz")
Matching all conditions.
>>> import conjecture
>>> assert 5 == conjecture.has(lambda v: v <= 5) & conjecture.has(lambda v: v => 5)
>>> assert 6 == conjecture.has(lambda v: v <= 5) & conjecture.has(lambda v: v => 5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert 5 == conjecture.all_of(
... conjecture.has(lambda v: v <= 5),
... conjecture.has(lambda v: v => 5)
... )
>>> assert 6 == conjecture.all_of(
... conjecture.has(lambda v: v <= 5),
... conjecture.has(lambda v: v => 5)
... )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
Matching any conditions.
>>> import conjecture
>>> assert 0 == conjecture.has(lambda v: v == 5) | conjecture.has(lambda v: v == 0)
>>> assert 5 == conjecture.has(lambda v: v == 5) | conjecture.has(lambda v: v == 0)
>>> assert 6 == conjecture.has(lambda v: v == 5) | conjecture.has(lambda v: v == 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert 5 == conjecture.any_of(
... conjecture.has(lambda v: v == 5),
... conjecture.has(lambda v: v == 0)
... )
>>> assert 6 == conjecture.any_of(
... conjecture.has(lambda v: v == 5),
... conjecture.has(lambda v: v == 0)
... )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
A negative assertion.
>>> import conjecture
>>> assert 5 != conjecture.has(lambda v: v == 10)
>>> assert 5 == ~conjecture.has(lambda v: v == 10)
>>> assert 10 == ~conjecture.has(lambda v: v == 10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
This project is licensed under the MIT licence.
All documentation and images are licenced under the Creative Commons Attribution-ShareAlike 4.0 International License.
This project uses Semantic Versioning.