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_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_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_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