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_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_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 _enumeration_value(cls, x, **kwargs): a = first(enum(int, start=x, max_enum=1)) b = first(enum(float, start=x, max_enum=1)) return cls(a, b)
def __call__(self, *args): from .function import Function names = enum(str, max_enum=len(args)) func = Function(self.name, [Variable(name) for name in names], [], placeholder=True) return func(*args)