A pythonic assertion library
Project description
Conjecture
A pythonic assertion library.
🛠 Installing
Poetry
poetry add conjecture
pip
pip install conjecture
🎓 Usage
Basic
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
Built-in conjectures
General
Matching none.
>>> import conjecture
>>> assert None == conjecture.none()
Matching anything.
>>> import conjecture
>>> assert None == conjecture.anything()
>>> assert 123 == conjecture.anything()
>>> assert "abc" == conjecture.anything()
Mapping
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))
Object
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))
Rich ordering
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)
Size
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))
String
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")
Combined conjectures
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
Negation
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
⚖️ Licence
This project is licensed under the MIT licence.
All documentation and images are licenced under the Creative Commons Attribution-ShareAlike 4.0 International License.
📝 Meta
This project uses Semantic Versioning.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file conjecture-0.1.0.tar.gz
.
File metadata
- Download URL: conjecture-0.1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0b2 CPython/3.10.5 Linux/5.13.0-1031-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4b5480a068266bb040ae792dfc721b5d46137f59b35bd841304e2889843b958 |
|
MD5 | 887402c26124c00bf9bc8768e5124141 |
|
BLAKE2b-256 | bbb7b7850bcc8a9092623831410c0a7ed33bbf4c5607a7bb9e6038fb759ef7ce |
File details
Details for the file conjecture-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: conjecture-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0b2 CPython/3.10.5 Linux/5.13.0-1031-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ca21418bd9720aae534f16ee8150434d61df37e9dda34c03ef3c00b5b9133cf |
|
MD5 | b808c3aa8affdf089cb28c14bb6d756e |
|
BLAKE2b-256 | 885e727d0c179498690f58d4fafa6886fa35a3e8d3a892d65e7fc29fd97f8caf |