From 87485e651e5c20ed12be5863afc52b39016bc581 Mon Sep 17 00:00:00 2001 From: Tamar Oren <66771972+tamar-ot@users.noreply.github.com> Date: Wed, 28 Dec 2022 07:51:27 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Migrated=20`great=5Fthan`=20matc?= =?UTF-8?q?her?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/expycted/internals/value.py | 76 +++++++++++++----- src/expycted/matchers/__init__.py | 1 + src/expycted/matchers/great_than_matcher.py | 15 ++++ test/expect_to_test_suite.py | 28 ------- test/helpers/stubs.py | 14 ---- test/unit/matchers/test_great_than_matcher.py | 80 +++++++++++++++++++ 6 files changed, 150 insertions(+), 64 deletions(-) create mode 100644 src/expycted/matchers/great_than_matcher.py create mode 100644 test/unit/matchers/test_great_than_matcher.py diff --git a/src/expycted/internals/value.py b/src/expycted/internals/value.py index ec370be..1f303ad 100644 --- a/src/expycted/internals/value.py +++ b/src/expycted/internals/value.py @@ -12,6 +12,7 @@ IsFalseMatcher, IsTrueMatcher, LessThanMatcher, + GreatThanMatcher, TypeMatcher, ) @@ -219,16 +220,61 @@ def inherit(self, actual: type) -> None: bool: Result """ - @assertion_old - def be_greater_than(self, actual: Any) -> None: - """Check whether the value is greater than something + @property + @assertion + def be_greater_than(self) -> GreatThanMatcher: + """Asserts that the actual value is greater than the expected value.""" - Args: - actual (Any): Value to compare to + return GreatThanMatcher - Returns: - bool: Result - """ + @property + @assertion + def be_greater_than_or_equal_to(self) -> GreatThanMatcher: + """Asserts that the actual value is greater than or equal to the expected value.""" + + return GreatThanMatcher(self, or_equal=True) + + @property + def be_great_than(self) -> GreatThanMatcher: + """Alias for ``be_greater_than``.""" + + return self.be_greater_than + + @property + def be_great(self) -> GreatThanMatcher: + """Alias for ``be_greater_than``.""" + + return self.be_greater_than + + @property + def be_greater(self) -> GreatThanMatcher: + """Alias for ``be_greater_than``.""" + + return self.be_greater_than + + @property + def be_greater_or_equal_to(self) -> GreatThanMatcher: + """Alias for ``be_greater_than_or_equal_to``.""" + + return self.be_greater_than_or_equal_to + + @property + def be_great_than_or_equal_to(self) -> GreatThanMatcher: + """Alias for ``be_greater_than_or_equal_to``.""" + + return self.be_greater_than_or_equal_to + + @property + def be_great_or_equal(self) -> GreatThanMatcher: + """Alias for ``be_greater_than_or_equal_to``.""" + + return self.be_greater_than_or_equal_to + + @property + def be_greater_or_equal(self) -> GreatThanMatcher: + """Alias for ``be_greater_than_or_equal_to``.""" + + return self.be_greater_than_or_equal_to @property @assertion @@ -286,17 +332,6 @@ def be_lesser_or_equal(self) -> LessThanMatcher: return self.be_lesser_than_or_equal_to - @assertion_old - def be_greater_or_equal_to(self, actual: Any) -> None: - """Check whether the value is greater than or equal to something - - Args: - actual (Any): Value to compare to - - Returns: - bool: Result - """ - @assertion_old def be_numeric(self) -> None: """Check whether the value is numeric @@ -309,9 +344,6 @@ def be_numeric(self) -> None: be_a_number = be_numeric - be_greater_or_equal = be_greater_than_or_equal_to = be_greater_or_equal_to - be_greater = be_greater_than - be_in = be_included_in = be_contained_in have = include = contain diff --git a/src/expycted/matchers/__init__.py b/src/expycted/matchers/__init__.py index 2fb9951..bd70d30 100644 --- a/src/expycted/matchers/__init__.py +++ b/src/expycted/matchers/__init__.py @@ -7,4 +7,5 @@ from .is_matcher import IsMatcher from .is_true_matcher import IsTrueMatcher from .less_than_matcher import LessThanMatcher +from .great_than_matcher import GreatThanMatcher from .type_matcher import TypeMatcher diff --git a/src/expycted/matchers/great_than_matcher.py b/src/expycted/matchers/great_than_matcher.py new file mode 100644 index 0000000..a50a399 --- /dev/null +++ b/src/expycted/matchers/great_than_matcher.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +from typing import Any + +from expycted.core.matchers import BaseMatcher + + +class GreatThanMatcher(BaseMatcher): + """Asserts the actual value is great than the expected value.""" + + def _matches(self, *, expected: Any) -> bool: + if self._expectation.qualifiers.or_equal: + return self._expectation.actual >= expected + + return self._expectation.actual > expected diff --git a/test/expect_to_test_suite.py b/test/expect_to_test_suite.py index d5a4d87..288b9c5 100644 --- a/test/expect_to_test_suite.py +++ b/test/expect_to_test_suite.py @@ -80,34 +80,6 @@ def test_to_inherit_type_error(expected, actual, context): expect(expected).to.inherit(actual) -@expected_actual_params(stubs.GREATER_THAN, extract_ids=False) -def test_to_be_greater_than(expected, actual, context): - expect(expected).to.be_greater_than(actual) - expect(expected).to.be_greater(actual) - - with context.raises: - expect(expected).to_not.be_greater_than(actual) - - with context.raises: - expect(expected).to_not.be_greater(actual) - - -@expected_actual_params(stubs.GREATER_THAN_OR_EQUAL, extract_ids=False) -def test_to_be_greater_than_or_equal_to(expected, actual, context): - expect(expected).to.be_greater_than_or_equal_to(actual) - expect(expected).to.be_greater_than_or_equal_to(actual) - expect(expected).to.be_greater_or_equal(actual) - - with context.raises: - expect(expected).to_not.be_greater_than_or_equal_to(actual) - - with context.raises: - expect(expected).to_not.be_greater_than_or_equal_to(actual) - - with context.raises: - expect(expected).to_not.be_greater_or_equal(actual) - - @expected_params(stubs.NUMERIC, extract_ids=False) def test_to_be_numeric(expected, context): expect(expected).to.be_numeric() diff --git a/test/helpers/stubs.py b/test/helpers/stubs.py index 4257c07..bc26c95 100644 --- a/test/helpers/stubs.py +++ b/test/helpers/stubs.py @@ -165,20 +165,6 @@ class Day(Enum): INHERIT_TYPE_ERROR = (("string", "str"),) -GREATER_THAN = ( - (3, 2), - (3.2, 3), - ([2], [1]), - ([1, 0], [1]), -) - - -GREATER_THAN_OR_EQUAL = ( - *GREATER_THAN, - (1, 1.0), - ([1], [1]), -) - NUMERIC = ( 1, "1", diff --git a/test/unit/matchers/test_great_than_matcher.py b/test/unit/matchers/test_great_than_matcher.py new file mode 100644 index 0000000..76dd865 --- /dev/null +++ b/test/unit/matchers/test_great_than_matcher.py @@ -0,0 +1,80 @@ +from __future__ import annotations + +from expycted import expect +from expycted.matchers import GreatThanMatcher + +from helpers import stubs +from helpers.utils import parametrize_expectation + + +def test_via_expect(context): + expectation = expect(2) + + assert isinstance(expectation.to.be_great, GreatThanMatcher) + assert isinstance(expectation.to.be_great_than, GreatThanMatcher) + assert isinstance(expectation.to.be_greater, GreatThanMatcher) + assert isinstance(expectation.to.be_greater_than, GreatThanMatcher) + + assert isinstance(expectation.to.be_great_or_equal, GreatThanMatcher) + assert isinstance(expectation.to.be_great_than_or_equal_to, GreatThanMatcher) + assert isinstance(expectation.to.be_greater_or_equal_to, GreatThanMatcher) + assert isinstance(expectation.to.be_greater_than_or_equal_to, GreatThanMatcher) + + expectation.to.be_great_than(1) + expectation.to.be_great_than_or_equal_to(2) + + with context.raises: + expectation.to.be_great_than(2) + + +@parametrize_expectation( + [ + ("hello world", "hello"), + (3, 2), + (3.2, 3), + ([2], [1]), + ([1, 0], [1]), + ], + matcher=GreatThanMatcher, + wrap=False, +) +def test_matches(expectation): + matcher = expectation.matcher() + + assert matcher(expectation.expected) is True + + +@parametrize_expectation( + [ + ("hello world", "hello"), + ("hello", "hello"), + (3, 2), + (2, 2), + (3.2, 3), + ([2], [1]), + ([1], [1]), + ], + matcher=GreatThanMatcher, + wrap=False, +) +def test_or_equal_matches(expectation): + matcher = expectation.matcher(or_equal=True) + + assert matcher(expectation.expected) is True + + +@parametrize_expectation( + [ + ("hello", "hello world"), + (2, 3), + (3, 3.2), + ([1], [2]), + ([1], [1, 0]), + ], + matcher=GreatThanMatcher, + wrap=False, +) +def test_not_matches(expectation): + matcher = expectation.matcher() + + assert matcher(expectation.expected) is False