示例#1
0
def test_sequence():
    l = [1, 2.3, 'abc']
    t = Type.dispatch(l)
    assert isinstance(t, Sequence)
    assert type(t) is List
    if PY2:
        assert set(hashable(l)) == set(t.hashable()) == \
            {'__builtin__.list', 1, 2.3, 'abc'}
    else:
        assert set(hashable(l)) == set(t.hashable()) == \
            {'builtins.list', 1, 2.3, 'abc'}

    assert not is_hashable(l)
    assert is_hashable(hashable(l))

    l1 = [1, 2, 3]
    l2 = [1, 2, 3, 4]
    l3 = [1, 2, 4]
    assert find_ne(l1, l2) == DifferentLength(l1, l2)
    assert find_ne(l2, l1) == DifferentLength(l2, l1)
    assert find_ne(l1, l3) == DiffersAtIndex(l1, l3, 2)

    e1 = eval(estr(l1))
    assert_equivalent(e1, l1)

    tup = tuple(l)
    examine_sequence(List, l)
    examine_sequence(Tuple, tup)

    examine_sequence(Tuple, ([
        -1839677305294322342, b'', b'\x05l\xbf', b'0\xcfXp\xaa',
        -8468204163727415930
    ], True))

    for cls in subclasses(Sequence):
        for k in xrange(SAMPLES):
            val = cls.generate()
            with on_error(elog, examine_sequence, (cls, val)):
                hangwatch(1, examine_sequence, cls, val)

        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item

        assert is_unique(buf)

    assert list(visit(l, enumerate=True)) == [(0, 1), (1, 2.3), (2, 'abc')]
    assert list(visit([])) == []

    l = [1, 2, (3, 4)]
    assert primitive_form(l) == [1, 2, [3, 4]]
    assert collect(l) == primitive_form(l)
示例#2
0
def test_sequence():
    l = [1, 2.3, 'abc']
    t = Type.dispatch(l)
    assert isinstance(t, Sequence)
    assert type(t) is List
    if PY2:
        assert set(hashable(l)) == set(t.hashable()) == \
            {'__builtin__.list', 1, 2.3, 'abc'}
    else:
        assert set(hashable(l)) == set(t.hashable()) == \
            {'builtins.list', 1, 2.3, 'abc'}

    assert not is_hashable(l)
    assert is_hashable(hashable(l))

    l1 = [1, 2, 3]
    l2 = [1, 2, 3, 4]
    l3 = [1, 2, 4]
    assert find_ne(l1, l2) == DifferentLength(l1, l2)
    assert find_ne(l2, l1) == DifferentLength(l2, l1)
    assert find_ne(l1, l3) == DiffersAtIndex(l1, l3, 2)

    e1 = eval(estr(l1))
    assert_equivalent(e1, l1)

    tup = tuple(l)
    examine_sequence(List, l)
    examine_sequence(Tuple, tup)

    examine_sequence(Tuple, ([-1839677305294322342, b'', b'\x05l\xbf', 
                              b'0\xcfXp\xaa', -8468204163727415930], True))

    for cls in subclasses(Sequence):
        for k in xrange(SAMPLES):
            val = cls.generate()
            with on_error(elog, examine_sequence, (cls, val)):
                hangwatch(1, examine_sequence, cls, val)

        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item

        assert is_unique(buf)

    assert list(visit(l, enumerate=True)) == [(0, 1), (1, 2.3), (2, 'abc')]
    assert list(visit([])) == []

    l = [1, 2, (3, 4)]
    assert primitive_form(l) == [1, 2, [3, 4]]
    assert collect(l) == primitive_form(l)
示例#3
0
def test_none():
    n = None
    t = Type.dispatch(n)
    assert isinstance(t, NONE)
    assert type(t) is NONE

    assert t._find_ne(None, None) is None
    assert t._find_ne(1, None) is None

    assert hashable(n) is t.hashable() is n
    assert is_hashable(n)
    assert is_hashable(hashable(n))
    assert deserialize(serialize(n)) is n
    assert deserialize(serialize(type(None))) is type(None)
    assert primitive_form(None) is None

    val = NONE.generate()
    assert val is None
    assert rstr(n) == 'None'
    assert eval(estr(n)) is n

    for item in enumerate_(type(None), max_enum=10):
        assert item is None

    assert list(visit(None)) == [None]
    assert find_ne(None, None) is None
    assert isinstance(find_ne(None, 1), DifferentTypes)
示例#4
0
def test_none():
    n = None
    t = Type.dispatch(n)
    assert isinstance(t, NONE)
    assert type(t) is NONE

    assert t._find_ne(None, None) is None
    assert t._find_ne(1, None) is None

    assert hashable(n) is t.hashable() is n
    assert is_hashable(n)
    assert is_hashable(hashable(n))
    assert deserialize(serialize(n)) is n
    assert deserialize(serialize(type(None))) is type(None)
    assert primitive_form(None) is None

    val = NONE.generate()
    assert val is None
    assert rstr(n) == 'None'
    assert eval(estr(n)) is n

    for item in enumerate_(type(None), max_enum=10):
        assert item is None

    assert list(visit(None)) == [None]
    assert find_ne(None, None) is None
    assert isinstance(find_ne(None, 1), DifferentTypes)
示例#5
0
def examine_set(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val)
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == safe_sorted(list(val))
    assert find_ne(val, val) is None
示例#6
0
def examine_set(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val)
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == safe_sorted(list(val))
    assert find_ne(val, val) is None
示例#7
0
def examine_sequence(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val) or deep_feq(collect(sval, ss), collect(val, ss))
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == list(val)
    assert find_ne(val, val) is None
示例#8
0
def examine_sequence(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val) or deep_feq(collect(sval, ss), collect(val, ss))
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == list(val)
    assert find_ne(val, val) is None
示例#9
0
def examine_mapping(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val) or deep_feq(collect(sval, ss), collect(val, ss))
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == safe_sorted(list(val.items()))
    assert find_ne(val, val) is None
示例#10
0
def test_is_visit_primitive():
    from syn.types.a import is_visit_primitive

    class Foo(object):
        pass
    
    f = Foo()

    assert is_visit_primitive(1)
    assert is_visit_primitive(int)
    assert is_visit_primitive(Foo)
    assert not is_visit_primitive([1, 2, 3])
    assert is_visit_primitive('a')
    assert not is_visit_primitive('ab')

    assert is_visit_primitive(f)
    assert list(visit(f)) == [f]

    f.a = 1
    assert not is_visit_primitive(f)
    assert list(visit(f)) == [('a', 1)]
示例#11
0
def test_is_visit_primitive():
    from syn.types.a import is_visit_primitive

    class Foo(object):
        pass

    f = Foo()

    assert is_visit_primitive(1)
    assert is_visit_primitive(int)
    assert is_visit_primitive(Foo)
    assert not is_visit_primitive([1, 2, 3])
    assert is_visit_primitive('a')
    assert not is_visit_primitive('ab')

    assert is_visit_primitive(f)
    assert list(visit(f)) == [f]

    f.a = 1
    assert not is_visit_primitive(f)
    assert list(visit(f)) == [('a', 1)]
示例#12
0
def test_normal_type():
    b = Bar(1, 2.3)
    b2 = Bar(1, 2.4)
    b3 = Bar(2, 2.3)

    assert b != b2
    assert_equivalent(Bar(1, 2.3), Bar(1, 2.3))

    if PY3:
        assert not is_hashable(b)
    else:
        assert is_hashable(b)
    assert is_hashable(hashable(b))

    assert find_ne(b, b) is None
    assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b')
    assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a')

    # Is evaluable, but not correct, because we haven't given the
    # types system the proper information for this class
    e1 = eval(estr(b))
    assert b != e1

    assert list(visit(b)) == [('a', 1), ('b', 2.3)]
    assert attrs(b) == ['a', 'b']
    assert rstr(b).startswith('<{} object at '.format(get_fullname(Bar)))

    sval = deserialize(serialize(b))
    assert_equivalent(b, sval)
    assert deep_feq(b, sval)

    assert primitive_form(Bar) is Bar
    assert primitive_form(b) == dict(a=1, b=2.3)

    b4 = Bar(2, b2)
    assert primitive_form(b4) == dict(a=2, b=dict(a=1, b=2.4))

    assert collect(b4) == primitive_form(b4)

    def dothing(obj):
        if isinstance(obj, collections.Mapping):
            return safe_sorted(obj.values())
        return safe_sorted(obj)

    assert collect(b4, dothing) in [[2, [1, 2.4]], [2, [2.4, 1]], [[1, 2.4],
                                                                   2],
                                    [[2.4, 1], 2]]

    # Because the types system knows nothing of the Bar class
    assert_raises(NotImplementedError, generate, Bar)
    assert_raises(NotImplementedError, list, enum(Bar, max_enum=50))
示例#13
0
def test_normal_type():
    b = Bar(1, 2.3)
    b2 = Bar(1, 2.4)
    b3 = Bar(2, 2.3)

    assert b != b2
    assert_equivalent(Bar(1, 2.3), Bar(1, 2.3))

    if PY3:
        assert not is_hashable(b)
    else:
        assert is_hashable(b)
    assert is_hashable(hashable(b))

    assert find_ne(b, b) is None
    assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b')
    assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a')

    # Is evaluable, but not correct, because we haven't given the
    # types system the proper information for this class
    e1 = eval(estr(b))
    assert b != e1

    assert list(visit(b)) == [('a', 1), ('b', 2.3)]
    assert attrs(b) == ['a', 'b']
    assert rstr(b).startswith('<{} object at '.format(get_fullname(Bar)))

    sval =  deserialize(serialize(b))
    assert_equivalent(b, sval)
    assert deep_feq(b, sval)

    assert primitive_form(Bar) is Bar
    assert primitive_form(b) == dict(a=1, b=2.3)

    b4 = Bar(2, b2)
    assert primitive_form(b4) == dict(a=2, b=dict(a=1, b=2.4))

    assert collect(b4) == primitive_form(b4)
    
    def dothing(obj):
        if isinstance(obj, collections.Mapping):
            return safe_sorted(obj.values())
        return safe_sorted(obj)
    assert collect(b4, dothing) in [[2, [1, 2.4]],
                                    [2, [2.4, 1]],
                                    [[1, 2.4], 2],
                                    [[2.4, 1], 2]]

    # Because the types system knows nothing of the Bar class
    assert_raises(NotImplementedError, generate, Bar)
    assert_raises(NotImplementedError, list, enum(Bar, max_enum=50))
示例#14
0
def examine_numeric(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    assert deserialize(serialize(val)) == val
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)
    assert primitive_form(val) == val

    assert list(visit(val)) == [val]
    assert find_ne(val, val) is None

    eitem = eval(estr(val))
    assert cfeq(eitem, val, relative=True)
    assert type(eitem) is cls.type
示例#15
0
def examine_string(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    assert deserialize(serialize(val)) == val
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)
    assert primitive_form(val) == val

    assert list(visit(val)) == list(val)
    assert find_ne(val, val) is None

    eitem = eval(estr(val))
    assert eitem == val
    assert type(eitem) is cls.type
示例#16
0
def examine_string(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    assert deserialize(serialize(val)) == val
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)
    assert primitive_form(val) == val

    assert list(visit(val)) == list(val)
    assert find_ne(val, val) is None

    eitem = eval(estr(val))
    assert eitem == val
    assert type(eitem) is cls.type
示例#17
0
def examine_numeric(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    assert deserialize(serialize(val)) == val
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)
    assert primitive_form(val) == val

    assert list(visit(val)) == [val]
    assert find_ne(val, val) is None

    eitem = eval(estr(val))
    assert cfeq(eitem, val, relative=True)
    assert type(eitem) is cls.type
示例#18
0
def test_custom_type():
    b = Baz(1, 2.3)
    b2 = Baz(1, 2.4)
    b3 = Baz(2, 2.3)

    assert b != b2
    assert_equivalent(Baz(1, 2.3), Baz(1, 2.3))

    if PY3:
        assert not is_hashable(b)
    else:
        assert is_hashable(b)
    assert is_hashable(hashable(b))

    assert find_ne(b, b) is None
    assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b')
    assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a')

    e1 = eval(estr(b))
    assert_equivalent(e1, b)

    assert list(visit(b)) == [('a', 1), ('b', 2.3)]
    assert rstr(b) == 'Baz(1,2.3)'

    assert attrs(b) == ['a', 'b']

    sval = deserialize(serialize(b))
    assert_equivalent(sval, b)
    assert deep_feq(sval, b)

    assert Baz is deserialize(serialize(Baz))
    assert primitive_form(Baz) is Baz
    assert primitive_form(b) == dict(a=1, b=2.3)

    val = generate(Baz)
    assert type(val) is Baz
    assert isinstance(val.a, int)
    assert isinstance(val.b, float)

    buf = []
    last = None
    for item in enum(Baz, max_enum=SAMPLES * 10, step=100):
        assert type(item) is Baz
        assert item != last
        buf.append(item)
        last = item

    assert is_unique(buf)
示例#19
0
def test_custom_type():
    b = Baz(1, 2.3)
    b2 = Baz(1, 2.4)
    b3 = Baz(2, 2.3)

    assert b != b2
    assert_equivalent(Baz(1, 2.3), Baz(1, 2.3))

    if PY3:
        assert not is_hashable(b)
    else:
        assert is_hashable(b)
    assert is_hashable(hashable(b))

    assert find_ne(b, b) is None
    assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b')
    assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a')

    e1 = eval(estr(b))
    assert_equivalent(e1, b)

    assert list(visit(b)) == [('a', 1), ('b', 2.3)]
    assert rstr(b) == 'Baz(1,2.3)'

    assert attrs(b) == ['a', 'b']

    sval = deserialize(serialize(b))
    assert_equivalent(sval, b)
    assert deep_feq(sval, b)

    assert Baz is deserialize(serialize(Baz))
    assert primitive_form(Baz) is Baz
    assert primitive_form(b) == dict(a=1, b=2.3)

    val = generate(Baz)
    assert type(val) is Baz
    assert isinstance(val.a, int)
    assert isinstance(val.b, float)

    buf = []
    last = None
    for item in enum(Baz,  max_enum=SAMPLES * 10, step=100):
        assert type(item) is Baz
        assert item != last
        buf.append(item)
        last = item
        
    assert is_unique(buf)
示例#20
0
def test_custom_object():
    f = Foo(1, 1.2)
    f2 = Foo(1, 1.3)
    f3 = Foo(2, 1.2)

    assert f != f2
    assert_equivalent(Foo(1, 2.3), Foo(1, 2.3))

    assert not is_hashable(f)
    assert is_hashable(hashable(f))

    assert find_ne(f, f) is None
    assert find_ne(f, f2) == DiffersAtAttribute(f, f2, 'b')
    assert find_ne(f, f3) == DiffersAtAttribute(f, f3, 'a')

    e1 = eval(estr(f))
    assert_equivalent(e1, f)

    assert list(visit(f)) == [('a', 1), ('b', 1.2)]
    assert rstr(f) == 'Foo(1,1.2)'

    assert attrs(f) == ['a', 'b']
    assert pairs(f) == [('a', 1), ('b', 1.2)]

    sval = deserialize(serialize(f))
    assert_equivalent(sval, f)
    assert deep_feq(sval, f)

    assert Foo is deserialize(serialize(Foo))
    assert primitive_form(Foo) is Foo
    assert primitive_form(f) == dict(a=1, b=1.2)
    assert collect(f) == primitive_form(f)

    val = generate(Foo)
    assert type(val) is Foo

    buf = []
    last = None
    for item in enum(Foo, max_enum=SAMPLES * 10, step=100):
        assert type(item) is Foo
        assert item != last
        buf.append(item)
        last = item

    assert enumeration_value(Foo, 0) == first(enum(Foo, max_enum=1))
    assert is_unique(buf)
示例#21
0
def test_custom_object():
    f = Foo(1, 1.2)
    f2 = Foo(1, 1.3)
    f3 = Foo(2, 1.2)

    assert f != f2
    assert_equivalent(Foo(1, 2.3), Foo(1, 2.3))

    assert not is_hashable(f)
    assert is_hashable(hashable(f))

    assert find_ne(f, f) is None
    assert find_ne(f, f2) == DiffersAtAttribute(f, f2, 'b')
    assert find_ne(f, f3) == DiffersAtAttribute(f, f3, 'a')

    e1 = eval(estr(f))
    assert_equivalent(e1, f)

    assert list(visit(f)) == [('a', 1), ('b', 1.2)]
    assert rstr(f) == 'Foo(1,1.2)'

    assert attrs(f) == ['a', 'b']
    assert pairs(f) == [('a', 1), ('b', 1.2)]

    sval = deserialize(serialize(f))
    assert_equivalent(sval, f)
    assert deep_feq(sval, f)

    assert Foo is deserialize(serialize(Foo))
    assert primitive_form(Foo) is Foo
    assert primitive_form(f) == dict(a=1, b=1.2)
    assert collect(f) == primitive_form(f)

    val = generate(Foo)
    assert type(val) is Foo

    buf = []
    last = None
    for item in enum(Foo,  max_enum=SAMPLES * 10, step=100):
        assert type(item) is Foo
        assert item != last
        buf.append(item)
        last = item

    assert enumeration_value(Foo, 0) == first(enum(Foo, max_enum=1))
    assert is_unique(buf)