Skip to content

Commit

Permalink
add tests of is_multiple_of() extension
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnboy committed Oct 22, 2019
1 parent b53d265 commit 97a01a8
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,86 @@
from assertpy import assert_that, add_extension
import numbers


def is_even(self):
if isinstance(self.val, numbers.Integral) is False:
raise TypeError('val is not integer number')
raise TypeError('val must be an integer')
if self.val % 2 != 0:
self._err('Expected <%s> to be even, but was not.' % (self.val))
return self

def is_multiple_of(self, other):
if isinstance(self.val, numbers.Integral) is False:
raise TypeError('val must be an integer')
if isinstance(other, numbers.Integral) is False:
raise TypeError('given arg must be an integer')
_, rem = divmod(self.val, other)
if rem != 0:
self._err('Expected <%s> to be multiple of <%s>, but was not.' % (self.val, other))
return self



def test_extension():
def test_is_even_extension():
add_extension(is_even)
assert_that(124).is_even()
assert_that(124).is_type_of(int).is_even().is_greater_than(123).is_less_than(125).is_equal_to(124)
assert_that(12345678901234567890).is_even()

def test_extension_failure():
def test_is_even_extension_failure():
try:
add_extension(is_even)
assert_that(123).is_even()
fail('should have raised error')
except AssertionError as ex:
assert_that(str(ex)).is_equal_to('Expected <123> to be even, but was not.')

def test_extension_failure_not_callable():
def test_is_even_extension_failure_not_callable():
try:
add_extension('foo')
fail('should have raised error')
except TypeError as ex:
assert_that(str(ex)).is_equal_to('func must be callable')

def test_extension_failure_not_integer():
def test_is_even_extension_failure_not_integer():
try:
add_extension(is_even)
assert_that(124.0).is_even()
fail('should have raised error')
except TypeError as ex:
assert_that(str(ex)).is_equal_to('val is not integer number')
assert_that(str(ex)).is_equal_to('val must be an integer')

def test_is_multiple_of_extension():
add_extension(is_multiple_of)
assert_that(24).is_multiple_of(1)
assert_that(24).is_multiple_of(2)
assert_that(24).is_multiple_of(3)
assert_that(24).is_multiple_of(4)
assert_that(24).is_multiple_of(6)
assert_that(24).is_multiple_of(8)
assert_that(24).is_multiple_of(12)
assert_that(24).is_multiple_of(24)
assert_that(124).is_type_of(int).is_even().is_multiple_of(31).is_equal_to(124)

def test_is_multiple_of_extension_failure():
try:
add_extension(is_multiple_of)
assert_that(24).is_multiple_of(5)
fail('should have raised error')
except AssertionError as ex:
assert_that(str(ex)).is_equal_to('Expected <24> to be multiple of <5>, but was not.')

def test_is_multiple_of_extension_failure_not_integer():
try:
add_extension(is_multiple_of)
assert_that(24.0).is_multiple_of(5)
fail('should have raised error')
except TypeError as ex:
assert_that(str(ex)).is_equal_to('val must be an integer')

def test_is_multiple_of_extension_failure_arg_not_integer():
try:
add_extension(is_multiple_of)
assert_that(24).is_multiple_of('foo')
fail('should have raised error')
except TypeError as ex:
assert_that(str(ex)).is_equal_to('given arg must be an integer')

0 comments on commit 97a01a8

Please sign in to comment.