コード例 #1
0
 def test_expectUpvaluesPreserved(self):
     """
     If a function uses other "upvalues" (global variable, public classes, imported symbols etc.) these upvalues
      need to be available when the wrapper runs
     """
     petour.patch('petourtest.sut_', free_func_names=['upvalues'])
     self.assertTrue(sut_.upvalues())
コード例 #2
0
 def test_patchAfterThreadCreation(self):
     t = threading.Thread(target=foobar)
     ctx = Counter()
     petour.patch('petourtest.sut_', free_func_names=['foobar'], ctx=ctx)
     t.start()
     t.join()
     self.assertEqual(1, ctx.count)
コード例 #3
0
 def test_unpatchAll_expectContextManagerNotInvoked(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.kls_count'])
     ctx = Counter()
     petour.unpatch_all()
     FooBar.kls_count(11)
     sut_.FooBar.kls_count(111)
     self.assertEqual(0, ctx.count)
コード例 #4
0
 def test_expectFunctionSignaturePreserved(self):
     """
     function's default argument list is preserved
     """
     petour.patch('petourtest.sut_', free_func_names=['compute'])
     self.assertAlmostEqual(15.0, sut_.compute(10, 20))
     self.assertAlmostEqual(31.0, sut_.compute(10, 20, c=2.0, mm='abc'))
コード例 #5
0
 def test_unpatchAll_expectContextManagerNotInvoked(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     ctx = Counter()
     petour.unpatch_all()
     foobar()
     sut_.foobar()
     self.assertEqual(0, ctx.count)
コード例 #6
0
 def test_overrideContextManager_expectInvoked(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
     ctx = Counter()
     petour.set_context_manager('petourtest.sut_', 'FooBar.count', ctx)
     FooBar().count()
     sut_.FooBar().count()
     self.assertEqual(2, ctx.count)
コード例 #7
0
 def test_unpatchAll_expectContextManagerNotInvoked(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.kls_count'])
     ctx = Counter()
     petour.unpatch_all()
     FooBar.kls_count(11)
     sut_.FooBar.kls_count(111)
     self.assertEqual(0, ctx.count)
コード例 #8
0
 def test_unpatchAll_expectContextManagerNotInvoked(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     ctx = Counter()
     petour.unpatch_all()
     foobar()
     sut_.foobar()
     self.assertEqual(0, ctx.count)
コード例 #9
0
 def test_overrideContextManager_expectInvoked(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
     ctx = Counter()
     petour.set_context_manager('petourtest.sut_', 'FooBar.count', ctx)
     FooBar().count()
     sut_.FooBar().count()
     self.assertEqual(2, ctx.count)
コード例 #10
0
 def test_overrideContextManager_expectInvoked(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     ctx = Counter()
     petour.set_context_manager('petourtest.sut_', 'foobar', ctx)
     foobar()
     sut_.foobar()
     self.assertEqual(2, ctx.count)
コード例 #11
0
 def test_expectFreeVarsPreserved(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.meth_freevars'])
     _ = FooBar()
     _.meth_freevars()
     _.meth_freevars()
     _.meth_freevars()
     self.assertEqual(3, _.meth_freevars_calls())
コード例 #12
0
 def test_overrideContextManager_expectInvoked(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     ctx = Counter()
     petour.set_context_manager('petourtest.sut_', 'foobar', ctx)
     foobar()
     sut_.foobar()
     self.assertEqual(2, ctx.count)
コード例 #13
0
 def test_expectFunctionSignaturePreserved(self):
     """
     function's default argument list is preserved
     """
     petour.patch('petourtest.sut_', free_func_names=['compute'])
     self.assertAlmostEqual(15.0, sut_.compute(10, 20))
     self.assertAlmostEqual(31.0, sut_.compute(10, 20, c=2.0, mm='abc'))
コード例 #14
0
 def test_expectUpvaluesPreserved(self):
     """
     If a function uses other "upvalues" (global variable, public classes, imported symbols etc.) these upvalues
      need to be available when the wrapper runs
     """
     petour.patch('petourtest.sut_', free_func_names=['upvalues'])
     self.assertTrue(sut_.upvalues())
コード例 #15
0
 def test_patchAfterThreadCreation(self):
     t = threading.Thread(target=foobar)
     ctx = Counter()
     petour.patch('petourtest.sut_', free_func_names=['foobar'], ctx=ctx)
     t.start()
     t.join()
     self.assertEqual(1, ctx.count)
コード例 #16
0
 def test_expectFreeVarsPreserved(self):
     petour.patch('petourtest.sut_',
                  class_dot_methods=['FooBar.meth_freevars'])
     _ = FooBar()
     _.meth_freevars()
     _.meth_freevars()
     _.meth_freevars()
     self.assertEqual(3, _.meth_freevars_calls())
コード例 #17
0
 def test_unpatchBeforeThreadCreation(self):
     ctx = Counter()
     petour.patch('petourtest.sut_', free_func_names=['foobar'], ctx=ctx)
     petour.unpatch_all()
     t = threading.Thread(target=foobar)
     t.start()
     t.join()
     self.assertEqual(0, ctx.count)
コード例 #18
0
 def test_unpatchBeforeThreadCreation(self):
     ctx = Counter()
     petour.patch('petourtest.sut_', free_func_names=['foobar'], ctx=ctx)
     petour.unpatch_all()
     t = threading.Thread(target=foobar)
     t.start()
     t.join()
     self.assertEqual(0, ctx.count)
コード例 #19
0
    def test_expectNoRacingCondition(self):
        t = threading.Thread(target=sut_.multi_foobar, args=(0.01, 50))
        t.start()
        ctx = Counter()
        petour.patch('petourtest.sut_', free_func_names=['foobar'], ctx=ctx)
        t.join()

        # during the 50 iterations, there should be at least one iteration where the detour-patch is in effect
        self.assertGreater(ctx.count, 1)
コード例 #20
0
    def test_expectNoRacingCondition(self):
        t = threading.Thread(target=sut_.multi_foobar, args=(0.01, 50))
        t.start()
        ctx = Counter()
        petour.patch('petourtest.sut_', free_func_names=['foobar'], ctx=ctx)
        t.join()

        # during the 50 iterations, there should be at least one iteration where the detour-patch is in effect
        self.assertGreater(ctx.count, 1)
コード例 #21
0
 def test_expectFreeVarsPreserved(self):
     ctx = Counter()
     petour.patch('petourtest.sut_', free_func_names=['func_freevars'], ctx=ctx)
     sut_.func_freevars()
     sut_.func_freevars()
     self.assertEqual(3, sut_.func_freevars())
     petour.unpatch_all()
     self.assertEqual(4, sut_.func_freevars())
     self.assertEqual(3, ctx.count)
コード例 #22
0
 def test_patchTwice_expectNoSideEffect(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertEqual(1, len(petour.petours()))
     self.assertEqual(1, foobar(1))
     ctx = petour.context_manager('petourtest.sut_', 'foobar')
     self.assertTrue(ctx)
     petour.unpatch_all()
     self.assertEqual(0, len(petour.petours()))
     ctx = petour.context_manager('petourtest.sut_', 'foobar')
     self.assertFalse(ctx)
コード例 #23
0
 def test_patchTwice_expectNoSideEffect(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertEqual(1, len(petour.petours()))
     self.assertEqual(1, foobar(1))
     ctx = petour.context_manager('petourtest.sut_', 'foobar')
     self.assertTrue(ctx)
     petour.unpatch_all()
     self.assertEqual(0, len(petour.petours()))
     ctx = petour.context_manager('petourtest.sut_', 'foobar')
     self.assertFalse(ctx)
コード例 #24
0
 def test_patchTwice_expectNoSideEffect(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
     self.assertEqual(1, len(petour.petours()))
     self.assertEqual(1, foobar(1))
     ctx = petour.context_manager('petourtest.sut_', 'FooBar.count')
     self.assertTrue(ctx)
     petour.unpatch_all()
     self.assertEqual(0, len(petour.petours()))
     ctx = petour.context_manager('petourtest.sut_', 'FooBar.count')
     self.assertFalse(ctx)
コード例 #25
0
 def test_expectFreeVarsPreserved(self):
     ctx = Counter()
     petour.patch('petourtest.sut_',
                  free_func_names=['func_freevars'],
                  ctx=ctx)
     sut_.func_freevars()
     sut_.func_freevars()
     self.assertEqual(3, sut_.func_freevars())
     petour.unpatch_all()
     self.assertEqual(4, sut_.func_freevars())
     self.assertEqual(3, ctx.count)
コード例 #26
0
 def test_patchTwice_expectNoSideEffect(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
     self.assertEqual(1, len(petour.petours()))
     self.assertEqual(1, foobar(1))
     ctx = petour.context_manager('petourtest.sut_', 'FooBar.count')
     self.assertTrue(ctx)
     petour.unpatch_all()
     self.assertEqual(0, len(petour.petours()))
     ctx = petour.context_manager('petourtest.sut_', 'FooBar.count')
     self.assertFalse(ctx)
コード例 #27
0
    def test_callPatchedInstanceMethod_expectInstanceAccessRestriction(self):
        """
        This is very important:

        A patched instance method should still be an instance method, which
        must be called from the instance not the class

        The following two test methods are to verify the access mode when
         a class-method and a static-method is patched
        """
        petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
        self.assertRaises(TypeError, sut_.FooBar.count)
コード例 #28
0
    def test_callPatchedInstanceMethod_expectInstanceAccessRestriction(self):
        """
        This is very important:

        A patched instance method should still be an instance method, which
        must be called from the instance not the class

        The following two test methods are to verify the access mode when
         a class-method and a static-method is patched
        """
        petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
        self.assertRaises(TypeError, sut_.FooBar.count)
コード例 #29
0
    def test_patchRunningThread(self):
        t = threading.Thread(target=sut_.sleep_in_loop, args=(0.05, 10))
        t.start()
        ctx = Counter()
        petour.patch('petourtest.sut_', free_func_names=['sleep_in_loop'], ctx=ctx)
        t.join()

        # the patch is not "on the train" yet
        self.assertEqual(0, ctx.count)

        t = threading.Thread(target=sut_.sleep_in_loop, args=(0.01, 10))
        t.start()
        t.join()

        # the patch is "on the train" - it is invoked
        self.assertEqual(1, ctx.count)
コード例 #30
0
    def test_patchRunningThread(self):
        t = threading.Thread(target=sut_.sleep_in_loop, args=(0.05, 10))
        t.start()
        ctx = Counter()
        petour.patch('petourtest.sut_',
                     free_func_names=['sleep_in_loop'],
                     ctx=ctx)
        t.join()

        # the patch is not "on the train" yet
        self.assertEqual(0, ctx.count)

        t = threading.Thread(target=sut_.sleep_in_loop, args=(0.01, 10))
        t.start()
        t.join()

        # the patch is "on the train" - it is invoked
        self.assertEqual(1, ctx.count)
コード例 #31
0
 def test_(self):
     petour.patch('petourtest.sut_', free_func_names=['render'])
     petour.patch('petourtest.sut_', free_func_names=['compute'])
     self.assertEqual(True, sut_.render(800, 600, rate=0.5))
     self.assertAlmostEqual(3.2, sut_.compute(1.3, 3.1, mm=0.5))
コード例 #32
0
 def test_expectUpvaluesPreserved(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.upvalues'])
     self.assertTrue(FooBar().upvalues())
コード例 #33
0
 def test_builtins_freeFunction(self):
     petour.patch('__builtin__', free_func_names=['abs'], ctx=self.ctx)
     abs(-10)
コード例 #34
0
 def test_canNotPatchClassAttribute(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.IDDQD'])
     self.assertFalse(petour.petours())
コード例 #35
0
 def test_nonExistingModule(self):
     self.assertFalse(
         petour.patch('petourtest.level', free_func_names=['beef']))
コード例 #36
0
 def test_useWrongKey_expectNoContextManagerObject(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertFalse(petour.petour('module.something', 'foobar'))
コード例 #37
0
 def test_canNotPatchStaticVariable(self):
     petour.patch('petourtest.sut_', free_func_names=['beef'])
     self.assertFalse(petour.petours())
コード例 #38
0
 def test_nonExistingMethod(self):
     self.assertFalse(
         petour.patch('petourtest.sut_',
                      class_dot_methods=['FooBar.Count']))
コード例 #39
0
 def test_expectUpvaluesPreserved(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.upvalues'])
     self.assertTrue(FooBar().upvalues())
コード例 #40
0
 def test_expectMethodSignaturePreserved(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
     self.assertAlmostEqual(5, FooBar().compute(10, 20))
     self.assertAlmostEqual(21, FooBar().compute(10, 20, c=2.0, mm='abc'))
コード例 #41
0
 def test_nonExistingNonCallable(self):
     self.assertFalse(
         petour.patch('petourtest.sut_',
                      class_dot_methods=['FooBar.IDNOCLIP']))
コード例 #42
0
 def test_expectPetourCount(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertEqual(1, len(petour.petours()))
コード例 #43
0
 def test_unpatchAll_expectNoPetourCount(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     petour.unpatch_all()
     self.assertFalse(petour.petours())
コード例 #44
0
 def test_expectDefaultContextManagerObject(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     _, ctx = petour.petour('petourtest.sut_', 'foobar')
     self.assertTrue(ctx)
コード例 #45
0
 def test_expectDefaultContextManagerObject(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     _, ctx = petour.petour('petourtest.sut_', 'foobar')
     self.assertTrue(ctx)
コード例 #46
0
 def test_canNotPatchClassAttribute(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.IDDQD'])
     self.assertFalse(petour.petours())
コード例 #47
0
 def test_nonExistingFreeFuncs(self):
     self.assertFalse(
         petour.patch('petourtest.sut_', free_func_names=['foo2bar']))
コード例 #48
0
 def test_nonExistingPackage(self):
     self.assertFalse(petour.patch('doom2.level', free_func_names=['beef']))
コード例 #49
0
 def test_nonExistingPackage(self):
     self.assertFalse(petour.patch('doom2.level', free_func_names=['beef']))
コード例 #50
0
 def test_nonExistingModule(self):
     self.assertFalse(petour.patch('petourtest.level', free_func_names=['beef']))
コード例 #51
0
 def test_canNotPatchStaticVariable(self):
     petour.patch('petourtest.sut_', free_func_names=['beef'])
     self.assertFalse(petour.petours())
コード例 #52
0
 def test_nonExistingFreeFuncs(self):
     self.assertFalse(petour.patch('petourtest.sut_', free_func_names=['foo2bar']))
コード例 #53
0
 def test_builtins_freeFunction(self):
     petour.patch('__builtin__', free_func_names=['abs'], ctx=self.ctx)
     abs(-10)
コード例 #54
0
 def test_nonExistingMethod(self):
     self.assertFalse(petour.patch('petourtest.sut_', class_dot_methods=['FooBar.Count']))
コード例 #55
0
 def test_expectMethodSignaturePreserved(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.count'])
     self.assertAlmostEqual(5, FooBar().compute(10, 20))
     self.assertAlmostEqual(21, FooBar().compute(10, 20, c=2.0, mm='abc'))
コード例 #56
0
 def test_nonExistingNonCallable(self):
     self.assertFalse(petour.patch('petourtest.sut_', class_dot_methods=['FooBar.IDNOCLIP']))
コード例 #57
0
 def test_expectPetourCount(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertEqual(1, len(petour.petours()))
コード例 #58
0
 def test_unpatchAll_expectNoPetourCount(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     petour.unpatch_all()
     self.assertFalse(petour.petours())
コード例 #59
0
 def test_(self):
     petour.patch('petourtest.sut_', free_func_names=['render'])
     petour.patch('petourtest.sut_', free_func_names=['compute'])
     self.assertEqual(True, sut_.render(800, 600, rate=0.5))
     self.assertAlmostEqual(3.2, sut_.compute(1.3, 3.1, mm=0.5))
コード例 #60
0
 def test_useWrongKey_expectNoContextManagerObject(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertFalse(petour.petour('module.something', 'foobar'))