Skip to content

Commit 920916d

Browse files
committed
add comparator assertions
* assert_less() * assert_less_equal() * assert_greater() * assert_greater_equal()
1 parent 49656fb commit 920916d

File tree

3 files changed

+128
-25
lines changed

3 files changed

+128
-25
lines changed

NEWS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Version 0.6
22
===========
33

4+
* add assert_less(), assert_less_equal(), assert_greater(), and
5+
assert_greater_equal()
46
* assert_datetime_about_now()/assert_datetime_about_now_utc(): Handle
57
comparison with None more gracefully.
68

asserts.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,66 @@ def assert_almost_equal(first, second, places=None, msg=None, delta=None):
183183
fail(msg or "{!r} != {!r} ".format(first, second) + detail_msg)
184184

185185

186+
def assert_less(first, second, msg=None):
187+
"""Fail if first is not less than second.
188+
189+
>>> assert_less('bar', 'foo')
190+
>>> assert_less(5, 5)
191+
Traceback (most recent call last):
192+
...
193+
AssertionError: 5 is not less than 5
194+
195+
"""
196+
if not first < second:
197+
fail(msg or "{!r} is not less than {!r}".format(first, second))
198+
199+
200+
def assert_less_equal(first, second, msg=None):
201+
"""Fail if first is not less than or equal to second.
202+
203+
>>> assert_less_equal('bar', 'foo')
204+
>>> assert_less_equal(5, 5)
205+
>>> assert_less_equal(6, 5)
206+
Traceback (most recent call last):
207+
...
208+
AssertionError: 6 is not less than or equal to 5
209+
210+
"""
211+
if not first <= second:
212+
fail(msg or "{!r} is not less than or equal to {!r}".format(
213+
first, second))
214+
215+
216+
def assert_greater(first, second, msg=None):
217+
"""Fail if first is not greater than second.
218+
219+
>>> assert_greater('foo', 'bar')
220+
>>> assert_greater(5, 5)
221+
Traceback (most recent call last):
222+
...
223+
AssertionError: 5 is not greater than 5
224+
225+
"""
226+
if not first > second:
227+
fail(msg or "{!r} is not greater than {!r}".format(first, second))
228+
229+
230+
def assert_greater_equal(first, second, msg=None):
231+
"""Fail if first is not greater than or equal to second.
232+
233+
>>> assert_greater_equal('foo', 'bar')
234+
>>> assert_greater_equal(5, 5)
235+
>>> assert_greater_equal(5, 6)
236+
Traceback (most recent call last):
237+
...
238+
AssertionError: 5 is not greater than or equal to 6
239+
240+
"""
241+
if not first >= second:
242+
fail(msg or "{!r} is not greater than or equal to {!r}".format(
243+
first, second))
244+
245+
186246
def assert_regex(text, regex, msg=None):
187247
"""Fail if text does not match the regular expression.
188248

test_asserts.py

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@
33
from unittest import TestCase
44
import sys
55

6-
from asserts import (fail,
7-
assert_true,
8-
assert_false,
9-
assert_boolean_true,
10-
assert_boolean_false,
11-
assert_is_none,
12-
assert_is_not_none,
13-
assert_equal,
14-
assert_not_equal,
15-
assert_almost_equal,
16-
assert_between,
17-
assert_is,
18-
assert_is_not,
19-
assert_in,
20-
assert_not_in,
21-
assert_regex,
22-
assert_is_instance,
23-
assert_has_attr,
24-
assert_datetime_about_now,
25-
assert_datetime_about_now_utc,
26-
assert_raises,
27-
assert_raises_regex,
28-
assert_raises_errno,
29-
assert_succeeds,
30-
)
6+
from asserts import (
7+
fail,
8+
assert_true,
9+
assert_false,
10+
assert_boolean_true,
11+
assert_boolean_false,
12+
assert_is_none,
13+
assert_is_not_none,
14+
assert_equal,
15+
assert_not_equal,
16+
assert_almost_equal,
17+
assert_less,
18+
assert_less_equal,
19+
assert_greater,
20+
assert_greater_equal,
21+
assert_between,
22+
assert_is,
23+
assert_is_not,
24+
assert_in,
25+
assert_not_in,
26+
assert_regex,
27+
assert_is_instance,
28+
assert_has_attr,
29+
assert_datetime_about_now,
30+
assert_datetime_about_now_utc,
31+
assert_raises,
32+
assert_raises_regex,
33+
assert_raises_errno,
34+
assert_succeeds,
35+
)
3136

3237

3338
class _DummyObject(object):
@@ -215,6 +220,42 @@ def test_assert_almost_equal__places_and_delta(self):
215220
else:
216221
raise AssertionError("TypeError not raised")
217222

223+
def test_assert_less(self):
224+
assert_less(4, 5)
225+
with _assert_raises_assertion("5 is not less than 5"):
226+
assert_less(5, 5)
227+
with _assert_raises_assertion("'foo' is not less than 'bar'"):
228+
assert_less('foo', 'bar')
229+
with _assert_raises_assertion("test message"):
230+
assert_less(6, 5, msg="test message")
231+
232+
def test_assert_less_equal(self):
233+
assert_less_equal(4, 5)
234+
assert_less_equal(5, 5)
235+
with _assert_raises_assertion(
236+
"'foo' is not less than or equal to 'bar'"):
237+
assert_less_equal('foo', 'bar')
238+
with _assert_raises_assertion("test message"):
239+
assert_less_equal(6, 5, msg="test message")
240+
241+
def test_assert_greater(self):
242+
assert_greater(5, 4)
243+
with _assert_raises_assertion("5 is not greater than 5"):
244+
assert_greater(5, 5)
245+
with _assert_raises_assertion("'bar' is not greater than 'foo'"):
246+
assert_greater('bar', 'foo')
247+
with _assert_raises_assertion("test message"):
248+
assert_greater(5, 5, msg="test message")
249+
250+
def test_assert_greater_equal(self):
251+
assert_greater_equal(5, 4)
252+
assert_greater_equal(5, 5)
253+
with _assert_raises_assertion(
254+
"'bar' is not greater than or equal to 'foo'"):
255+
assert_greater_equal('bar', 'foo')
256+
with _assert_raises_assertion("test message"):
257+
assert_greater_equal(5, 6, msg="test message")
258+
218259
def test_assert_regex__matches_string(self):
219260
assert_regex("This is a test text", "is.*test")
220261

0 commit comments

Comments
 (0)