1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" Test the adaptation offers. """
import sys
import unittest
from traits.adaptation.adaptation_offer import AdaptationOffer
class TestAdaptationOffer(unittest.TestCase):
""" Test the adaptation offers. """
def test_lazy_loading(self):
LAZY_EXAMPLES = "traits.adaptation.tests.lazy_examples"
if LAZY_EXAMPLES in sys.modules:
del sys.modules[LAZY_EXAMPLES]
offer = AdaptationOffer(
factory=(LAZY_EXAMPLES + ".IBarToIFoo"),
from_protocol=(LAZY_EXAMPLES + ".IBar"),
to_protocol=(LAZY_EXAMPLES + ".IFoo"),
)
self.assertNotIn(LAZY_EXAMPLES, sys.modules)
factory = offer.factory
self.assertIn(LAZY_EXAMPLES, sys.modules)
from traits.adaptation.tests.lazy_examples import IBarToIFoo
self.assertIs(factory, IBarToIFoo)
del sys.modules[LAZY_EXAMPLES]
from_protocol = offer.from_protocol
from traits.adaptation.tests.lazy_examples import IBar
self.assertIs(from_protocol, IBar)
del sys.modules[LAZY_EXAMPLES]
to_protocol = offer.to_protocol
from traits.adaptation.tests.lazy_examples import IFoo
self.assertIs(to_protocol, IFoo)
def test_adaptation_offer_str_representation(self):
""" test string representation of the AdaptationOffer class. """
class Foo:
pass
class Bar:
pass
adaptation_offer = AdaptationOffer(from_protocol=Foo, to_protocol=Bar)
desired_repr = "<AdaptationOffer: '{}' -> '{}'>".format(
adaptation_offer.from_protocol_name,
adaptation_offer.to_protocol_name
)
self.assertEqual(desired_repr, str(adaptation_offer))
self.assertEqual(desired_repr, repr(adaptation_offer))
|