def test_adapts_should_register_class_as_providing_the_to_protocol(self): from traits.api import Adapter, adapts, HasTraits, Instance, \ Int, Interface class IFoo(Interface): x = Int class Bar(HasTraits): foo = Instance(IFoo) class Baz(HasTraits): pass # Warning: because we are trying to test the 'adapts' class advisor, # this will effect the global adaptation manager and hence may # interfere with any other tests that rely on it (all of the tests # in this package use a separate adaptation manager so there should # be no clashes here ;^). # # 'adapts' is also deprecated, so expect a warning message when you # run the tests. class BazToIFooAdapter(Adapter): adapts(Baz, IFoo) baz = Baz() bar = Bar() bar.foo = adapt(baz, IFoo) self.assertEqual(bar.foo.adaptee, baz) return
def test_global_register_provides(self): from traits.api import Interface class IFoo(Interface): pass obj = {} # Global `register_provides`. register_provides(dict, IFoo) self.assertEqual(obj, adapt(obj, IFoo))
def _adapt_wrapper(*args, **kw): # We need this wrapper to defer the import of 'adapt' and avoid a circular # import. The ctraits 'adapt' callback needs to be set as soon as possible, # but the adaptation mechanism relies on traits. # This wrapper is called once, after which we set the ctraits callback # to point directly to 'adapt'. from traits.adaptation.api import adapt ctraits._adapt(adapt) return adapt(*args, **kw)
def test_global_register_offer(self): ex = self.examples offer = AdaptationOffer(factory=ex.UKStandardToEUStandard, from_protocol=ex.UKStandard, to_protocol=ex.EUStandard) # Global `register_offer`. register_offer(offer) uk_plug = ex.UKPlug() eu_plug = adapt(uk_plug, ex.EUStandard) self.assertIsNotNone(eu_plug) self.assertIsInstance(eu_plug, ex.UKStandardToEUStandard)
def __enter__(self): """ Enter method. """ locals_val = sys._getframe().f_back.f_locals if 'context' in locals_val and isinstance(locals_val['context'], dict): locals_val = locals_val['context'] # Make a copy of this context. locals_val = adapt(locals_val, ICheckpointable).checkpoint() adapters = [WithMaskAdapter(mask=self.mask)] self.context = AdaptedDataContext(subcontext=locals_val, _adapters=adapters) return
def test_global_register_offer(self): ex = self.examples offer = AdaptationOffer( factory = ex.UKStandardToEUStandard, from_protocol = ex.UKStandard, to_protocol = ex.EUStandard ) # Global `register_offer`. register_offer(offer) uk_plug = ex.UKPlug() eu_plug = adapt(uk_plug, ex.EUStandard) self.assertIsNotNone(eu_plug) self.assertIsInstance(eu_plug, ex.UKStandardToEUStandard)
def test_global_convenience_functions(self): ex = self.examples # Global `register_factory`. register_factory(factory=ex.UKStandardToEUStandard, from_protocol=ex.UKStandard, to_protocol=ex.EUStandard) uk_plug = ex.UKPlug() # Global `adapt`. eu_plug = adapt(uk_plug, ex.EUStandard) self.assertIsNotNone(eu_plug) self.assertIsInstance(eu_plug, ex.UKStandardToEUStandard) # Global `provides_protocol`. self.assertTrue(provides_protocol(ex.UKPlug, ex.UKStandard)) # Global `supports_protocol`. self.assertTrue(supports_protocol(uk_plug, ex.EUStandard))
def test_global_convenience_functions(self): ex = self.examples # Global `register_factory`. register_factory( factory = ex.UKStandardToEUStandard, from_protocol = ex.UKStandard, to_protocol = ex.EUStandard ) uk_plug = ex.UKPlug() # Global `adapt`. eu_plug = adapt(uk_plug, ex.EUStandard) self.assertIsNotNone(eu_plug) self.assertIsInstance(eu_plug, ex.UKStandardToEUStandard) # Global `provides_protocol`. self.assertTrue(provides_protocol(ex.UKPlug, ex.UKStandard)) # Global `supports_protocol`. self.assertTrue(supports_protocol(uk_plug, ex.EUStandard))
def adapt(*args, **kw): from traits.adaptation.api import adapt return adapt(*args, **kw)