def test_intersect_1(self): """ Make sure .intersect works on all iterables """ from typecheck import Typeclass tc_int = Typeclass(int) tc_flt = Typeclass(float) interface_int = set(tc_int.interface()) interface_flt = set(tc_flt.interface()) merged_intfc = interface_int.intersection(interface_flt) def run_test(itr_type): tc_int.intersect(itr_type([float])) for t in (int, float): assert tc_int.has_instance(t) for method in merged_intfc: assert method in tc_int.interface() def gen(seq): for o in seq: yield o for itr_type in (tuple, list, set, gen): run_test(itr_type)
def test_cope_with_class_changes(self): from typecheck import Typeclass, _TC_AttrError, _TC_TypeError from typecheck import IsCallable class A(object): def foo(self): pass class B(object): def foo(self): pass tc = Typeclass(A) b = B() # Should pass check_type(tc, b) B.foo = 5 # B is still cached as known-good check_type(tc, b) tc.recalculate_interface() try: check_type(tc, b) except _TC_AttrError, e: assert e.attr == 'foo' assert isinstance(e.inner, _TC_TypeError) assert e.inner.right == IsCallable() assert e.inner.wrong == int
def test_instances_1(self): from typecheck import Typeclass tc = Typeclass(list, tuple, set) instances = tc.instances() assert isinstance(instances, list) assert len(instances) == 3 assert list in instances assert tuple in instances assert set in instances
def test_instances_2(self): from typecheck import Typeclass tc = Typeclass(list, tuple, Typeclass(str, unicode)) instances = tc.instances() assert isinstance(instances, list) assert len(instances) == 4 assert list in instances assert tuple in instances assert str in instances assert unicode in instances
def test_interface(self): from typecheck import Typeclass tc = Typeclass(list, tuple, set) interface = tc.interface() for method in interface: for t in (list, tuple, set): assert callable(getattr(t, method)) for method in ('__class__', '__init__', '__new__', '__doc__'): assert method not in interface
def test_fail_1(self): from typecheck import Typeclass, _TC_AttrError, _TC_TypeError from typecheck import IsCallable class A(object): def foo(self): pass class B(object): def foo(self): pass def bar(self): pass class C(object): def __init__(self): self.foo = 5 tc = Typeclass(A, B) try: check_type(tc, C()) except _TC_AttrError, e: assert e.attr == 'foo' assert isinstance(e.inner, _TC_TypeError) assert e.inner.right == IsCallable() assert e.inner.wrong == int
def test_intersect_1(self): # Make sure .intersect works on other Typeclass instances from typecheck import Typeclass tc_int = Typeclass(int) tc_flt = Typeclass(float) interface_int = set(tc_int.interface()) interface_flt = set(tc_flt.interface()) merged_intfc = interface_int.intersection(interface_flt) tc_int.intersect(tc_flt) for t in (int, float): assert tc_int.has_instance(t) for method in merged_intfc: assert method in tc_int.interface()
def test_no_args(self): from typecheck import Typeclass try: Typeclass() except TypeError: pass else: raise AssertionError("Failed to raise TypeError")
def test_has_instance(self): from typecheck import Typeclass tc = Typeclass(list, tuple) assert tc.has_instance(list) assert tc.has_instance(tuple) assert not tc.has_instance(set) tc.add_instance(set) assert tc.has_instance(set)
def test_pass(self): from typecheck import Typeclass tc = Typeclass(list, set) # These are a no-brainer check_type(tc, list) check_type(tc, set) check_type(tc, tuple)
def test_intersect_2(self): # Make sure .intersect works on all iterables from typecheck import Typeclass tc_int = Typeclass(int) tc_flt = Typeclass(float) interface_int = set(tc_int.interface()) interface_flt = set(tc_flt.interface()) merged_intfc = interface_int.intersection(interface_flt) def run_test(itr_type): tc_int.intersect(itr_type([float])) for t in (int, float): assert tc_int.has_instance(t) for method in merged_intfc: assert method in tc_int.interface() def gen(seq): for o in seq: yield o for itr_type in (tuple, list, set, gen): run_test(itr_type)
def test_add_instance(self): from typecheck import Typeclass tc = Typeclass(list, set, tuple) interface_before = tc.interface() tc.add_instance(dict) # Adding an instance shouldn't chance the interface... assert interface_before == tc.interface() # but it should change the instances list assert dict in tc.instances()
def test_intersect_1(self): """ Make sure .intersect works on other Typeclass instances """ from typecheck import Typeclass tc_int = Typeclass(int) tc_flt = Typeclass(float) interface_int = set(tc_int.interface()) interface_flt = set(tc_flt.interface()) merged_intfc = interface_int.intersection(interface_flt) tc_int.intersect(tc_flt) for t in (int, float): assert tc_int.has_instance(t) for method in merged_intfc: assert method in tc_int.interface()
def test_fail_2(self): from typecheck import Typeclass, _TC_MissingAttrError class A(object): def foo(self): pass class B(object): def foo(self): pass def bar(self): pass class C(object): pass tc = Typeclass(A, B) try: check_type(tc, C()) except _TC_MissingAttrError, e: assert e.attr == 'foo'
from typecheck import Typeclass ### Number #################################################### _numbers = [int, float, complex, long, bool] try: from decimal import Decimal _numbers.append(Decimal) del Decimal except ImportError: pass Number = Typeclass(*_numbers) del _numbers ### String -- subinstance of ImSequence #################################################### String = Typeclass(str, unicode) ### ImSequence -- immutable sequences #################################################### ImSequence = Typeclass(tuple, xrange, String) ### MSequence -- mutable sequences #################################################### MSequence = Typeclass(list)
def test_hash(self): from typecheck import Typeclass eq_tests = [(Typeclass(int), Typeclass(int)), (Typeclass(int, float), Typeclass(float, int)), (Typeclass(int, int), Typeclass(int)), (Typeclass(int), Typeclass(Typeclass(int)))] ne_tests = [(Typeclass(int), Typeclass(float))] self.multipleAssertEqualHashes(eq_tests, ne_tests)