Skip to content

Commit 05a5a37

Browse files
committed
- improve project info
- add MIT license - improve readme
1 parent 36878cd commit 05a5a37

File tree

5 files changed

+102
-9
lines changed

5 files changed

+102
-9
lines changed

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 SourceCode.AI
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
fassert: Fuzzy assert
1+
^fassert$: Fuzzy assert
22
---------------------
33

4-
Assert in your tests only a subset of data that matters
4+
Fuzzy assert in your tests only a subset of data that matters
55

66
```
77
from fassert import fassert
@@ -18,20 +18,75 @@ fassert(
1818
) # key2: value2 is ignored as it's not defined in the template
1919
2020
fassert(
21-
{"key": "value"},
21+
{"key": "value", "abc": "value"},
2222
# You can nest and combine the fuzzy matching types in containers
2323
{re.compile(r"[a-z]{3}"): "value"}
2424
)
2525
26+
fassert(
27+
[1, {"very": {"nested": {"dictionary": "data"}}}, {"not": "this"}],
28+
# Isn't this cool?
29+
[{"very": {re.compile(".{6}"): {"dictionary": lambda x: len(x) == 4}}}]
30+
)
31+
2632
# Template can contain callables as well
2733
fassert("value", lambda x: x == "value")
2834
2935
try:
36+
fassert("expected", "to not match")
3037
fassert({"a": "b"}, {"c": "d"}) # This will fail, {"c":"d"} is not in the target data
38+
fassert([1, 2, 3], [4])
39+
fassert("string", b"string") # bytes != string in fassert
3140
except AssertionError:
3241
pass # All the examples within the try block above will raise this exception
3342
```
3443

3544
In fassert, you can define a template to match your data against.
3645
When you use a type that is a container (e.g. list, tuple, dict, etc...), then only the data that you defined in the template will be asserted.
3746
All the addition data in the container will be ignored
47+
48+
Did you know this project is also dedicated to have **0 dependencies** on other packages?
49+
50+
Installation
51+
------------
52+
53+
From PyPI:
54+
`pip install fassert`
55+
56+
Locally:
57+
```
58+
pip install .
59+
60+
# Run tests with:
61+
python -m unittest discover -s tests/
62+
```
63+
64+
65+
66+
Advanced usage
67+
--------------
68+
69+
`fassert` function is equivalent in creating a default `FuzzyAssert()` object and calling match on it.
70+
You can configure some behaviour of the fuzzy matcher via the object attributes like so:
71+
72+
```
73+
from fassert import FuzzyAssert
74+
75+
fasserter = FuzzyAssert()
76+
# Eval functions is turned on by default
77+
fasserter.match("value", lambda x: x == "value")
78+
79+
fasserter.eval_functions = False
80+
# This will now raise Assertion error
81+
fasserter.match("value", lambda x: x == "value")
82+
83+
```
84+
85+
Bellow is an overview of the configurable options and their default values
86+
87+
| Name | Default value | Description |
88+
|-------------------------------|---------------|----------------------------------------------------------------------------------------|
89+
| eval_functions | True | Enable template matching as callable functions |
90+
| regex_allowed | True | Enable matching regexes from template agains strings in the data |
91+
| fuzzy_sequence_types | False | Ignore types of similar sequence types when matching the template (e.g. tuple vs list) |
92+
| check_minimum_sequence_length | True | Check that the data has a minimum length of greater or equal to the template |

fassert/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from abc import ABC, abstractmethod
33
from typing import Pattern, Sequence, Literal
44

5+
__version__ = "0.2"
6+
57

68
class FuzzyAssert:
79
def __init__(self):
@@ -18,7 +20,9 @@ def match(self, data, template) -> Literal[True]:
1820
1921
:param data: Data to match against, extra data, container types or other python types is ignored
2022
:param template: Template to match against, everything defined here must have a match
23+
:raises AssertionError: Raised when data does not match the given template
2124
:return: True if `template` matches the `data`
25+
:rtype: Literal[True]
2226
"""
2327
if inspect.isfunction(template):
2428
if self.eval_functions and template(data):

setup.cfg

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
[metadata]
22
name = fassert
3-
version = 0.1
3+
version = attr: fassert.__version__
44
author = Martin Carnogursky
55
author_email = admin@sourcecode.ai
6-
6+
long_description = file: README.md
7+
url = https://github.com/SourceCode-AI/fassert
78
classifiers =
8-
Programming Language :: Python :: 3
9+
Intended Audience :: Developers
10+
Programming Language :: Python :: 3 :: Only
11+
License :: OSI Approved :: MIT License
912
Operating System :: OS Independent
13+
keywords = fuzzy, assert, matching, nested
1014

1115

1216
[options]
13-
packages = find:
17+
packages = fassert
1418
python_requires = >=3.8

tests/test_misc.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,32 @@ def test_readme_main_example(self):
1212

1313
fassert("This is expected", "This is expected")
1414

15-
fassert("This matches as well", re.compile(r"[thismacewl ]+", re.I))
15+
fassert("This matches as well", re.compile(r"[thismachewl ]+", re.I))
1616

1717
fassert(
1818
{"key": "value", "key2": "value2"},
1919
{"key": "value"}
2020
) # key2: value2 is ignored as it's not defined in the template
2121

2222
fassert(
23-
{"key": "value"},
23+
{"key": "value", "abc": "value"},
2424
# You can nest and combine the fuzzy matching types in containers
2525
{re.compile(r"[a-z]{3}"): "value"}
2626
)
2727

28+
fassert(
29+
[1, {"very": {"nested": {"dictionary": "data"}}}, {"not": "this"}],
30+
# Isn't this cool?
31+
[{"very": {re.compile(".{6}"): {"dictionary": lambda x: len(x) == 4}}}]
32+
)
33+
2834
# Template can contain callables as well
2935
fassert("value", lambda x: x == "value")
3036

3137
try:
38+
fassert("expected", "to not match")
3239
fassert({"a": "b"}, {"c": "d"}) # This will fail, {"c":"d"} is not in the target data
40+
fassert([1, 2, 3], [4])
41+
fassert("string", b"string") # bytes != string in fassert
3342
except AssertionError:
3443
pass # All the examples within the try block above will raise this exception

0 commit comments

Comments
 (0)