def testInvariant(self):
        from zope.interface.exceptions import Invalid
        from zope.interface import directlyProvides
        from zope.interface.tests.unitfixtures import BarGreaterThanFoo
        from zope.interface.tests.unitfixtures import ifFooThenBar
        from zope.interface.tests.unitfixtures import IInvariant
        from zope.interface.tests.unitfixtures import InvariantC
        from zope.interface.tests.unitfixtures import ISubInvariant
        # set up
        o = InvariantC()
        directlyProvides(o, IInvariant)

        # a helper
        def errorsEqual(self, o, error_len, error_msgs, iface=None):
            if iface is None:
                iface = IInvariant
            self.assertRaises(Invalid, iface.validateInvariants, o)
            e = []
            try:
                iface.validateInvariants(o, e)
            except Invalid, error:
                self.assertEquals(error.args[0], e)
            else:
                self.assertEquals(msg, error_msgs.pop(0))

        # the tests
        self.assertEquals(IInvariant.getTaggedValue('invariants'),
                          [ifFooThenBar])
        self.assertEquals(IInvariant.validateInvariants(o), None)
        o.bar = 27
        self.assertEquals(IInvariant.validateInvariants(o), None)
        o.foo = 42
        self.assertEquals(IInvariant.validateInvariants(o), None)
        del o.bar
        errorsEqual(self, o, 1, ['If Foo, then Bar!'])
        # nested interfaces with invariants:
        self.assertEquals(ISubInvariant.getTaggedValue('invariants'),
                          [BarGreaterThanFoo])
        o = InvariantC()
        directlyProvides(o, ISubInvariant)
        o.foo = 42
        # even though the interface has changed, we should still only have one
        # error.
        errorsEqual(self, o, 1, ['If Foo, then Bar!'], ISubInvariant)
        # however, if we set foo to 0 (Boolean False) and bar to a negative
        # number then we'll get the new error
        o.foo = 2
        o.bar = 1
        errorsEqual(self, o, 1, ['Please, Boo MUST be greater than Foo!'],
                    ISubInvariant)
        # and if we set foo to a positive number and boo to 0, we'll
        # get both errors!
        o.foo = 1
        o.bar = 0
예제 #3
0
     for msg in msgs:
         self.assertEquals(msg, error_msgs.pop(0))
 # the tests
 self.assertEquals(IInvariant.getTaggedValue('invariants'),
                   [ifFooThenBar])
 self.assertEquals(IInvariant.validateInvariants(o), None)
 o.bar = 27
 self.assertEquals(IInvariant.validateInvariants(o), None)
 o.foo = 42
 self.assertEquals(IInvariant.validateInvariants(o), None)
 del o.bar
 errorsEqual(self, o, 1, ['If Foo, then Bar!'])
 # nested interfaces with invariants:
 self.assertEquals(ISubInvariant.getTaggedValue('invariants'),
                   [BarGreaterThanFoo])
 o = InvariantC()
 directlyProvides(o, ISubInvariant)
 o.foo = 42
 # even though the interface has changed, we should still only have one
 # error.
 errorsEqual(self, o, 1, ['If Foo, then Bar!'], ISubInvariant)
 # however, if we set foo to 0 (Boolean False) and bar to a negative
 # number then we'll get the new error
 o.foo = 2
 o.bar = 1
 errorsEqual(self, o, 1, ['Please, Boo MUST be greater than Foo!'],
             ISubInvariant)
 # and if we set foo to a positive number and boo to 0, we'll
 # get both errors!
 o.foo = 1
 o.bar = 0