diff --git a/majava/basic.py b/majava/basic.py index 05b12d4..981f0ba 100644 --- a/majava/basic.py +++ b/majava/basic.py @@ -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: + """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) diff --git a/tests/test_basic.py b/tests/test_basic.py index ba1e937..a2b615d 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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(): + 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]) + + @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")