-
Notifications
You must be signed in to change notification settings - Fork 0
AnyOf matcher and its tests #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,10 @@ def __init__(self, *value): | |
| self.first_value, self.second_value = value | ||
|
|
||
| def __eq__(self, other): | ||
| if self.first_value is None: | ||
| return other <= self.second_value | ||
| if self.second_value is None: | ||
| return self.first_value <= other | ||
| return self.first_value <= other <= self.second_value | ||
|
|
||
| def __repr__(self): | ||
|
|
@@ -47,3 +51,32 @@ def __eq__(self, other): | |
|
|
||
| def __repr__(self): | ||
| return ', '.join(it.__name__ for it in self.types) | ||
|
|
||
|
|
||
| class AnyOf: | ||
| """Actual value must match at least one of the expected values.""" | ||
|
|
||
| def __init__(self, value): | ||
| self.value = value | ||
|
|
||
| def __eq__(self, other): | ||
| return self.value in other | ||
|
|
||
| def __repr__(self): | ||
| return repr(self.value) | ||
|
|
||
|
|
||
| class SimilarList: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather name it as "unorderd list" for this particular case. |
||
| """List must match the other list no matter the order""" | ||
|
|
||
| def __init__(self, value): | ||
| self.value = value | ||
|
|
||
| def __eq__(self, other): | ||
| if len(set(self.value)) == len(set(other)): | ||
| common_issues = [i for i in self.value if i in other] | ||
| if len(set(common_issues)) == len(set(other)): | ||
| return common_issues | ||
|
|
||
| def __repr__(self): | ||
| return repr(self.value) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| import pytest | ||
| from majava.basic import InInterval, IsType, DictContains | ||
| from majava.basic import InInterval, IsType, DictContains, AnyOf, SimilarList | ||
|
|
||
|
|
||
| def test_ininterval(): | ||
| assert 7 == InInterval(1, 7) | ||
| assert 10 != InInterval(1, 7) | ||
| assert 7 == InInterval(None, 7) | ||
| assert 4 == InInterval(None, 7) | ||
| assert 1 == InInterval(1, None) | ||
|
|
||
|
|
||
| def test_istype(): | ||
|
|
@@ -22,6 +25,19 @@ def test_dict__plain_match(): | |
| assert {"a": 1, "b": 2} == DictContains({}) | ||
|
|
||
|
|
||
| def test_anyof(): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it should be vice versa =) Actual value should be equal to one of "AnyOf". Not "AnyOf" is equal to one of the actual. I.e. it should do: |
||
| assert [8, "a", 3.5] == AnyOf(8) | ||
| assert [8, "a", 3.5] == AnyOf("a") | ||
| assert [8, "a", 3.5] != AnyOf(4) | ||
|
|
||
|
|
||
| def test_similarlist(): | ||
| assert [8, "a", 3.5] == SimilarList(["a", 8, 3.5]) | ||
| assert [8, "a", 3.5] == SimilarList([3.5, 8, "a"]) | ||
| assert [8, "a", 3.5] != SimilarList([3.5, 9, "a"]) | ||
| assert [8, "a", 3.5] != SimilarList([3.5, 8, "a", 10]) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add also test(s) with empty lists, as it's usually a special corner case |
||
|
|
||
|
|
||
| @pytest.mark.parametrize("actual, expected, message", [ | ||
| ({"a": 1, "b": 2}, {"a": 1, "b": 3}, "Value 2 at 'b' does not match - 2 != 3"), | ||
| ({"a": 1}, {"b": 1}, "Value {'a': 1} at 'b' does not match - key 'b' not found") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of implementing its own
__eq__, we need to inherite fromMatcherand implement_match(self, other). Check, howDictContainsworks.