コード例 #1
0
 def testWrap(self): #{{{
     '''Proper wrap functionality given proper input'''
     cw = CallableWrapper(self.func)
     cw.wrap(self.deco)
     self.assertTrue(cw._newcall('a', str))
     self.assertFalse(cw._newcall('a', unicode))
     self.assertTrue(cw._newcall(u'a', unicode))
     self.assertFalse(cw._newcall(1, int))
コード例 #2
0
 def testWrap(self): #{{{
     '''If wrapped, will call the wrap function'''
     def deco(f): #{{{
         def w(s): #{{{
             return 400
         # End def #}}}
         return w
     # End def #}}}
     _ = lambda: 100
     cw = CallableWrapper(_)
     self.assertEqual(cw(), 100)
     cw.wrap(deco)
     self.assertEqual(cw(), 400)
     cw.unwrap()
     self.assertEqual(cw(), 100)
コード例 #3
0
    def testUnWrap(self):  # {{{
        """Unwrap"""

        class A(object):
            def helloworld(self, a, b):
                return "hello world"

        a = A()
        w = CallableWrapper(a.helloworld)
        w.wrap(DummyReplacement)
        a.helloworld = method(w, a, A)

        self.assertEqual(a.helloworld(1, 2), "HELLO: hello world :WORLD")
        self.assertEqual(A().helloworld(1, 2), "hello world")
        w.unwrap()
        self.assertEqual(A().helloworld(1, 2), a.helloworld(1, 2))
コード例 #4
0
    def testClassMethodWrap(self):  # {{{
        """Wrapping class method"""

        class A(object):
            def helloworld(self, a, b):
                return "hello world"

        w = CallableWrapper(A.helloworld)
        w.wrap(DummyReplacement)
        A.helloworld = method(w, None, A)

        z = CallableWrapper(A.helloworld)
        z.wrap(DummyReplacement)
        A.helloworld = method(z, None, A)

        a = A()
        self.assertEqual(a.helloworld(1, 2), "HELLO: HELLO: hello world :WORLD :WORLD")
コード例 #5
0
    def testInstanceMethodWrap(self):  # {{{
        """Wrapping instance method"""

        class A(object):
            def helloworld(self, a, b):
                return "hello world"

        a = A()
        w = CallableWrapper(a.helloworld)
        w.wrap(DummyReplacement)
        a.helloworld = method(w, a, A)

        z = CallableWrapper(a.helloworld)
        z.wrap(DummyReplacement)
        a.helloworld = method(z, a, A)

        self.assertEqual(a.helloworld(1, 2), "HELLO: HELLO: hello world :WORLD :WORLD")
        self.assertEqual(A().helloworld(1, 2), "hello world")
コード例 #6
0
 def testNonMethodWrap(self):  # {{{
     """Wrapping a non-method callable"""
     w = CallableWrapper(DummyFunction)
     w.wrap(DummyReplacement)
     self.assertEqual(w(1, 2), "HELLO: %s :WORLD" % DummyFunction(1, 2))
コード例 #7
0
 def testUnwrap(self): #{{{
     '''Unwrapping returns to default call state'''
     cw = CallableWrapper(self.func)
     cw.wrap(self.deco)
     cw.unwrap()
     self.assertEqual(cw._newcall, cw.call)
コード例 #8
0
 def testMethodWrap(self): #{{{
     '''If return value of arg is not a method, makes it into a method'''
     cw = CallableWrapper(self.func)
     cw.wrap(self.deco)
     self.assertTrue(isinstance(cw._newcall, method))