Example #1
0
def test_multiple_basic_sanity():
    assert check_contracts(['*'], [0]) == {}
    assert check_contracts(['*', '*'], [0, 1]) == {}
    assert check_contracts(['=0', '=1'], [0, 1]) == {}

    with raises(ContractNotRespected):
        check_contracts(['=0', '=1'], [0, 2])
def test_separate_context_order_of_types():
    # This fails because we have x=int,y=float followed by float,int
    with raises(ContractNotRespected):
        check_contracts(['dict(str:tuple(type(x),type(y))),x!=y'], [{
            'a': (2, 1.1),
            'b': (1.1, 2)
        }])
Example #3
0
def test_list_value_constraints():
    assert check_contracts(['list(int,>0)'], [[2, 1]])

    with raises(ContractNotRespected):
        check_contracts(['list(int,>0)'], [[0, 1]])

    assert check_contracts(['list(int,=0)'], [[0, 0]])
Example #4
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])
Example #5
0
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])
Example #6
0
def test_array_elements_boolean_or():
    arr124 = numpy.array([1, 2, 4])
    arr125 = numpy.array([1, 2, 5])

    assert check_contracts(['array(=4|>=0,<=2)'], [arr124]) == {}
    assert check_contracts(['array(=5|>=0,<=2)'], [arr125]) == {}
    with raises(ContractNotRespected):
        check_contracts(['array(=4|>=2,<=0)'], [arr124])
Example #7
0
def test_tuple_multiple_args():
    assert check_contracts(['tuple(int,int)'], [(1, 2)])
    assert check_contracts(['tuple(int,float)'], [(1, 2.0)])

    with raises(ContractNotRespected):
        check_contracts(['tuple(float,float)'], [(1, 2.0)])

    assert check_contracts(['tuple(type(x),type(x))'], [(1, 2)])
Example #8
0
def test_attr_multiple_constraints():
    tc_a = A()

    assert check_contracts(['attr(a:int;b:int)'], [tc_a])
    assert check_contracts(['attr(a:int;b:int)'], [tc_a])
    assert check_contracts(['attr(a:int;b:int,>1)'], [tc_a])
    with raises(ContractNotRespected):
        check_contracts(['attr(a:int;b:int,<=1)'], [tc_a])
Example #9
0
def test_comparison_incorrect_types():
    assert check_contracts(['=1'], [1]) == {}

    # with raises(ContractNotRespected):
    #     check_contracts(['=1'], [1])
    # with raises(ContractNotRespected):
    #     check_contracts(['=0'], [0])
    with raises(AssertionError):
        check_contracts(['>0'], [])
Example #10
0
def test_array_elements_basic_sanity():
    arr01 = numpy.array([0, 1, 0, 1])
    arr012 = numpy.array([0, 1, 0, 2])

    assert check_contracts(['array(=0|=1|=2)'], [arr01]) == {}
    assert check_contracts(['array(=0|=1|=2)'], [arr012]) == {}
    assert check_contracts(['array(=0|=1)'], [arr01]) == {}
    with raises(ContractNotRespected):
        check_contracts(['array(=0|=1)'], [arr012])
Example #11
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()')
def test_separate_context_basic_sanity():
    # dictionary of string -> tuple, with tuple of two elements with different type
    # In this case, each value should have the same two types
    assert check_contracts(['dict(str:tuple(type(x),type(y))),x!=y'], [{
        'a': (2, 1.1)
    }])

    with raises(ContractNotRespected):
        check_contracts(['dict(str:tuple(type(x),type(y))),x!=y'], [{
            'a': (2, 1)
        }])
def test_separate_context_no_match():
    # Here we force the context to not match using $(...)
    assert check_contracts(['dict(str:$(tuple(type(x),type(y)),x!=y))'], [{
        'a': (2, 1.1),
        'b': (1.1, 2)
    }]) == {}

    with raises(ContractNotRespected):
        check_contracts(['dict(str:$(tuple(type(x),type(y)),x!=y))'], [{
            'a': (2, 1)
        }])
Example #14
0
def test_isinstance_basic_sanity():
    class BaseClass():
        pass
    
    class SubClass(BaseClass):
        pass

    assert check_contracts(['isinstance(BaseClass)'], [BaseClass()]) == {}
    assert check_contracts(['isinstance(BaseClass)'], [SubClass()]) == {}
    assert check_contracts(['isinstance(SubClass)'], [SubClass()]) == {}

    with raises(ContractNotRespected):
        check_contracts(['isinstance(SubClass)'], [BaseClass()])
Example #15
0
def test_isinstance_object_base():
    class BaseClass2(object):
        pass
    
    class SubClass2(BaseClass2):
        pass
    
    assert check_contracts(['isinstance(BaseClass2)'], [BaseClass2()]) == {}
    assert check_contracts(['isinstance(BaseClass2)'], [SubClass2()]) == {}
    assert check_contracts(['isinstance(SubClass2)'], [SubClass2()]) == {}

    with raises(ContractNotRespected):
        check_contracts(['isinstance(SubClass2)'], [BaseClass2()])
Example #16
0
def test_isinstance_3_level_object():
    class BaseClass4(object):
        pass
    
    class MidClass4(BaseClass4):
        pass
    
    class SubClass4(MidClass4):
        pass
    
    assert check_contracts(['isinstance(BaseClass4)'], [BaseClass4()]) == {}
    assert check_contracts(['isinstance(BaseClass4)'], [SubClass4()]) == {}
    assert check_contracts(['isinstance(SubClass4)'], [SubClass4()]) == {}

    with raises(ContractNotRespected):
        check_contracts(['isinstance(SubClass4)'], [BaseClass4()])
Example #17
0
def check_contracts_fail(contract, value, error=ContractNotRespected):
    """ Returns the exception """
    if isinstance(contract, six.string_types):
        contract = [contract]
        value = [value]

    try:
        context = check_contracts(contract, value)

        msg = ('I was expecting that the values would not not'
               ' satisfy the contract.\n')

        for v in value:
            msg += '      value: %s\n' % describe_value(v)

        for c in contract:
            cp = parse_contract_string(c)
            msg += '   contract: %r, parsed as %r (%s)\n' % (c, cp, cp)

        msg += '    context:  %r\n' % context

        raise Exception(msg)

    except error as e:
        # Try generation of strings:
        s = "%r" % e  # @UnusedVariable
        s = "%s" % e  # @UnusedVariable
        return e
Example #18
0
def test_array_finite():
    assert check_contracts(['finite'], [1])
    assert check_contracts(['finite'], [0])
    assert check_contracts(['finite'], [-1])
    assert check_contracts(['finite'], [np.float(1)])
    with raises(ContractNotRespected):
        check_contracts(['finite'], [np.inf])
    with raises(ContractNotRespected):
        check_contracts(['finite'], [np.nan])
Example #19
0
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])
Example #20
0
def check_contracts_ok(contract, value):
    if isinstance(contract, six.string_types):
        contract = [contract]
        value = [value]
    context = check_contracts(contract, value)

    assert isinstance(context, dict)
    "%s" % context
    "%r" % context
Example #21
0
def test_lists_of_equal_length():
    assert check_contracts(['list[N]', 'list[N]'], [[4], [3]])
    assert check_contracts(['list[N]', 'list[N]'], [[], []])
    with raises(ContractNotRespected):
        check_contracts(['list[N]', 'list[N]'], [[], [1]])

    assert check_contracts(['list[N]', 'list[N],N>0'], [[1], [3]])

    # we can also refer to the other context
    assert check_contracts(['list[N]', 'list,N>0'], [[1], [3]])
    with raises(ContractNotRespected):
        check_contracts(['list[N]', 'list,N>0'], [[], [3]])
Example #22
0
def test_wildcard_basic_sanity():
    assert check_contracts(['*'], [0]) == {}
    assert check_contracts(['*'], [[1]]) == {}
    assert check_contracts(['*'], [None]) == {}

    with raises(ContractNotRespected):
        check_contracts(['#'], [None])

    assert check_contracts(['*|#'], [None]) == {}

    with raises(ContractNotRespected):
        check_contracts(['*,#'], [None])
Example #23
0
def test_sequences_equality():
    assert check_contracts(['seq[=1]'], [[0]]) == {}
    assert check_contracts(['seq[=2]'], [[0, 1]]) == {}

    with raises(ContractNotRespected):
        check_contracts(['seq[=2]'], [[0]])

    assert check_contracts(['seq[1]'], [[0]]) == {}
    assert check_contracts(['seq[2]'], [(0, 1)]) == {}

    with raises(ContractNotRespected):
        check_contracts(['seq[2]'], [(0,)])
Example #24
0
def test_compositions_multiple_consts_logical_or():
    assert check_contracts(['0|1|2'], [2]) == {}
    assert check_contracts(['0|1|2'], [1]) == {}

    assert check_contracts(['0|2'], [2]) == {}
    assert check_contracts(['0|1|2'], [2]) == {}
    assert check_contracts(['0|1|2|3'], [2]) == {}
    assert check_contracts(['0|1|2|3|4'], [2]) == {}
    assert check_contracts(['0|1|2|3|4|5'], [2]) == {}
Example #25
0
def test_values_of_the_same_type():
    assert check_contracts(['type(x)', 'type(x)'], [0, 1])
    assert check_contracts(['type(x)', 'type(x)'], [0.1, 1.1])
    with raises(ContractNotRespected):
        check_contracts(['type(x)', 'type(x)'], [0.1, 1])

    assert check_contracts(['type(x)', 'type(y)', 'type(x)|type(y)'],
                           [0, 1.1, 1.2])
    assert check_contracts(['type(x)', 'type(y)', 'type(x)|type(y)'],
                           [0, 1.1, 3])
    with raises(ContractNotRespected):
        check_contracts(['type(x)', 'type(y)', 'type(x)|type(y)'],
                        [0, 1.1, 'ciao'])
def test_constants_basic_sanity():
    assert check_contracts(['0'], [0]) == {}
    assert check_contracts(['1'], [1]) == {}

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

    assert check_contracts(['pi'], [math.pi]) == {}

    with raises(ContractNotRespected):
        check_contracts(['pi'], [math.pi * 2])
Example #27
0
def test_compositions_logical_not():
    with raises(ContractNotRespected):
        check_contracts(['!1'], [1])
    assert check_contracts(['!None'], [1])

    with raises(ContractNotRespected):
        check_contracts(['!(1|2)'], [1])
    assert check_contracts(['!(0|None)'], [3]) == {}
Example #28
0
def test_types_none():
    assert check_contracts(['None'], [None])
    assert check_contracts(['NoneType'], [None])

    with raises(ContractNotRespected):
        check_contracts(['None'], [1])
    with raises(ContractNotRespected):
        check_contracts(['NoneType'], [1])
Example #29
0
def test_list_parameterised_constraints():
    assert check_contracts(['list[N]'], [[]])
    assert check_contracts(['list[N],N>0'], [[1]])
    assert check_contracts(['list[N],N=1'], [[1]])
    assert check_contracts(['list[N],N>0,N<2'], [[1]])

    with raises(ContractNotRespected):
        check_contracts(['list[N],N>0'], [[]])
Example #30
0
def test_array_fixed_size():
    v1d = np.zeros(100)
    v2d = np.zeros((10, 10))
    assert check_contracts(['array[100]'], [v1d]) == {}
    with raises(ContractNotRespected):
        check_contracts(['array[100]'], [v2d])

    assert check_contracts(['array[10x10]'], [v2d]) == {}
    with raises(ContractNotRespected):
        check_contracts(['array[10x...]'], [v1d])

    assert check_contracts(['array[10x...]'], [v2d]) == {}