Esempio n. 1
0
    def test_generated_advice(self):
        def add_three(attribute, context, *args, **kwargs):
            context.count += 3

        def mult_by_2(attribute, context, result):
            context.count *= 2

        generated_around = generate_around_advice(add_three, mult_by_2)
        builder = AdviceBuilder()
        builder.add_around(Target.increment_count, generated_around)
        builder.apply()

        target = Target()
        target.increment_count()
        self.assertEqual(target.count, (3 + 1) * 2)
Esempio n. 2
0
    def test_advice_builder_around(self):
        def around_to_test(attribute, context, *args, **kwargs):
            '''
            Add three, run the original (going to add another one), then multiply by two.
            '''
            context.count += 3
            result = attribute(*args, **kwargs)
            context.count *= 2
            return result

        builder = AdviceBuilder()
        builder.add_around(Target.increment_count, around_to_test)
        builder.apply()

        target = Target()
        target.increment_count()
        self.assertEqual(target.count, (3 + 1) * 2)
Esempio n. 3
0
    def test_multiple_arounds(self):
        def around_to_test(attribute, context, *args, **kwargs):
            '''
            Add three, run the original (going to add another one), then multiply by two.
            '''
            context.count += 3
            result = attribute(*args, **kwargs)
            context.count *= 2
            return result

        builder = AdviceBuilder()
        builder.add_around(Target.increment_count, around_to_test)
        builder.add_around(Target.increment_count, around_to_test)
        builder.apply()

        target = Target()
        target.increment_count()
        # We want arounds to wrap around each other, so we want to add three twice, then increment, then mult by 4.
        self.assertEqual(target.count, ((3 + (3 + 1)) * 2) * 2)