示例#1
0
def check_reconversion(s, exact):
    """ Checks that we can eval() the __repr__() value and we get
        an equivalent object. """
    parsed = parse_contract_string(s)

    s2 = parsed.__str__()
    reconv = parse_contract_string(s2)

    msg = 'Reparsing gives different objects:\n'
    msg += '  Original string: %r\n' % s
    msg += '           parsed: %r\n' % parsed
    msg += '      Regenerated: %r\n' % s2
    msg += '         reparsed: %r' % reconv

    assert reconv == parsed, msg

    if exact:
        # Warn if the string is not exactly the same.
        if s2 != s:
            msg = ('Slight different regenerated strings:\n')
            msg += ('   original: %s\n' % s)
            msg += ('  generated: %s\n' % s2)
            msg += ('   parsed the first time as: %r\n' % parsed)
            msg += ('                and then as: %r' % reconv)
            assert s2 == s, msg
def test_syntax_integer_arithmetic():
    # TODO : these `.check()` calls should return
    #        a boolean for testing purposes
    value = parse_contract_string('=2')
    value.check(2)

    with raises(ContractNotRespected):
        value.check(3)

    eq_one_plus_one = parse_contract_string('=1+1')
    eq_one_plus_one.check(2)

    with raises(ContractNotRespected):
        value.check(5)

    one_plus_one = parse_contract_string('1+1')
    one_plus_one.check(2)

    with raises(ContractNotRespected):
        value.check(10)

    eq_one_minus_one = parse_contract_string('=1-1')
    eq_one_minus_one.check(0)

    with raises(ContractNotRespected):
        value.check(0)

    one_minus_one = parse_contract_string('1-1')
    one_minus_one.check(0)

    with raises(ContractNotRespected):
        value.check(-1)
示例#3
0
    def __init__(self, name, map=None, meta=None, contract=None):
        if not isinstance(name, str):
            raise PyungoError(
                f"IO name must be str, however get name = {name} with type {type(name)}"
            )
        if '.' in name:
            raise PyungoError(
                f"IO can't have '.' in name, however get name = {name}")
        self._name = name

        self._meta = meta if meta is not None else {}
        self._value = None
        self._contract = None

        if map is not None:
            if not isinstance(map, str):
                raise PyungoError(
                    f"IO map must be str, however get map = {map} with type {type(map)}"
                )
            if '.' in map:
                raise PyungoError(
                    f"IO can't have '.' in map, however get map = {map}")
        self._map = map if map is not None else self._name

        if contract:
            try:
                from contracts.main import parse_contract_string
            except ImportError:
                raise ImportError('pycontracts is needed to use contracts')
            self._contract = parse_contract_string(contract)
示例#4
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
示例#5
0
文件: io.py 项目: ioancw/pyungo
 def __init__(self, name, meta=None, contract=None):
     self._name = name
     self._meta = meta if meta is not None else {}
     self._value = None
     self._contract = None
     if contract:
         try:
             from contracts.main import parse_contract_string
         except ImportError:
             raise ImportError('pycontracts is needed to use contracts')
         self._contract = parse_contract_string(contract)
示例#6
0
def syntax_fail(string):
    assert isinstance(string, six.string_types)

    try:
        parsed_contract = parse_contract_string(string)
        msg = 'I would not expect to parse %r.' % string
        msg += ' contract:         %s\n' % parsed_contract
        return False

    except ContractSyntaxError as e:
        # Try generation of strings:
        s = "%r" % e  # @UnusedVariable
        s = "%s" % e  # @UnusedVariable
        return True
示例#7
0
def check_good_repr(c):
    """ Checks that we can eval() the __repr__() value and we get
        an equivalent object. """
    parsed = parse_contract_string(c)
    # Check that it compares true with itself
    assert parsed.__eq__(parsed), 'Repr does not know itself: %r' % parsed

    reprc = parsed.__repr__()

    try:
        reeval = eval(reprc)
    except Exception as e:
        traceback.print_exc()
        raise Exception('Could not evaluate expression %r: %s' % (reprc, e))

    assert reeval == parsed, \
            'Repr gives different object:\n  %r !=\n  %r' % (parsed, reeval)
def test_unicode_literal():
    result = parse_contract_string(u'int')
    assert result == Extension('int')
示例#9
0
 def test_unicode_literal(self):
     r = parse_contract_string(u'int')
     print(r)
def test_syntax_floats():
    assert parse_contract_string('=%r' % 1.0)
    assert parse_contract_string('%r' % 1.0)
    assert parse_contract_string('=%r' % 1e10)
    assert parse_contract_string('%r' % 1e10)
def test_syntax_ints():
    assert parse_contract_string('=%r' % 1)
    assert parse_contract_string('%r' % 1)
    assert parse_contract_string('=%r' % -1)
    assert parse_contract_string('%r' % -1)