Beispiel #1
0
    def testProvides(self):
        """
        Tests the @Provides decorator
        """
        class DummyClass(object):
            pass

        def method():
            pass

        # Empty specification
        for empty in (None, "", "   "):
            self.assertRaises(ValueError, decorators.Provides, empty)

            # No error should be raised
            log_off()
            decorators.Provides("spec", empty)
            log_on()

        # Field name with a space
        self.assertRaises(ValueError, decorators.Provides, "spec", "a space")

        # Invalid specification type
        for invalid in ([1, 2, 3], tuple((1, 2, 3)), 123):
            self.assertRaises(ValueError, decorators.Provides, invalid)
            self.assertRaises(ValueError, decorators.Provides, "spec", invalid)

        # Invalid target
        for invalid in (None, method, 123):
            self.assertRaises(TypeError, decorators.Provides("spec", "field"),
                              invalid)
Beispiel #2
0
    def test_provides_factory(self):
        """
        Tests the @Provides decorator for a service factory
        """
        class DummyClass(object):
            pass

        def invalid_method(self, foo):
            pass

        def valid_method(self, bundle, svc_reg):
            pass

        # Missing method in class
        self.assertRaises(
            TypeError, decorators.Provides("spec", factory=True), DummyClass)

        # One of two methods
        DummyClass.get_service = valid_method
        self.assertRaises(
            TypeError, decorators.Provides("spec", factory=True), DummyClass)

        # Both methods
        DummyClass.unget_service = valid_method
        try:
            decorators.Provides("spec", factory=True)(DummyClass)
        except TypeError:
            self.fail("Error on valid class")

        # Invalid arity
        DummyClass.get_service = invalid_method
        self.assertRaises(
            TypeError, decorators.Provides("spec", factory=True), DummyClass)
Beispiel #3
0
    def test_provides_prototype(self):
        """
        Tests the @Provides decorator for a prototype service factory
        """
        class DummyClass(object):
            pass

        def invalid_method(self, foo):
            pass

        def valid_method(self, bundle, svc_reg):
            pass

        def valid_instance_method(self, bundle, svc_reg, svc):
            pass

        # Missing method in class
        self.assertRaises(
            TypeError, decorators.Provides("spec", prototype=True), DummyClass)

        # One of three methods
        DummyClass.get_service = valid_method
        self.assertRaises(
            TypeError, decorators.Provides("spec", prototype=True), DummyClass)

        # Two of three methods
        DummyClass.unget_service = valid_method
        self.assertRaises(
            TypeError, decorators.Provides("spec", prototype=True), DummyClass)

        # Two (other) of three methods
        del DummyClass.unget_service
        DummyClass.unget_service_instance = valid_instance_method
        self.assertRaises(
            TypeError, decorators.Provides("spec", prototype=True), DummyClass)

        # All methods
        DummyClass.unget_service = valid_method
        try:
            decorators.Provides("spec", prototype=True)(DummyClass)
        except TypeError:
            self.fail("Error on valid class")

        # Invalid arity
        DummyClass.get_service = invalid_method
        self.assertRaises(
            TypeError, decorators.Provides("spec", prototype=True), DummyClass)

        DummyClass.get_service = valid_method
        DummyClass.unget_service_instance = invalid_method
        self.assertRaises(
            TypeError, decorators.Provides("spec", prototype=True), DummyClass)
Beispiel #4
0
    def testProvides(self):
        """
        Tests the @Provides decorator
        """
        class DummyClass(object):
            pass

        def method():
            pass

        # Field name with a space
        self.assertRaises(ValueError, decorators.Provides, "spec", "a space")

        # Invalid specification type
        for invalid in ([1, 2, 3], tuple((1, 2, 3)), 123):
            self.assertRaises(ValueError, decorators.Provides, "spec", invalid)

        # Invalid target
        for invalid in (None, method, 123):
            self.assertRaises(TypeError, decorators.Provides("spec", "field"),
                              invalid)