예제 #1
0
 def get_identifier(self, instance):
     if ISerializable.providedBy(instance):
         return instance.type_name, id(instance)
     return id(instance)
예제 #2
0
def assert_equal_but_different(
        value, expected, idx, valids, expids, strict=True):
    '''
    idx is used to identify every values uniquely to be able to verify
    references are made to the same value, valids and expids are
    dictionaries with instance id() for key and idx for value.
    Types and interfaces are assumed to be immutable atoms.'''

    # Only check references for type that can be referenced.
    # Let the immutable type do what they want, sometime strings
    # are interned sometime no, we don't care.
    basic_types = (int, long, float, bool, str, unicode, type(None))
    if not isinstance(expected, basic_types):
        # Get unique instance identifiers
        if not strict:
            value, expected = _get_resolved_dereference(value, expected)
        expid = id(expected)
        valid = id(value)

        if expid in expids:
            # Expected value is a reference, check the other value is too
            assert valid in valids
            # Check the two reference the same value inside the structure
            assert valids[valid] == expids[expid]
            return idx

        # Check the other value is not a reference if it wasn't expected
        assert valid not in valids

        # Store the instance identifiers for later checks
        expids[expid] = idx
        valids[valid] = idx

    if expected is None:
        assert expected == value
    elif isinstance(expected, (list, tuple)):
        if expected != ():  # Special case for tuple singleton
            assert value is not expected

        assert len(expected) == len(value)
        for exp, val in zip(expected, value):
            idx = assert_equal_but_different(
                val, exp, idx + 1, valids, expids, strict=strict)
    elif isinstance(expected, set):
        assert len(expected) == len(value)
        for exp in expected:
            assert exp in value
            val = [v for v in value if v == exp][0]
            idx = assert_equal_but_different(
                val, exp, idx + 1, valids, expids, strict=strict)
    elif isinstance(expected, dict):
        assert len(expected) == len(value)
        for exp_key, exp_val in expected.items():
            assert exp_key in value
            val_key = [k for k in value if k == exp_key][0]
            val_val = value[val_key]
            idx = assert_equal_but_different(
                val_key, exp_key, idx + 1, valids, expids, strict=strict)
            idx = assert_equal_but_different(
                val_val, exp_val, idx + 1, valids, expids, strict=strict)
    elif isinstance(value, float):
        assert round(value - expected, 7) == 0
    elif isinstance(value, (int, long, bool, str, unicode,
                            type, InterfaceClass, enum.Enum)):
        assert value == expected
    else:
        assert expected is not value
        if ISerializable.providedBy(expected):
            assert ISerializable.providedBy(value)
        idx = assert_equal_but_different(
            value.__dict__, expected.__dict__, idx + 1, valids, expids,
            strict=strict)
    return idx
예제 #3
0
 def get_identifier(self, instance):
     if ISerializable.providedBy(instance):
         return instance.type_name, id(instance)
     return id(instance)
예제 #4
0
def assert_equal_but_different(value,
                               expected,
                               idx,
                               valids,
                               expids,
                               strict=True):
    '''
    idx is used to identify every values uniquely to be able to verify
    references are made to the same value, valids and expids are
    dictionaries with instance id() for key and idx for value.
    Types and interfaces are assumed to be immutable atoms.'''

    # Only check references for type that can be referenced.
    # Let the immutable type do what they want, sometime strings
    # are interned sometime no, we don't care.
    basic_types = (int, long, float, bool, str, unicode, type(None))
    if not isinstance(expected, basic_types):
        # Get unique instance identifiers
        if not strict:
            value, expected = _get_resolved_dereference(value, expected)
        expid = id(expected)
        valid = id(value)

        if expid in expids:
            # Expected value is a reference, check the other value is too
            assert valid in valids
            # Check the two reference the same value inside the structure
            assert valids[valid] == expids[expid]
            return idx

        # Check the other value is not a reference if it wasn't expected
        assert valid not in valids

        # Store the instance identifiers for later checks
        expids[expid] = idx
        valids[valid] = idx

    if expected is None:
        assert expected == value
    elif isinstance(expected, (list, tuple)):
        if expected != ():  # Special case for tuple singleton
            assert value is not expected

        assert len(expected) == len(value)
        for exp, val in zip(expected, value):
            idx = assert_equal_but_different(val,
                                             exp,
                                             idx + 1,
                                             valids,
                                             expids,
                                             strict=strict)
    elif isinstance(expected, set):
        assert len(expected) == len(value)
        for exp in expected:
            assert exp in value
            val = [v for v in value if v == exp][0]
            idx = assert_equal_but_different(val,
                                             exp,
                                             idx + 1,
                                             valids,
                                             expids,
                                             strict=strict)
    elif isinstance(expected, dict):
        assert len(expected) == len(value)
        for exp_key, exp_val in expected.items():
            assert exp_key in value
            val_key = [k for k in value if k == exp_key][0]
            val_val = value[val_key]
            idx = assert_equal_but_different(val_key,
                                             exp_key,
                                             idx + 1,
                                             valids,
                                             expids,
                                             strict=strict)
            idx = assert_equal_but_different(val_val,
                                             exp_val,
                                             idx + 1,
                                             valids,
                                             expids,
                                             strict=strict)
    elif isinstance(value, float):
        assert round(value - expected, 7) == 0
    elif isinstance(
            value,
        (int, long, bool, str, unicode, type, InterfaceClass, enum.Enum)):
        assert value == expected
    else:
        assert expected is not value
        if ISerializable.providedBy(expected):
            assert ISerializable.providedBy(value)
        idx = assert_equal_but_different(value.__dict__,
                                         expected.__dict__,
                                         idx + 1,
                                         valids,
                                         expids,
                                         strict=strict)
    return idx