From 92fbf77230745cc69b329f8c2fc71b804a0f5a44 Mon Sep 17 00:00:00 2001 From: Mariia Samohkina Date: Wed, 12 Feb 2025 11:11:03 +0000 Subject: [PATCH 1/2] AnyOf matcher and its tests --- majava/basic.py | 13 +++++++++++++ tests/test_basic.py | 8 +++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/majava/basic.py b/majava/basic.py index 05b12d4..64f09d6 100644 --- a/majava/basic.py +++ b/majava/basic.py @@ -47,3 +47,16 @@ 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) \ No newline at end of file diff --git a/tests/test_basic.py b/tests/test_basic.py index ba1e937..5239ef1 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1,5 +1,5 @@ import pytest -from majava.basic import InInterval, IsType, DictContains +from majava.basic import InInterval, IsType, DictContains, AnyOf def test_ininterval(): @@ -22,6 +22,12 @@ 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) + + @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") From 29bd7ff1c5ee16df3a8b662a3f5527ccd6aef007 Mon Sep 17 00:00:00 2001 From: Mariia Samohkina Date: Wed, 12 Feb 2025 11:50:48 +0000 Subject: [PATCH 2/2] Made SimilarList, modified InInterval + tests --- majava/basic.py | 22 +++++++++++++++++++++- tests/test_basic.py | 12 +++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/majava/basic.py b/majava/basic.py index 64f09d6..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): @@ -59,4 +63,20 @@ def __eq__(self, other): return self.value in other def __repr__(self): - return repr(self.value) \ No newline at end of file + 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 5239ef1..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, AnyOf +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(): @@ -28,6 +31,13 @@ def test_anyof(): 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")