Пример #1
0
    def test(self):

        field = Text(title=u"Foo thing")

        class I(Interface):

            getFoo, setFoo = accessors(field)

        class Bad(object):
            implements(I)

        class Good(object):
            implements(I)

            def __init__(self):
                self.set = 0

            def getFoo(self):
                return u"foo"

            def setFoo(self, v):
                self.set += 1

        names = I.names()
        names.sort()
        self.assertEqual(names, ['getFoo', 'setFoo'])
        self.assertEqual(I['getFoo'].field, field)
        self.assertEqual(I['getFoo'].__name__, 'getFoo')
        self.assertEqual(I['getFoo'].__doc__, u'get Foo thing')
        self.assertEqual(I['getFoo'].__class__, FieldReadAccessor)
        self.assertEqual(I['getFoo'].writer, I['setFoo'])

        # test some field attrs
        for attr in ('title', 'description', 'readonly'):
            self.assertEqual(getattr(I['getFoo'], attr), getattr(field, attr))

        self.assert_(IText.providedBy(I['getFoo']))

        self.assert_(IMethod.providedBy(I['getFoo']))
        self.assert_(IMethod.providedBy(I['setFoo']))

        self.assertEqual(I['setFoo'].field, field)
        self.assertEqual(I['setFoo'].__name__, 'setFoo')
        self.assertEqual(I['setFoo'].__doc__, u'set Foo thing')
        self.assertEqual(I['setFoo'].__class__, FieldWriteAccessor)

        self.assertRaises(Exception, verifyClass, I, Bad)
        self.assertRaises(Exception, verifyObject, I, Bad())

        self.assertEquals(I['getFoo'].query(Bad(), 42), 42)
        self.assertRaises(AttributeError, I['getFoo'].get, Bad())

        verifyClass(I, Good)
        verifyObject(I, Good())

        self.assertEquals(I['getFoo'].query(Good(), 42), u'foo')
        self.assertEquals(I['getFoo'].get(Good()), u'foo')
        instance = Good()
        I['getFoo'].set(instance, u'whatever')
        self.assertEquals(instance.set, 1)
Пример #2
0
    def testNotImplemented(self):

        class C(object): pass

        class I(Interface): pass

        self.assertRaises(DoesNotImplement, verifyClass, I, C)

        classImplements(C, I)

        verifyClass(I, C)
Пример #3
0
    def testMissingAttr(self):

        class I(Interface):
            def f(): pass

        class C(object):
            implements(I)

        self.assertRaises(BrokenImplementation, verifyClass, I, C)

        C.f=lambda self: None

        verifyClass(I, C)
Пример #4
0
    def testMethodForAttr(self):
        
        class IFoo(Interface):
             foo = Attribute("The foo Attribute")


        class Foo:
             implements(IFoo)

             def foo(self):
                 pass

        verifyClass(IFoo, Foo)
Пример #5
0
    def testWrongArgs(self):

        class I(Interface):
            def f(a): pass

        class C(object):
            def f(self, b): pass

            implements(I)

        # We no longer require names to match.
        #self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a: None

        verifyClass(I, C)

        C.f=lambda self, **kw: None

        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a, *args: None

        verifyClass(I, C)

        C.f=lambda self, a, *args, **kw: None

        verifyClass(I, C)

        C.f=lambda self, *args: None

        verifyClass(I, C)
Пример #6
0
    def testNoKW(self):

        class I(Interface):
            def f(a, **args): pass

        class C(object):
            def f(self, a): pass

            implements(I)

        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a, **foo: None

        verifyClass(I, C)
Пример #7
0
 def test_interfaces(self):
     verifyObject(ITimeDelta, timedelta(minutes=20))
     verifyObject(IDate, date(2000, 1, 2))
     verifyObject(IDateTime, datetime(2000, 1, 2, 10, 20))
     verifyObject(ITime, time(20, 30, 15, 1234))
     verifyObject(ITZInfo, tzinfo())
     verifyClass(ITimeDeltaClass, timedelta)
     verifyClass(IDateClass, date)
     verifyClass(IDateTimeClass, datetime)
     verifyClass(ITimeClass, time)
Пример #8
0
    def testExtraArgs(self):

        class I(Interface):
            def f(a): pass

        class C(object):
            def f(self, a, b): pass

            implements(I)

        self.assertRaises(BrokenMethodImplementation, verifyClass, I, C)

        C.f=lambda self, a: None

        verifyClass(I, C)

        C.f=lambda self, a, b=None: None

        verifyClass(I, C)
Пример #9
0
 def testVerifyImplementation(self):
     from zpt._zope.interface.verify import verifyClass
     self.assert_(verifyClass(FooInterface, Foo))
     self.assert_(Interface.providedBy(I1))