Ejemplo n.º 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())
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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'))
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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())
Ejemplo n.º 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)
Ejemplo n.º 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'))
Ejemplo n.º 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())
Ejemplo n.º 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)
Ejemplo n.º 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())
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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))
Ejemplo n.º 32
0
 def test_expectUpvaluesPreserved(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.upvalues'])
     self.assertTrue(FooBar().upvalues())
Ejemplo n.º 33
0
 def test_builtins_freeFunction(self):
     petour.patch('__builtin__', free_func_names=['abs'], ctx=self.ctx)
     abs(-10)
Ejemplo n.º 34
0
 def test_canNotPatchClassAttribute(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.IDDQD'])
     self.assertFalse(petour.petours())
Ejemplo n.º 35
0
 def test_nonExistingModule(self):
     self.assertFalse(
         petour.patch('petourtest.level', free_func_names=['beef']))
Ejemplo n.º 36
0
 def test_useWrongKey_expectNoContextManagerObject(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertFalse(petour.petour('module.something', 'foobar'))
Ejemplo n.º 37
0
 def test_canNotPatchStaticVariable(self):
     petour.patch('petourtest.sut_', free_func_names=['beef'])
     self.assertFalse(petour.petours())
Ejemplo n.º 38
0
 def test_nonExistingMethod(self):
     self.assertFalse(
         petour.patch('petourtest.sut_',
                      class_dot_methods=['FooBar.Count']))
Ejemplo n.º 39
0
 def test_expectUpvaluesPreserved(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.upvalues'])
     self.assertTrue(FooBar().upvalues())
Ejemplo n.º 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'))
Ejemplo n.º 41
0
 def test_nonExistingNonCallable(self):
     self.assertFalse(
         petour.patch('petourtest.sut_',
                      class_dot_methods=['FooBar.IDNOCLIP']))
Ejemplo n.º 42
0
 def test_expectPetourCount(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertEqual(1, len(petour.petours()))
Ejemplo n.º 43
0
 def test_unpatchAll_expectNoPetourCount(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     petour.unpatch_all()
     self.assertFalse(petour.petours())
Ejemplo n.º 44
0
 def test_expectDefaultContextManagerObject(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     _, ctx = petour.petour('petourtest.sut_', 'foobar')
     self.assertTrue(ctx)
Ejemplo n.º 45
0
 def test_expectDefaultContextManagerObject(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     _, ctx = petour.petour('petourtest.sut_', 'foobar')
     self.assertTrue(ctx)
Ejemplo n.º 46
0
 def test_canNotPatchClassAttribute(self):
     petour.patch('petourtest.sut_', class_dot_methods=['FooBar.IDDQD'])
     self.assertFalse(petour.petours())
Ejemplo n.º 47
0
 def test_nonExistingFreeFuncs(self):
     self.assertFalse(
         petour.patch('petourtest.sut_', free_func_names=['foo2bar']))
Ejemplo n.º 48
0
 def test_nonExistingPackage(self):
     self.assertFalse(petour.patch('doom2.level', free_func_names=['beef']))
Ejemplo n.º 49
0
 def test_nonExistingPackage(self):
     self.assertFalse(petour.patch('doom2.level', free_func_names=['beef']))
Ejemplo n.º 50
0
 def test_nonExistingModule(self):
     self.assertFalse(petour.patch('petourtest.level', free_func_names=['beef']))
Ejemplo n.º 51
0
 def test_canNotPatchStaticVariable(self):
     petour.patch('petourtest.sut_', free_func_names=['beef'])
     self.assertFalse(petour.petours())
Ejemplo n.º 52
0
 def test_nonExistingFreeFuncs(self):
     self.assertFalse(petour.patch('petourtest.sut_', free_func_names=['foo2bar']))
Ejemplo n.º 53
0
 def test_builtins_freeFunction(self):
     petour.patch('__builtin__', free_func_names=['abs'], ctx=self.ctx)
     abs(-10)
Ejemplo n.º 54
0
 def test_nonExistingMethod(self):
     self.assertFalse(petour.patch('petourtest.sut_', class_dot_methods=['FooBar.Count']))
Ejemplo n.º 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'))
Ejemplo n.º 56
0
 def test_nonExistingNonCallable(self):
     self.assertFalse(petour.patch('petourtest.sut_', class_dot_methods=['FooBar.IDNOCLIP']))
Ejemplo n.º 57
0
 def test_expectPetourCount(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertEqual(1, len(petour.petours()))
Ejemplo n.º 58
0
 def test_unpatchAll_expectNoPetourCount(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     petour.unpatch_all()
     self.assertFalse(petour.petours())
Ejemplo n.º 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))
Ejemplo n.º 60
0
 def test_useWrongKey_expectNoContextManagerObject(self):
     petour.patch('petourtest.sut_', free_func_names=['foobar'])
     self.assertFalse(petour.petour('module.something', 'foobar'))