Пример #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'))