Skip to content

Commit 9ff4ab3

Browse files
committed
test dupe extensions
1 parent ecb1bd9 commit 9ff4ab3

File tree

1 file changed

+89
-10
lines changed

1 file changed

+89
-10
lines changed

tests/test_extensions.py

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2727
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

29-
from assertpy import assert_that, add_extension
29+
from assertpy import assert_that, add_extension, remove_extension, fail
3030
import numbers
3131

3232

@@ -38,13 +38,24 @@ def is_even(self):
3838
return self
3939

4040
def is_multiple_of(self, other):
41-
if isinstance(self.val, numbers.Integral) is False:
42-
raise TypeError('val must be an integer')
43-
if isinstance(other, numbers.Integral) is False:
44-
raise TypeError('given arg must be an integer')
41+
# validate actual value - must be "integer" (aka int or long)
42+
if isinstance(self.val, numbers.Integral) is False or self.val <= 0:
43+
# bad input is error, not an assertion fail, so raise error
44+
raise TypeError('val must be a positive integer')
45+
46+
# validate expected value
47+
if isinstance(other, numbers.Integral) is False or other <= 0:
48+
raise TypeError('given arg must be a positive integer')
49+
50+
# divide and compute remainder using divmod() built-in
4551
_, rem = divmod(self.val, other)
46-
if rem != 0:
52+
53+
# test the negative (is remainder non-zero?)
54+
if rem > 0:
55+
# non-zero remainder, so not multiple -> we fail!
4756
self._err('Expected <%s> to be multiple of <%s>, but was not.' % (self.val, other))
57+
58+
# success, and return self to allow chaining
4859
return self
4960

5061
add_extension(is_even)
@@ -94,16 +105,84 @@ def test_is_multiple_of_extension_failure():
94105
except AssertionError as ex:
95106
assert_that(str(ex)).is_equal_to('Expected <24> to be multiple of <5>, but was not.')
96107

97-
def test_is_multiple_of_extension_failure_not_integer():
108+
def test_is_multiple_of_extension_failure_bad_val():
98109
try:
99110
assert_that(24.0).is_multiple_of(5)
100111
fail('should have raised error')
101112
except TypeError as ex:
102-
assert_that(str(ex)).is_equal_to('val must be an integer')
113+
assert_that(str(ex)).is_equal_to('val must be a positive integer')
103114

104-
def test_is_multiple_of_extension_failure_arg_not_integer():
115+
def test_is_multiple_of_extension_failure_negative_val():
116+
try:
117+
add_extension(is_multiple_of)
118+
assert_that(-24).is_multiple_of(6)
119+
fail('should have raised error')
120+
except TypeError as ex:
121+
assert_that(str(ex)).is_equal_to('val must be a positive integer')
122+
123+
def test_is_multiple_of_extension_failure_bad_arg():
105124
try:
106125
assert_that(24).is_multiple_of('foo')
107126
fail('should have raised error')
108127
except TypeError as ex:
109-
assert_that(str(ex)).is_equal_to('given arg must be an integer')
128+
assert_that(str(ex)).is_equal_to('given arg must be a positive integer')
129+
130+
def test_is_multiple_of_extension_failure_negative_arg():
131+
try:
132+
add_extension(is_multiple_of)
133+
assert_that(24).is_multiple_of(-6)
134+
fail('should have raised error')
135+
except TypeError as ex:
136+
assert_that(str(ex)).is_equal_to('given arg must be a positive integer')
137+
138+
def test_call_missing_extension():
139+
def is_missing(): pass
140+
try:
141+
remove_extension(is_even)
142+
remove_extension(is_multiple_of)
143+
remove_extension(is_missing)
144+
assert_that(24).is_multiple_of(6)
145+
fail('should have raised error')
146+
except AttributeError as ex:
147+
assert_that(str(ex)).is_equal_to('assertpy has no assertion <is_multiple_of()>')
148+
149+
def test_remove_bad_extension():
150+
def is_missing(): pass
151+
try:
152+
remove_extension('foo')
153+
fail('should have raised error')
154+
except TypeError as ex:
155+
assert_that(str(ex)).is_equal_to('func must be callable')
156+
157+
def is_foo(self):
158+
if self.val != 'foo':
159+
self._err('Expected <%s> to be foo, but was not.' % (self.val))
160+
return self
161+
162+
def dupe1():
163+
add_extension(is_foo)
164+
assert_that('foo').is_foo()
165+
try:
166+
assert_that('FOO').is_foo()
167+
fail('should have raised error')
168+
except AssertionError as ex:
169+
assert_that(str(ex)).is_equal_to('Expected <FOO> to be foo, but was not.')
170+
171+
def dupe2():
172+
def is_foo(self):
173+
if self.val != 'FOO':
174+
self._err('Expected <%s> to be FOO, but was not.' % (self.val))
175+
return self
176+
177+
add_extension(is_foo)
178+
assert_that('FOO').is_foo()
179+
try:
180+
assert_that('foo').is_foo()
181+
fail('should have raised error')
182+
except AssertionError as ex:
183+
assert_that(str(ex)).is_equal_to('Expected <foo> to be FOO, but was not.')
184+
185+
def test_dupe_extensions():
186+
dupe1()
187+
dupe2()
188+
dupe1()

0 commit comments

Comments
 (0)