コード例 #1
0
ファイル: test_map.py プロジェクト: bluecoveltd/contracts
def test_map_basic_sanity():
    assert check_contracts(['map'], [{}]) == {}

    with raises(ContractNotRespected):
        check_contracts(['map'], [1])
    with raises(ContractNotRespected):
        check_contracts(['map'], [[]])

    assert syntax_fail('map[]')
    assert syntax_fail('map[]()')
    assert syntax_fail('map()')

    assert check_contracts(['map[1]'], [{1: 2}]) == {}
    assert check_contracts(['map[N],N<2'], [{1: 2}])

    with raises(ContractNotRespected):
        check_contracts(['map[N],N<2'], [{1: 2, 3: 4}])

    assert check_contracts(['map(int:int)'], [{1: 2}])

    with raises(ContractNotRespected):
        check_contracts(['map(int:int)'], [{'a': 2}])

    assert check_contracts(['map(*:int)'], [{1: 2}])
    assert check_contracts(['map(*:int)'], [{'a': 2}])

    # mapionary of string -> tuple, with tuple of two elements with different type
    assert check_contracts(['map(str:tuple(type(x),type(y))),x!=y'], [{
        'a': (2, 1.1)
    }])

    with raises(ContractNotRespected):
        check_contracts(['map(str:tuple(type(x),type(y))),x!=y'], [{
            'a': (2, 1)
        }])
コード例 #2
0
def test_multi_binding():
    assert check_contracts(['array[XxYx...],X=2,Y=4'], [a3d])

    assert syntax_fail('array[2x...x3]')
    assert syntax_fail('array[2x...x3]')

    assert check_contracts(['array[XxYx...],X=2,Y=4'], [a2d])
    assert check_contracts(['array[XxYx...],X=2,Y=4'], [a3d])
コード例 #3
0
ファイル: test_types.py プロジェクト: bluecoveltd/contracts
def test_types_type_func():
    syntax_fail('type')
    syntax_fail('type()')

    assert check_contracts(['type(x)'], [1])

    with raises(ContractNotRespected):
        check_contracts(['type(X)'], [1])
コード例 #4
0
ファイル: test_tuple.py プロジェクト: bluecoveltd/contracts
def test_nested_tuples():
    assert check_contracts(['tuple(x,tuple(*,*,x))'], [(1, (2, 3, 1))])

    with raises(ContractNotRespected):
        check_contracts(['tuple(x,tuple(*,*,x))'], [(1, (2, 3, 2))])

    assert check_contracts(['tuple(type(x),tuple(*,*,type(x)))'],
                           [(1, (2.1, 3.0, 3))])

    with raises(ContractNotRespected):
        check_contracts(['tuple(type(x),tuple(*,*,type(x)))'],
                        [(1, (2.1, 3.0, 3.1))])

    # cannot specify both, even if coherent
    syntax_fail('tuple[*](*,*)')
コード例 #5
0
def test_dicts_basic_sanity():
    assert check_contracts(['dict'], [{}]) == {}

    with raises(ContractNotRespected):
        check_contracts(['dict'], [1])
    with raises(ContractNotRespected):
        check_contracts(['dict'], [[]])

    syntax_fail('dict[]')
    syntax_fail('dict[]()')
    syntax_fail('dict()')

    assert check_contracts(['dict[1]'], [{1: 2}]) == {}
    assert check_contracts(['dict[N],N<2'], [{1: 2}])

    with raises(ContractNotRespected):
        check_contracts(['dict[N],N<2'], [{1: 2, 3: 4}])

    assert check_contracts(['dict(int:int)'], [{1: 2}])

    with raises(ContractNotRespected):
        check_contracts(['dict(int:int)'], [{'a': 2}])

    assert check_contracts(['dict(*:int)'], [{1: 2}])
    assert check_contracts(['dict(*:int)'], [{'a': 2}])
コード例 #6
0
def test_extensions_failure_cases():
    # needs to fail parsing because we didn't provide argument
    syntax_fail('ext1_lessthan')
    # needs to fail parsing because the argument name is wrong
    syntax_fail('ext1_lessthan(th=0)')
    # too many arguments
    syntax_fail('ext1_lessthan(0,1)')
コード例 #7
0
def test_file_basic_sanity():
    assert check_contracts(['file'], [io.IOBase()]) == {}

    with raises(ContractNotRespected):
        check_contracts(['file'], [1])
    with raises(ContractNotRespected):
        check_contracts(['file'], [[]])

    syntax_fail('file[]')
    syntax_fail('file[]()')
    syntax_fail('file()')
コード例 #8
0
ファイル: test_attr.py プロジェクト: bluecoveltd/contracts
def test_attr_basic_sanity():
    tc_a = A()
    tc_b = B()

    assert syntax_fail('attr')  # need at least some attribute

    assert check_contracts(['attr(a:*)'], [tc_a]) == {}
    assert check_contracts(['attr(a:int)'], [tc_a])
    assert check_contracts(['attr(b:int)'], [tc_a])
    assert check_contracts(['attr(b:>1)'], [tc_a]) == {}
    assert check_contracts(['attr(b:int,>1)'], [tc_a])
    with raises(ContractNotRespected):
        check_contracts(['attr(b:int,<=1)'], [tc_a])

    assert check_contracts(['attr(a:*)'], [tc_b]) == {}
    with raises(ContractNotRespected):
        check_contracts(['attr(b:*)'], [tc_b])
コード例 #9
0
def test_strings_basic_sanity():
    assert check_contracts(['str'], ['ciao']) == {}
    assert check_contracts(['string'], ['ciao']) == {}

    # Check sequences of chars are not str
    with raises(ContractNotRespected):
        check_contracts(['str'], [list('ciao')])

    # Can specify the length
    assert check_contracts(['str[N]'], [''])
    assert check_contracts(['str[1]'], ['a']) == {}
    assert check_contracts(['str[2]'], ['ab']) == {}
    assert check_contracts(['str[>0]'], ['ab']) == {}

    with raises(ContractNotRespected):
        check_contracts(['str[>0]'], [''])

    assert check_contracts(['str[N],N>3'], ['ciao'])

    with raises(ContractNotRespected):
        check_contracts(['str[N],N>3'], ['cia'])

    assert syntax_fail('str(*)')
コード例 #10
0
def test_syntax_equals_plus():
    assert syntax_fail('=1+')
コード例 #11
0
def test_syntax_equals_mul():
    assert syntax_fail('=1*')
コード例 #12
0
def test_syntax_equals_minus():
    assert syntax_fail('=1-')