Last active
July 16, 2017 12:05
-
-
Save mackwic/b99a01a7c5d9a131f94ed50fb3d07831 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contracts import contract, invariant, precondition, postcondition | |
import pytest | |
@precondition(lambda ctx: ctx[0] == 1) # positional arguments | |
@precondition(lambda ctx: ctx['two'] == 2) # named arguments | |
@precondition(lambda ctx: ctx['three'] == 3, 'Onoes !') # with a message | |
@postcondition(lambda ctx: ctx['return'] == 1 + 2 + 3) # return | |
def plop(one, two, three): | |
return one + two + three | |
plop(1, two=2, three=3) | |
plop(1, two=3, three=3) | |
# ^^^^^^^^^^^ | |
# Traceback (most recent call last): | |
# File "test.py", line 15, in <module> | |
# plop(1, two=3, three=3) | |
# File "/Users/thomas/projects/tradebot/app/dummybot/contracts.py", line 46, in inner | |
# res = func(*args, **kwargs) | |
# File "/Users/thomas/projects/tradebot/app/dummybot/contracts.py", line 44, in inner | |
# assert pre(ctx), "Pre-condition failed: {}".format(get_message(pre, pre_msg)) | |
# AssertionError: Pre-condition failed: @precondition(lambda ctx: ctx['two'] == 2) # named arguments | |
plop(1, two=2, three=4) | |
# ^^^^^^^^^^^^^^^^^^^^^ | |
# Traceback (most recent call last): | |
# File "test.py", line 15, in <module> | |
# plop(1, two=2, three=4) | |
# File "/Users/thomas/projects/tradebot/app/dummybot/contracts.py", line 46, in inner | |
# res = func(*args, **kwargs) | |
# File "/Users/thomas/projects/tradebot/app/dummybot/contracts.py", line 46, in inner | |
# res = func(*args, **kwargs) | |
# File "/Users/thomas/projects/tradebot/app/dummybot/contracts.py", line 44, in inner | |
# assert pre(ctx), "Pre-condition failed: {}".format(get_message(pre, pre_msg)) | |
# AssertionError: Pre-condition failed: Onoes ! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment