Skip to content

Commit d1bf6d5

Browse files
authored
Merge pull request #598 from testing-cabal/issue/594
Add LessThanOrEqual + GreaterThanOrEqual matchers
2 parents 208d2db + a12e720 commit d1bf6d5

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

tests/matchers/test_basic.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
EndsWith,
1313
Equals,
1414
GreaterThan,
15+
GreaterThanOrEqual,
1516
HasLength,
1617
Is,
1718
IsInstance,
1819
LessThan,
20+
LessThanOrEqual,
1921
MatchesRegex,
2022
Nearly,
2123
NotEquals,
@@ -217,6 +219,36 @@ class TestGreaterThanInterface(TestCase, TestMatchersInterface):
217219
]
218220

219221

222+
class TestLessThanOrEqualInterface(TestCase, TestMatchersInterface):
223+
matches_matcher: ClassVar[Matcher[Any]] = LessThanOrEqual(4)
224+
matches_matches: ClassVar[list[Any]] = [-5, 3, 4]
225+
matches_mismatches: ClassVar[list[Any]] = [5, 5000]
226+
227+
str_examples: ClassVar = [
228+
("LessThanOrEqual(12)", LessThanOrEqual(12)),
229+
]
230+
231+
describe_examples: ClassVar = [
232+
("5 > 4", 5, LessThanOrEqual(4)),
233+
("6 > 4", 6, LessThanOrEqual(4)),
234+
]
235+
236+
237+
class TestGreaterThanOrEqualInterface(TestCase, TestMatchersInterface):
238+
matches_matcher: ClassVar[Matcher[Any]] = GreaterThanOrEqual(4)
239+
matches_matches: ClassVar[list[Any]] = [4, 5, 8]
240+
matches_mismatches: ClassVar[list[Any]] = [-2, 0, 3]
241+
242+
str_examples: ClassVar = [
243+
("GreaterThanOrEqual(12)", GreaterThanOrEqual(12)),
244+
]
245+
246+
describe_examples: ClassVar = [
247+
("3 < 4", 3, GreaterThanOrEqual(4)),
248+
("2 < 4", 2, GreaterThanOrEqual(4)),
249+
]
250+
251+
220252
class TestContainsInterface(TestCase, TestMatchersInterface):
221253
matches_matcher: ClassVar[Matcher[Any]] = Contains("foo")
222254
matches_matches: ClassVar[list[Any]] = ["foo", "afoo", "fooa"]

testtools/matchers/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
"FileContains",
3131
"FileExists",
3232
"GreaterThan",
33+
"GreaterThanOrEqual",
3334
"HasLength",
3435
"HasPermissions",
3536
"Is",
3637
"IsDeprecated",
3738
"IsInstance",
3839
"KeysEqual",
3940
"LessThan",
41+
"LessThanOrEqual",
4042
"MatchesAll",
4143
"MatchesAny",
4244
"MatchesDict",
@@ -67,10 +69,12 @@
6769
EndsWith,
6870
Equals,
6971
GreaterThan,
72+
GreaterThanOrEqual,
7073
HasLength,
7174
Is,
7275
IsInstance,
7376
LessThan,
77+
LessThanOrEqual,
7478
MatchesRegex,
7579
Nearly,
7680
NotEquals,

testtools/matchers/_basic.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
"EndsWith",
66
"Equals",
77
"GreaterThan",
8+
"GreaterThanOrEqual",
89
"HasLength",
910
"Is",
1011
"IsInstance",
1112
"LessThan",
13+
"LessThanOrEqual",
1214
"MatchesRegex",
1315
"Nearly",
1416
"NotEquals",
@@ -200,6 +202,20 @@ class GreaterThan(_BinaryComparison[T]):
200202
mismatch_string = "<="
201203

202204

205+
class LessThanOrEqual(_BinaryComparison[T]):
206+
"""Matches if the item is less than or equal to the matchers reference object."""
207+
208+
comparator = operator.le
209+
mismatch_string = ">"
210+
211+
212+
class GreaterThanOrEqual(_BinaryComparison[T]):
213+
"""Matches if the item is greater than or equal to the matchers reference object."""
214+
215+
comparator = operator.ge
216+
mismatch_string = "<"
217+
218+
203219
class _NotNearlyEqual(Mismatch, Generic[T]):
204220
"""Mismatch for Nearly matcher."""
205221

0 commit comments

Comments
 (0)