Skip to content

Commit

Permalink
refactor maybe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemyslaw Jan Pietrzak committed Jan 10, 2018
1 parent a2a555c commit a7eb0c5
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions tests/test_maybe.py
Expand Up @@ -13,38 +13,61 @@
from hypothesis import given
from hypothesis.strategies import integers

import pytest

def wrong_mapper(_):
assert True is False

class MaybeSpy:

def test_maybe_eq_operator_shuld_compare_values():
def mapper(self, value):
return value + 1

assert Maybe.just(42) == Maybe.just(42)
def binder(self, value):
return Maybe.just(value + 1)


@pytest.fixture()
def maybe_spy(mocker):
spy = MaybeSpy()
mocker.spy(spy, 'mapper')
mocker.spy(spy, 'binder')

return spy


@given(integers())
def test_maybe_eq_operator_should_compare_values(integer):

assert Maybe.just(integer) == Maybe.just(integer)
assert Maybe.just(None) == Maybe.just(None)

assert Maybe.just(42) != Maybe.nothing()
assert Maybe.just(integer) != Maybe.nothing()


def test_maybe_map_operator_should_be_applied_only_on_just_value():
@given(integers())
def test_maybe_map_operator_should_be_applied_only_on_just_value(integer):
assert Maybe.just(42).map(increase) == Maybe.just(43)
assert Maybe.nothing().map(increase) == Maybe.nothing()


def test_maybe_map_should_not_call_mapper_when_monad_has_nothing():
Maybe.nothing().map(wrong_mapper)
def test_maybe_map_should_not_call_mapper_when_monad_has_nothing(maybe_spy):
Maybe.nothing().map(maybe_spy.binder)

assert maybe_spy.binder.call_count == 0


def test_maybe_bind_should_retrun_result_of_mapper_called_with_maybe_value():
def test_maybe_bind_should_retrun_result_of_mapper_called_with_maybe_value(maybe_spy):
assert Maybe.just(42).bind(increase) == 43


def test_maybe_bind_should_not_call_mapper_when_monad_has_nothing():
Maybe.nothing().bind(wrong_mapper)
def test_maybe_bind_should_not_call_mapper_when_monad_has_nothing(maybe_spy):
Maybe.nothing().bind(maybe_spy.binder)

assert maybe_spy.binder.call_count == 0

def test_maybe_get_or_else_method_should_return_maybe_value_when_monad_is_not_empty():
assert Maybe.just(42).get_or_else(0) == 42

@given(integers())
def test_maybe_get_or_else_method_should_return_maybe_value_when_monad_is_not_empty(integer):
assert Maybe.just(integer).get_or_else(0) is integer


@given(integers())
Expand Down

0 comments on commit a7eb0c5

Please sign in to comment.