def test_schema_attrs(): SchemaTest(SA1(), SA2(), a=1) assert_raises(TypeError, SchemaTest, SA1(), SA3(), a=2) for k in xrange(SAMPLES): val = generate(SchemaTest) assert type(val) is SchemaTest assert isinstance(val.a, int) assert type(val[0]) is SA1 assert type(val[1]) is SA2 assert len(val) == 2 def bad(): class SchemaTest2(Node): schema = Sequence(SA1, SA2) types = (SA1, SA1) assert_raises(TypeError, bad) ST2(SA1(), SA4(SA2())) SA4(SA3()).validate() assert_raises(TypeError, ST2, SA1(), SA4(SA3())) for k in xrange(SAMPLES): val = generate(ST2) assert type(val) is ST2 assert type(val[0]) is SA1 assert type(val[1]) is SA4 assert len(val) == 2
def test_schema_attrs(): SchemaTest(SA1(), SA2(), a=1) assert_raises(TypeError, SchemaTest, SA1(), SA3(), a=2) for k in xrange(SAMPLES): val = generate(SchemaTest) assert type(val) is SchemaTest assert isinstance(val.a, int) assert type(val[0]) is SA1 assert type(val[1]) is SA2 assert len(val) == 2 def bad(): class SchemaTest2(Node): schema = Sequence(SA1, SA2) types = (SA1, SA1) assert_raises(TypeError, bad) ST2(SA1(), SA4(SA2())) SA4(SA3()).validate() assert_raises(TypeError, ST2, SA1(), SA4(SA3())) for k in xrange(SAMPLES): val = generate(ST2) assert type(val) is ST2 assert type(val[0]) is SA1 assert type(val[1]) is SA4 assert len(val) == 2
def test_child_types(): CTTest(CT1()) assert_raises(TypeError, CTTest, CT1(), CT2()) for k in xrange(SAMPLES): val = generate(CTTest) val.validate()
def test_child_types(): CTTest(CT1()) assert_raises(TypeError, CTTest, CT1(), CT2()) for k in xrange(SAMPLES): val = generate(CTTest) val.validate()
def test_descendant_exclude(): DETest(DE1()) DETest(DE1(DE2(DE3()))) n = DE2(DE4()) assert_raises(TypeError, DETest, n) for k in xrange(SAMPLES): val = generate(DETest) val.validate()
def test_descendant_exclude(): DETest(DE1()) DETest(DE1(DE2(DE3()))) n = DE2(DE4()) assert_raises(TypeError, DETest, n) for k in xrange(SAMPLES): val = generate(DETest) val.validate()
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_listwrapper_generation(): lw1 = generate(LWGT1) assert isinstance(lw1.a, int) assert len(lw1) == 0 lw2 = generate(LWGT2) assert isinstance(lw2.a, int) assert len(lw2) == 2 lw3 = generate(LWGT3) assert isinstance(lw2.a, int) assert len(lw3) == 3 assert isinstance(lw3[0], int) assert isinstance(lw3[1], float) assert isinstance(lw3[2], int) lw4 = generate(LWGT4) assert 2 <= len(lw4) <= 4 lw41 = generate(LWGT4, max_len=3) assert 2 <= len(lw41) <= 3 assert_raises(TypeError, LWGT1, [], a=1.2)
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_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_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_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_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_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_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_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 test_numeric(): x = 1 t = Type.dispatch(x) assert isinstance(t, Numeric) assert type(t) is Int assert hashable(x) == x examine_numeric(Int, x) for cls in subclasses(Numeric): if cls.type is None: continue for k in xrange(SAMPLES): val = generate(cls.type) with on_error(elog, examine_numeric, (cls, val)): examine_numeric(cls, val) buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10): assert type(item) is cls.type assert item != last buf.append(item) if last is None: # These need to be here under enumerate_ b/c of float equality issues eitem = eval(estr(item)) assert eitem == item assert type(eitem) is cls.type last = item if cls.type not in { bool, }: assert is_unique(buf) else: assert not is_unique(buf)
def test_numeric(): x = 1 t = Type.dispatch(x) assert isinstance(t, Numeric) assert type(t) is Int assert hashable(x) == x examine_numeric(Int, x) for cls in subclasses(Numeric): if cls.type is None: continue for k in xrange(SAMPLES): val = generate(cls.type) with on_error(elog, examine_numeric, (cls, val)): examine_numeric(cls, val) buf = [] last = None for item in enumerate_(cls.type, max_enum=SAMPLES * 10): assert type(item) is cls.type assert item != last buf.append(item) if last is None: # These need to be here under enumerate_ b/c of float equality issues eitem = eval(estr(item)) assert eitem == item assert type(eitem) is cls.type last = item if cls.type not in {bool,}: assert is_unique(buf) else: assert not is_unique(buf)
def _generate(cls, **kwargs): a = generate(int, **kwargs) b = generate(float, **kwargs) return cls(a, b)
def test_bytes(): if PY3: gen = generate(bytes) assert isinstance(gen, bytes)
def _generate(cls, **kwargs): a = generate(int, **kwargs) b = generate(float, **kwargs) return cls(a, b)
def test_unicode(): if PY2: gen = generate(unicode) assert isinstance(gen, unicode) assert Unicode._enumeration_value(140) == u'\xab'
def test_bytes(): if PY3: gen = generate(bytes) assert isinstance(gen, bytes)
def test_unicode(): if PY2: gen = generate(unicode) assert isinstance(gen, unicode) assert Unicode._enumeration_value(140) == u'\xab'
def test_misc(): w = generate(DictWrapper) assert isinstance(w, DictWrapper) assert isinstance(w.dct, dict)
def test_misc(): w = generate(DictWrapper) assert isinstance(w, DictWrapper) assert isinstance(w.dct, dict)