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)
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)
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)
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))
def test_string(): s = u'abc' t = Type.dispatch(s) assert isinstance(t, String) if PY2: assert type(t) is Unicode examine_string(Unicode, s) else: assert type(t) is String examine_string(String, s) assert hashable(s) == t.hashable() == s assert is_hashable(s) assert is_hashable(hashable(s)) assert find_ne('abc', 'abcd') == DifferentLength('abc', 'abcd') assert find_ne('abcd', 'abc') == DifferentLength('abcd', 'abc') assert find_ne('abc', 'abd') == DiffersAtIndex('abc', 'abd', 2) for cls in subclasses(String, [String]): if cls.type is None or cls is Basestring: continue for k in xrange(SAMPLES): val = generate(cls.type) with on_error(elog, examine_string, (cls, val)): examine_string(cls, val) x = 0 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 x += 100 assert is_unique(buf) # estr potential edge cases cases = [ "abc'de\r7fghi", "\x00", "\\", "\'", '\"', '\a', '\b', '\t', '\v', u'\u2013', '\\u2' ] for case in cases: assert eval(estr(case)) == case
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)
def test_string(): s = u'abc' t = Type.dispatch(s) assert isinstance(t, String) if PY2: assert type(t) is Unicode examine_string(Unicode, s) else: assert type(t) is String examine_string(String, s) assert hashable(s) == t.hashable() == s assert is_hashable(s) assert is_hashable(hashable(s)) assert find_ne('abc', 'abcd') == DifferentLength('abc', 'abcd') assert find_ne('abcd', 'abc') == DifferentLength('abcd', 'abc') assert find_ne('abc', 'abd') == DiffersAtIndex('abc', 'abd', 2) for cls in subclasses(String, [String]): if cls.type is None or cls is Basestring: continue for k in xrange(SAMPLES): val = generate(cls.type) with on_error(elog, examine_string, (cls, val)): examine_string(cls, val) x = 0 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 x += 100 assert is_unique(buf) # estr potential edge cases cases = ["abc'de\r7fghi", "\x00", "\\", "\'", '\"', '\a', '\b', '\t', '\v', u'\u2013', '\\u2'] for case in cases: assert eval(estr(case)) == case
def test_type(): t = Type(1) assert t.obj == 1 assert t.rstr() == '1' assert t.hashable() is t.obj class Foo(object): __hash__ = None f = Foo() f.a = 1 g = Foo() g.a = 2 assert not is_hashable(f) assert is_hashable(hashable(f)) assert_inequivalent(hashable(f), hashable(g)) dct = serialize(f) assert dct[SER_KEYS.attrs]['a'] == 1 assert t.find_ne(0) == NotEqual(1, 0) assert list(t.visit(0)) == [1] assert t.visit_len() == 1 t = Type.type_dispatch(Foo) assert_raises(NotImplementedError, t._enumeration_value, 1) assert_raises(NotImplementedError, t._generate) assert TYPE_REGISTRY[object] is Type assert isinstance(find_ne(1, 1.2), DifferentTypes)
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)
def test_mapping(): d = dict(a = 1, b = 2.3) t = Type.dispatch(d) assert isinstance(t, Mapping) assert type(t) is Dict if PY2: assert set(hashable(d)) == set(t.hashable()) == \ {'__builtin__.dict', ('a', 1), ('b', 2.3)} else: assert set(hashable(d)) == set(t.hashable()) == \ {'builtins.dict', ('a', 1), ('b', 2.3)} d1 = dict(a=1, b=2) d2 = dict(a=1, b=2, c=3) d3 = dict(a=1, b=3) assert find_ne(d1, d2) == KeyDifferences(d1, d2) assert find_ne(d2, d1) == KeyDifferences(d2, d1) assert find_ne(d1, d3) == DiffersAtKey(d1, d3, 'b') e1 = eval(estr(d1)) assert_equivalent(e1, d1) assert not is_hashable(d) assert is_hashable(hashable(d)) examine_mapping(Dict, d) for cls in subclasses(Mapping): for k in xrange(SAMPLES): val = cls.generate() with on_error(elog, examine_mapping, (cls, val)): hangwatch(1, examine_mapping, 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) d = dict(a=1, b=[1, 2, (3, 4)]) assert primitive_form(d) == dict(a=1, b=[1, 2, [3, 4]]) assert collect(d) == primitive_form(d)
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
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
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
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
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
def test_int(): lst = list(enumerate_(int, max_enum=5)) assert lst == [0, 1, 2, 3, 4] lst = list(enumerate_(int, max_enum=5, start=1, step=3)) assert lst == [1, 4, 7, 10, 13] assert rstr(1) == estr(1) == '1' assert hashable(-1) == -1 gen = generate(int) assert isinstance(gen, int) assert find_ne(gen, gen+1) == NotEqual(gen, gen+1) assert serialize(-1) == -1 assert deserialize(-1) == -1
def test_bool(): lst = list(enumerate_(bool, max_enum=5)) assert lst == [False, True, False, True, False] lst = list(enumerate_(bool, max_enum=5, start=1)) assert lst == [True, False, True, False, True] assert rstr(True) == estr(True) == 'True' assert hashable(False) is False gen = generate(bool) assert gen is True or gen is False assert find_ne(gen, not gen) == NotEqual(gen, not gen) assert serialize(True) is True assert deserialize(True) is True
def test_complex(): lst = list(enumerate_(complex, max_enum=3)) assert collection_comp(lst, [0+0j, 0.1+0.05j, 0.2+0.1j], feq) c = 1+2j assert rstr(c) == estr(c) == '(1+2j)' assert eval(estr(c)) == c assert hashable(c) is c gen = generate(complex) assert isinstance(gen, complex) gen2 = 0+0j if gen != 0 else 1.0j assert find_ne(gen, gen2) == NotEqual(gen, gen2) assert_type_equivalent(deserialize(serialize(c)), c)
def test_float(): lst = list(enumerate_(float, max_enum=5)) assert collection_comp(lst, [0.0, 0.1, 0.2, 0.3, 0.4], feq) lst = list(enumerate_(float, max_enum=3, start=1, step=3, float_step=.2)) assert collection_comp(lst, [0.2, 0.8, 1.4], feq) assert rstr(1.1) == estr(1.1) == '1.1' assert hashable(1.1) == 1.1 gen = generate(float) assert isinstance(gen, float) gen2 = 0.0 if gen != 0 else 1.0 assert find_ne(gen, gen2) == NotEqual(gen, gen2) assert serialize(-1.1) == -1.1 assert deserialize(-1.1) == -1.1
def test_long(): if PY2: lst = list(enumerate_(long, max_enum=5)) assert lst == [0, 1, 2, 3, 4] x = long('1L') assert rstr(x) == '1' assert estr(x) == '1L' assert_equivalent(hashable(-x), -x) gen = generate(long) assert isinstance(gen, long) assert find_ne(gen, gen+1) == NotEqual(gen, gen+1) assert_type_equivalent(deserialize(serialize(x)), x) assert TYPE_REGISTRY[long] is Long assert Long in TYPE_REGISTRY.values() else: assert Long not in TYPE_REGISTRY.values()
def test_set(): s = frozenset([1, 2.3, 'abc']) t = Type.dispatch(s) assert isinstance(t, Set) assert type(t) is FrozenSet assert hashable(s) == t.hashable() == s assert is_hashable(s) assert is_hashable(hashable(s)) s1 = {1, 2, 3} s2 = {2, 3, 4} assert find_ne(s1, s2) == SetDifferences(s1, s2) e1 = eval(estr(s1)) assert_equivalent(e1, s1) examine_set(Set, set(s)) examine_set(FrozenSet, s) for cls in subclasses(Set, [Set]): for k in xrange(SAMPLES): val = cls.generate() with on_error(elog, examine_set, (cls, val)): hangwatch(1, examine_set, 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) s = {1, 2, (3, 4)} assert primitive_form(s) == [1, 2, [3, 4]] assert collect(s) == primitive_form(s)
def test_diffexplorer(): l1 = [1, 2, 3] l2 = [1, 2, 4] x = DiffExplorer(l1, l2) assert x.display() == u'A: 1\nB: 1' assert x.current_value == (1, 1) x.step() assert x.display() == u'A: 2\nB: 2' x.down() assert x.display() == u'A: 2\nB: 2' x.up() assert x.display() == u'A: 2\nB: 2' x.step() assert x.display() == u'A: 3\nB: 4' assert_raises(ExplorationError, x.step) x.step(-1) assert x.display() == u'A: 2\nB: 2' x.step() assert x.display() == u'A: 1\nB: 1' assert_raises(ExplorationError, x.step) x.reset() assert list(x.depth_first()) == [(l1, l2), (1, 1), (2, 2), (3, 4)] def last_lines(si): return si.getvalue().split('\n')[-3:-1] l1 = [1, [2, 3], [[4]]] l2 = [1, [2, 6], [[5]]] r = DiffExplorer(ValueExplorer(l1), ValueExplorer(l2)) with capture() as (out, err): r._eval('c') assert last_lines(out) == ['A: 1', 'B: 1'] r._eval('l') assert last_lines(out) == [ 'A: [1, [2, 3], [[4]]]', 'B: [1, [2, 6], [[5]]]' ] r._eval('n 2') r._eval('c') assert last_lines(out) == ['A: [[4]]', 'B: [[5]]'] r._eval('d 2') r._eval('c') assert last_lines(out) == ['A: 4', 'B: 5'] r._eval('u 2') r._eval('c') assert last_lines(out) == ['A: [[4]]', 'B: [[5]]'] r._eval('n -1') r._eval('c') assert last_lines(out) == ['A: [2, 3]', 'B: [2, 6]'] d1 = dict(a=1) d2 = dict(a=2) r = DiffExplorer(d1, d2) with capture() as (out, err): r._eval('c') assert last_lines(out) == ['A: 1', 'B: 2'] r._eval('l') assert last_lines(out) == ["A: {'a': 1}", "B: {'a': 2}"] class Bar(object): def __init__(self, a, b): self.a = a self.b = b b1 = Bar(1, [2, 3, 'abc']) b2 = Bar(1, [2, 3, 'adc']) accum = [] def fake_input(prompt): accum.append(1) if sum(accum) <= 1: return 'f' return 'q' r = find_ne(b1, b2) with assign(repl, 'raw_input', fake_input): r()
def test_diffexplorer(): l1 = [1, 2, 3] l2 = [1, 2, 4] x = DiffExplorer(l1, l2) assert x.display() == u'A: 1\nB: 1' assert x.current_value == (1, 1) x.step() assert x.display() == u'A: 2\nB: 2' x.down() assert x.display() == u'A: 2\nB: 2' x.up() assert x.display() == u'A: 2\nB: 2' x.step() assert x.display() == u'A: 3\nB: 4' assert_raises(ExplorationError, x.step) x.step(-1) assert x.display() == u'A: 2\nB: 2' x.step() assert x.display() == u'A: 1\nB: 1' assert_raises(ExplorationError, x.step) x.reset() assert list(x.depth_first()) == [(l1, l2), (1, 1), (2, 2), (3, 4)] def last_lines(si): return si.getvalue().split('\n')[-3:-1] l1 = [1, [2, 3], [[4]]] l2 = [1, [2, 6], [[5]]] r = DiffExplorer(ValueExplorer(l1), ValueExplorer(l2)) with capture() as (out, err): r._eval('c') assert last_lines(out) == ['A: 1', 'B: 1'] r._eval('l') assert last_lines(out) == ['A: [1, [2, 3], [[4]]]', 'B: [1, [2, 6], [[5]]]'] r._eval('n 2') r._eval('c') assert last_lines(out) == ['A: [[4]]', 'B: [[5]]'] r._eval('d 2') r._eval('c') assert last_lines(out) == ['A: 4', 'B: 5'] r._eval('u 2') r._eval('c') assert last_lines(out) == ['A: [[4]]', 'B: [[5]]'] r._eval('n -1') r._eval('c') assert last_lines(out) == ['A: [2, 3]', 'B: [2, 6]'] d1 = dict(a = 1) d2 = dict(a = 2) r = DiffExplorer(d1, d2) with capture() as (out, err): r._eval('c') assert last_lines(out) == ['A: 1', 'B: 2'] r._eval('l') assert last_lines(out) == ["A: {'a': 1}", "B: {'a': 2}"] class Bar(object): def __init__(self, a, b): self.a = a self.b = b b1 = Bar(1, [2, 3, 'abc']) b2 = Bar(1, [2, 3, 'adc']) accum = [] def fake_input(prompt): accum.append(1) if sum(accum) <= 1: return 'f' return 'q' r = find_ne(b1,b2) with assign(repl, 'raw_input', fake_input): r()