26
26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
27
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
28
29
- from assertpy import assert_that , add_extension
29
+ from assertpy import assert_that , add_extension , remove_extension , fail
30
30
import numbers
31
31
32
32
@@ -38,13 +38,24 @@ def is_even(self):
38
38
return self
39
39
40
40
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
45
51
_ , 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!
47
56
self ._err ('Expected <%s> to be multiple of <%s>, but was not.' % (self .val , other ))
57
+
58
+ # success, and return self to allow chaining
48
59
return self
49
60
50
61
add_extension (is_even )
@@ -94,16 +105,84 @@ def test_is_multiple_of_extension_failure():
94
105
except AssertionError as ex :
95
106
assert_that (str (ex )).is_equal_to ('Expected <24> to be multiple of <5>, but was not.' )
96
107
97
- def test_is_multiple_of_extension_failure_not_integer ():
108
+ def test_is_multiple_of_extension_failure_bad_val ():
98
109
try :
99
110
assert_that (24.0 ).is_multiple_of (5 )
100
111
fail ('should have raised error' )
101
112
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' )
103
114
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 ():
105
124
try :
106
125
assert_that (24 ).is_multiple_of ('foo' )
107
126
fail ('should have raised error' )
108
127
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