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 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_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_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_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_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_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_misc_serialization(): assert int is deserialize(serialize(int)) d = dict(a=1, b=2) dd = deserialize(d) assert_equivalent(d, dd) k = KWObject(a=1, b=2.3) assert k != KWObject(a=2, b=2.3) assert_equivalent(KWObject(a=1, b=2.3), KWObject(b=2.3, a=1)) sval = deserialize(serialize(k)) assert_equivalent(sval, k) assert deep_feq(sval, k) assert KWObject is deserialize(serialize(KWObject)) f = Foo2(1, b=2.3, c='abc') f2 = Foo2(1, b=2.4, c='abc') assert f != f2 sval = deserialize(serialize(f)) assert_equivalent(sval, f) assert deep_feq(sval, f) assert Foo2 is deserialize(serialize(Foo2)) n = NoSer() n.a = 1 sval = deserialize(serialize(n)) assert isinstance(sval, NoSer) assert n != sval assert vars(n) != vars(sval) assert n.a == 1 assert not hasattr(sval, 'a')
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_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_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_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_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 _serialize(self, dct, **kwargs): dct[SER_KEYS.args] = [serialize(self.a, **kwargs), serialize(self.b, **kwargs)] return dct
def _serialize(self, dct, **kwargs): dct[SER_KEYS.args] = [ serialize(self.a, **kwargs), serialize(self.b, **kwargs) ] return dct