|
29 | 29 | from assertpy import assert_that, add_extension
|
30 | 30 | import numbers
|
31 | 31 |
|
| 32 | + |
32 | 33 | def is_even(self):
|
33 | 34 | if isinstance(self.val, numbers.Integral) is False:
|
34 |
| - raise TypeError('val is not integer number') |
| 35 | + raise TypeError('val must be an integer') |
35 | 36 | if self.val % 2 != 0:
|
36 | 37 | self._err('Expected <%s> to be even, but was not.' % (self.val))
|
37 | 38 | return self
|
38 | 39 |
|
| 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') |
| 45 | + _, rem = divmod(self.val, other) |
| 46 | + if rem != 0: |
| 47 | + self._err('Expected <%s> to be multiple of <%s>, but was not.' % (self.val, other)) |
| 48 | + return self |
| 49 | + |
| 50 | + |
39 | 51 |
|
40 |
| -def test_extension(): |
| 52 | +def test_is_even_extension(): |
41 | 53 | add_extension(is_even)
|
42 | 54 | assert_that(124).is_even()
|
43 | 55 | assert_that(124).is_type_of(int).is_even().is_greater_than(123).is_less_than(125).is_equal_to(124)
|
44 |
| - assert_that(12345678901234567890).is_even() |
45 | 56 |
|
46 |
| -def test_extension_failure(): |
| 57 | +def test_is_even_extension_failure(): |
47 | 58 | try:
|
48 | 59 | add_extension(is_even)
|
49 | 60 | assert_that(123).is_even()
|
50 | 61 | fail('should have raised error')
|
51 | 62 | except AssertionError as ex:
|
52 | 63 | assert_that(str(ex)).is_equal_to('Expected <123> to be even, but was not.')
|
53 | 64 |
|
54 |
| -def test_extension_failure_not_callable(): |
| 65 | +def test_is_even_extension_failure_not_callable(): |
55 | 66 | try:
|
56 | 67 | add_extension('foo')
|
57 | 68 | fail('should have raised error')
|
58 | 69 | except TypeError as ex:
|
59 | 70 | assert_that(str(ex)).is_equal_to('func must be callable')
|
60 | 71 |
|
61 |
| -def test_extension_failure_not_integer(): |
| 72 | +def test_is_even_extension_failure_not_integer(): |
62 | 73 | try:
|
63 | 74 | add_extension(is_even)
|
64 | 75 | assert_that(124.0).is_even()
|
65 | 76 | fail('should have raised error')
|
66 | 77 | except TypeError as ex:
|
67 |
| - assert_that(str(ex)).is_equal_to('val is not integer number') |
| 78 | + assert_that(str(ex)).is_equal_to('val must be an integer') |
| 79 | + |
| 80 | +def test_is_multiple_of_extension(): |
| 81 | + add_extension(is_multiple_of) |
| 82 | + assert_that(24).is_multiple_of(1) |
| 83 | + assert_that(24).is_multiple_of(2) |
| 84 | + assert_that(24).is_multiple_of(3) |
| 85 | + assert_that(24).is_multiple_of(4) |
| 86 | + assert_that(24).is_multiple_of(6) |
| 87 | + assert_that(24).is_multiple_of(8) |
| 88 | + assert_that(24).is_multiple_of(12) |
| 89 | + assert_that(24).is_multiple_of(24) |
| 90 | + assert_that(124).is_type_of(int).is_even().is_multiple_of(31).is_equal_to(124) |
| 91 | + |
| 92 | +def test_is_multiple_of_extension_failure(): |
| 93 | + try: |
| 94 | + add_extension(is_multiple_of) |
| 95 | + assert_that(24).is_multiple_of(5) |
| 96 | + fail('should have raised error') |
| 97 | + except AssertionError as ex: |
| 98 | + assert_that(str(ex)).is_equal_to('Expected <24> to be multiple of <5>, but was not.') |
| 99 | + |
| 100 | +def test_is_multiple_of_extension_failure_not_integer(): |
| 101 | + try: |
| 102 | + add_extension(is_multiple_of) |
| 103 | + assert_that(24.0).is_multiple_of(5) |
| 104 | + fail('should have raised error') |
| 105 | + except TypeError as ex: |
| 106 | + assert_that(str(ex)).is_equal_to('val must be an integer') |
| 107 | + |
| 108 | +def test_is_multiple_of_extension_failure_arg_not_integer(): |
| 109 | + try: |
| 110 | + add_extension(is_multiple_of) |
| 111 | + assert_that(24).is_multiple_of('foo') |
| 112 | + fail('should have raised error') |
| 113 | + except TypeError as ex: |
| 114 | + assert_that(str(ex)).is_equal_to('given arg must be an integer') |
0 commit comments