コード例 #1
0
    def test_advice_builder_encore(self):
        builder = AdviceBuilder()
        builder.add_encore(Target.increment_count, increment_count)
        builder.apply()

        target = Target()
        target.increment_count()
        self.assertEqual(
            target.count, 1 + 1
        )  # Should have incremented after being initially incremented by an encore.
コード例 #2
0
    def test_advice_builder_error_handling(self):
        builder = AdviceBuilder()
        builder.add_error_handler(Target.raise_exception, handle_exception)
        builder.apply()

        target = Target()
        target.raise_exception()
        self.assertEqual(
            target.exceptions_handled,
            1)  # No error should be raised, we just increment when we handle.
コード例 #3
0
    def test_advice_builder_multiple_preludes(self):
        builder = AdviceBuilder()
        builder.add_prelude(Target.increment_count, increment_count)
        builder.add_prelude(Target.increment_count, increment_count)
        builder.apply()

        target = Target()
        target.increment_count()
        self.assertEqual(
            target.count, 1 + 1 + 1
        )  # Should have incremented after being incremented twice by two preludes.
コード例 #4
0
    def test_advice_builder_multiple_error_handlers(self):
        builder = AdviceBuilder()
        builder.add_error_handler(Target.raise_exception, handle_exception)
        builder.add_error_handler(Target.raise_exception, handle_exception)
        builder.apply()

        target = Target()
        target.raise_exception()
        self.assertEqual(
            target.exceptions_handled,
            2)  # We increment twice if we run the exception handler twice.
コード例 #5
0
    def test_advice_builder_with_advice_class(self):
        builder = AdviceBuilder()
        advice = TestAdviceClass()
        builder.add_advice(Target.noop, advice)
        builder.apply()

        target = Target()
        target.noop()

        self.assertEqual(target.count, 1 + 1)
        self.assertEqual(target.exceptions_handled, 1)
コード例 #6
0
    def test_advice_builder_with_dictionary_format_advice(self):
        builder = AdviceBuilder()
        advice = {Target.noop: TestAdviceClass()}
        builder.add_dictionary_advice(advice)
        builder.apply()

        target = Target()
        target.noop()

        self.assertEqual(target.count, 1 + 1)
        self.assertEqual(target.exceptions_handled, 1)
コード例 #7
0
    def test_advice_builder_multiple_encores(self):
        builder = AdviceBuilder()
        builder.add_encore(Target.increment_count, increment_count)
        builder.add_encore(Target.increment_count, increment_count)
        builder.apply()

        target = Target()
        target.increment_count()
        self.assertEqual(
            target.count, 1 + 1 + 1
        )  # Should have incremented, then incremented twice more by two encores.
コード例 #8
0
    def test_model_aspect_application(self):
        state = {'invocations': 0}
        advice = AdviceBuilder()

        encore = partial(counting_encore, state)
        advice.add_encore(CustomerServiceWorkflow.A_submitted, encore)
        advice.apply()

        self.company.recieve_message('start')
        self.clock.tick(2)

        self.assertEqual(state['invocations'], 1)
コード例 #9
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)
コード例 #10
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)
コード例 #11
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)
コード例 #12
0
    def test_model_fuzzing(self):
        def example_fuzzer(steps, context):
            steps[0].targets = []
            return steps

        set_fuzzer('a_submitted', example_fuzzer)

        exceptions_raised = []

        def handleExpectedErr(attribute, context, exception):
            exceptions_raised.append(exception)

        catch_expected_err = AdviceBuilder()
        catch_expected_err.add_error_handler(
            CustomerServiceWorkflow.A_submitted, handleExpectedErr)
        catch_expected_err.apply()

        self.company.recieve_message('start')
        self.clock.tick(2)
        self.assertTrue(len(exceptions_raised) is not 0)
コード例 #13
0
def remove_previous_action(_, __):
    action_log[-1].pop()

def repeat_step(attribute, actor):
    action_log[-1].append(action_log[-1][-1])


def repeat_step(_, __):
    action_log[-1].append(action_log[-1][-1])


mistake_points = []


builder = AdviceBuilder()

default_attention = 0.7
default_introspection = 0.5
class CompetenceModel(object):
    show_graphs = True
    def __init__(self, genius=0.1, learning_point=50, *args, **kwargs):
        super(CompetenceModel, self).__init__(*args, **kwargs)
        self.genius = genius
        self.learning_point = learning_point

    def around(self, attribute, actor, *args, **kwargs):
        '''
        To be applied to an Actor's get_next_task.
        :param attribute: An actor's get_next_task implementation
        :param actor: An actor