def test_reset_adaptation_manager(self):
        ex = self.examples
        adaptation_manager = get_global_adaptation_manager()

        # UKStandard->EUStandard.
        adaptation_manager.register_factory(
            factory       = ex.UKStandardToEUStandard,
            from_protocol = ex.UKStandard,
            to_protocol   = ex.EUStandard,
        )

        # Create a UKPlug.
        uk_plug = ex.UKPlug()

        reset_global_adaptation_manager()
        adaptation_manager = get_global_adaptation_manager()

        with self.assertRaises(AdaptationError):
            adaptation_manager.adapt(uk_plug, ex.EUStandard)
예제 #2
0
    def test_reset_adaptation_manager(self):
        ex = self.examples
        adaptation_manager = get_global_adaptation_manager()

        # UKStandard->EUStandard.
        adaptation_manager.register_factory(
            factory=ex.UKStandardToEUStandard,
            from_protocol=ex.UKStandard,
            to_protocol=ex.EUStandard,
        )

        # Create a UKPlug.
        uk_plug = ex.UKPlug()

        reset_global_adaptation_manager()
        adaptation_manager = get_global_adaptation_manager()

        with self.assertRaises(AdaptationError):
            adaptation_manager.adapt(uk_plug, ex.EUStandard)
예제 #3
0
    def test_set_adaptation_manager(self):
        ex = self.examples
        adaptation_manager = AdaptationManager()

        # UKStandard->EUStandard.
        adaptation_manager.register_factory(factory=ex.UKStandardToEUStandard,
                                            from_protocol=ex.UKStandard,
                                            to_protocol=ex.EUStandard)

        # Create a UKPlug.
        uk_plug = ex.UKPlug()

        set_global_adaptation_manager(adaptation_manager)
        global_adaptation_manager = get_global_adaptation_manager()

        eu_plug = global_adaptation_manager.adapt(uk_plug, ex.EUStandard)
        self.assertIsNotNone(eu_plug)
        self.assertIsInstance(eu_plug, ex.UKStandardToEUStandard)
    def test_set_adaptation_manager(self):
        ex = self.examples
        adaptation_manager = AdaptationManager()

        # UKStandard->EUStandard.
        adaptation_manager.register_factory(
            factory       = ex.UKStandardToEUStandard,
            from_protocol = ex.UKStandard,
            to_protocol   = ex.EUStandard
        )

        # Create a UKPlug.
        uk_plug = ex.UKPlug()

        set_global_adaptation_manager(adaptation_manager)
        global_adaptation_manager = get_global_adaptation_manager()

        eu_plug = global_adaptation_manager.adapt(uk_plug, ex.EUStandard)
        self.assertIsNotNone(eu_plug)
        self.assertIsInstance(eu_plug, ex.UKStandardToEUStandard)
예제 #5
0

import sys

from traits.testing.unittest_tools import unittest
from traits.adaptation.api import get_global_adaptation_manager, \
    set_global_adaptation_manager
from traits.api import HasTraits, Adapter, adapts, AdaptsTo, \
    implements, Instance, Int, Interface, List, Supports, TraitError


# Using the deprecated class advisor "adapts", the registration of adapters
# occurs globally at class definition time. Since other tests will reset the
# global adaptation manager, the registration will be lost.
# That's why we save a reference to the current global adaptation manager.
_adaptation_manager = get_global_adaptation_manager()


#------------------------------------------------------------------------------
#  Test 'Interface' definitions:
#------------------------------------------------------------------------------

# 'adapts' and 'implements' are not supported in Python 3.
if sys.version_info < (3,):

    class IFoo(Interface):

        def get_foo(self):
            """ Returns the current foo. """

from __future__ import absolute_import

import sys

from traits.testing.unittest_tools import unittest
from traits.adaptation.api import get_global_adaptation_manager, \
    set_global_adaptation_manager
from traits.api import HasTraits, Adapter, adapts, AdaptsTo, \
    implements, Instance, Int, Interface, List, Supports, TraitError


# Using the deprecated class advisor "adapts", the registration of adapters
# occurs globally at class definition time. Since other tests will reset the
# global adaptation manager, the registration will be lost.
# That's why we save a reference to the current global adaptation manager.
_adaptation_manager = get_global_adaptation_manager()


if sys.version_info[0] >= 3:
    import nose
    raise nose.SkipTest("""
        Currently, under Python 3, class advisors do not work anymore.
        Therefore, this test will fail due to the use of "adapts".
    """)


#------------------------------------------------------------------------------
#  Test 'Interface' definitions:
#------------------------------------------------------------------------------

class IFoo(Interface):
예제 #7
0
        copy : IContext
        """
        copy = self.clone_traits()
        checkpointable_subcontext = adapt(self.subcontext, ICheckpointable)
        copy.subcontext = checkpointable_subcontext.checkpoint()
        return copy


# Define adaptation offers from IContext to other context protocols using
# DataContext.

data_context_offer = AdaptationOffer(
    factory=lambda x: DataContext(subcontext=x),
    from_protocol=IContext,
    to_protocol=IDataContext)


def register_i_context_adapter_offers(adaptation_manager):
    """ Register adapters from the `dict` object to `codetools` protocols.

    1) `dict` provides the `IContext` interface
    2) `dict` can be adapted to `ICheckpointable`
    """

    adaptation_manager.register_offer(data_context_offer)


# For backward compatibility, we register the adapters from `dict` globally
# at import time.
register_i_context_adapter_offers(get_global_adaptation_manager())
예제 #8
0
        """
        copy = self.clone_traits()
        checkpointable_subcontext = adapt(self.subcontext, ICheckpointable)
        copy.subcontext = checkpointable_subcontext.checkpoint()
        return copy


# Define adaptation offers from IContext to other context protocols using
# DataContext.

data_context_offer = AdaptationOffer(
    factory=lambda x: DataContext(subcontext=x),
    from_protocol=IContext,
    to_protocol=IDataContext
)


def register_i_context_adapter_offers(adaptation_manager):
    """ Register adapters from the `dict` object to `codetools` protocols.

    1) `dict` provides the `IContext` interface
    2) `dict` can be adapted to `ICheckpointable`
    """

    adaptation_manager.register_offer(data_context_offer)


# For backward compatibility, we register the adapters from `dict` globally
# at import time.
register_i_context_adapter_offers(get_global_adaptation_manager())