Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions majava/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Copy link
Owner

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 from Matcher and implement _match(self, other). Check, how DictContains works.

return self.value in other

def __repr__(self):
return repr(self.value)


class SimilarList:
Copy link
Owner

Choose a reason for hiding this comment

The 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)
18 changes: 17 additions & 1 deletion tests/test_basic.py
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():
Expand All @@ -22,6 +25,19 @@ def test_dict__plain_match():
assert {"a": 1, "b": 2} == DictContains({})


def test_anyof():
Copy link
Owner

Choose a reason for hiding this comment

The 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 1 == AnyOf(1, 2, 3)

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])
Copy link
Owner

Choose a reason for hiding this comment

The 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")
Expand Down
Loading