How to use deal - 10 common examples

To help you get started, we’ve selected a few deal examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github life4 / deal / tests.py View on Github external
def test_init(self):
        with self.subTest(text='init has not raised any exceptions'):
            func = deal.pre(lambda x: x > 0)

        with self.subTest(text='validator'):
            func = deal.pre(lambda x: x > 0)(lambda x: x)
            with pytest.raises(deal.PreContractError):
                func(-2)

        with self.subTest(text='message'):
            func = deal.pre(lambda x: x > 0, message='TEST')(lambda x: x)
            try:
                func(-2)
            except AssertionError as e:
                self.assertEqual(e.args[0], 'TEST')

        with self.subTest(text='exception'):
            func = deal.pre(lambda x: x > 0, exception=NameError)(lambda x: x)
            with pytest.raises(NameError):
                func(-2)

        with self.subTest(text='exception with name'):
            func = deal.pre(lambda x: x > 0, exception=NameError('TEST'))(lambda x: x)
            with self.subTest(text='exception/exception'):
                with pytest.raises(NameError):
                    func(-2)
            with self.subTest(text='exception/message'):
github life4 / deal / tests / test_linter / test_rules.py View on Github external
def test_check_raises_inherited():
    checker = CheckRaises()
    text = """
    @deal.raises(LookupError)
    def test(a):
        raise KeyError
        raise ValueError
    """
    text = dedent(text).strip()
    funcs1 = Func.from_ast(ast.parse(text))
    funcs2 = Func.from_astroid(astroid.parse(text))
    for func in (funcs1[0], funcs2[0]):
        actual = [tuple(err) for err in checker(func)]
        expected = [(4, 10, 'DEAL021 raises contract error (ValueError)')]
        assert actual == expected
github life4 / deal / tests / test_linter / test_rules.py View on Github external
def test_check_pure_no_returns():
    checker = CheckPure()
    text = """
    @deal.pure
    def test(a):
        a + 3
    """
    text = dedent(text).strip()
    funcs1 = Func.from_ast(ast.parse(text))
    funcs2 = Func.from_astroid(astroid.parse(text))
    for func in (funcs1[0], funcs2[0]):
        actual = [tuple(err) for err in checker(func)]
        assert len(actual) == 1
        expected = 'DEAL023 pure contract error (no return)'
        assert actual[0][2] == expected
github life4 / deal / tests / test_linter / test_rules.py View on Github external
def test_check_returns_with_message():
    checker = CheckReturns()
    text = """
    @deal.post(lambda x: x > 0 or 'oh no!')
    def test(a):
        if a:
            return 1
        else:
            return -1
    """
    text = dedent(text).strip()
    funcs1 = Func.from_ast(ast.parse(text))
    funcs2 = Func.from_astroid(astroid.parse(text))
    for func in (funcs1[0], funcs2[0]):
        actual = [tuple(err) for err in checker(func)]
        expected = [(6, 15, 'DEAL012 oh no! (-1)')]
        assert actual == expected
github life4 / deal / tests / test_linter / test_rules.py View on Github external
def test_check_pure_no_returns():
    checker = CheckPure()
    text = """
    @deal.pure
    def test(a):
        a + 3
    """
    text = dedent(text).strip()
    funcs1 = Func.from_ast(ast.parse(text))
    funcs2 = Func.from_astroid(astroid.parse(text))
    for func in (funcs1[0], funcs2[0]):
        actual = [tuple(err) for err in checker(func)]
        assert len(actual) == 1
        expected = 'DEAL023 pure contract error (no return)'
        assert actual[0][2] == expected
github life4 / deal / tests / test_linter / test_rules.py View on Github external
def test_check_returns_ok_unresolved():
    checker = CheckReturns()
    text = """
    @deal.post(unknown)
    def test(a):
        return 1
    """
    text = dedent(text).strip()
    funcs1 = Func.from_ast(ast.parse(text))
    funcs2 = Func.from_astroid(astroid.parse(text))
    for func in (funcs1[0], funcs2[0]):
        actual = tuple(checker(func))
        assert not actual
github life4 / deal / tests / test_linter / test_rules.py View on Github external
def test_check_returns():
    checker = CheckReturns()
    text = """
    @deal.post(lambda x: x > 0)
    def test(a):
        if a:
            return 1
        else:
            return -1
    """
    text = dedent(text).strip()
    funcs1 = Func.from_ast(ast.parse(text))
    funcs2 = Func.from_astroid(astroid.parse(text))
    for func in (funcs1[0], funcs2[0]):
        actual = [tuple(err) for err in checker(func)]
        expected = [(6, 15, 'DEAL012 post contract error (-1)')]
        assert actual == expected
github life4 / deal / tests / test_schemes.py View on Github external
    @deal.pre(scheme, message='old message')
    def func(name):
        return name * 2
github life4 / deal / tests.py View on Github external
        @deal.pre(lambda name: name != 'Oleg')
        def func(name):
            return name * 2
github life4 / deal / tests / test_decorators / test_pre.py View on Github external
    @deal.pre(lambda x: x > 0 or 'new message', message='old message')
    def double(x):
        return x * 2