Ejemplo n.º 1
0
def test_raises_expects_function_to_raise_error():
    func = deal.raises(ZeroDivisionError)(lambda x: 1 / x)
    with pytest.raises(ZeroDivisionError):
        func(0)
    func(2)

    func = deal.raises(KeyError)(lambda x: 1 / x)
    with pytest.raises(deal.RaisesContractError):
        func(0)
Ejemplo n.º 2
0
    def test_main(self):
        func = deal.raises(ZeroDivisionError)(lambda x: 1 / x)
        with self.subTest(text='good'):
            with self.assertRaises(ZeroDivisionError):
                func(0)
        with self.subTest(text='good'):
            func(2)

        func = deal.raises(KeyError)(lambda x: 1 / x)
        with self.subTest(text='error'):
            with self.assertRaises(deal.RaisesContractError):
                func(0)
Ejemplo n.º 3
0
    # result is an index of items
    deal.post(lambda result: result >= 0),
    deal.ensure(lambda items, item, result: result < len(items)),
    # element at this position matches item
    deal.ensure(
        lambda items, item, result: items[result] == item,
        message='invalid match',
    ),
    # element at this position is the first match
    deal.ensure(
        lambda items, item, result: not any(el == item
                                            for el in items[:result]),
        message='not the first match',
    ),
    # LookupError will be raised if no elements found
    deal.raises(LookupError),
    deal.reason(LookupError, lambda items, item: item not in items),
    # no side-effects
    deal.has(),
)


@contract_for_index_of
def index_of(items: List[int], item: int) -> int:
    for index, el in enumerate(items):
        if el == item:
            return index
    raise LookupError


@pytest.mark.parametrize('case', deal.cases(index_of))