Ejemplo n.º 1
0
    def visit_Expr(self, expression_node):
        value = expression_node.value
        if not isinstance(value, _ast.BinOp) or not isinstance(
                value.op, _ast.Mult) or not isinstance(value.right, _ast.Call):
            return expression_node

        number_of_invocations = value.left
        if MockAssertionTransformer._value_is_a_wildcard(
                number_of_invocations):
            number_of_invocations = ast_proxy.ast_num(n=-1)
        target_mock = value.right.func.value
        target_method = ast_proxy.ast_str(s=value.right.func.attr)

        list_of_arguments = [
            MockAssertionTransformer._transform_arg_if_wildcard(x)
            for x in value.right.args
        ]
        spread_list_of_arguments = _ast.Starred(value=_ast.List(
            elts=list_of_arguments, ctx=_ast.Load()),
                                                ctx=_ast.Load())
        expression_node.value = _ast.Call(
            func=_ast.Attribute(value=_ast.Name(id='self', ctx=_ast.Load()),
                                attr='_assert_mock',
                                ctx=_ast.Load()),
            args=[
                number_of_invocations, target_mock, target_method,
                spread_list_of_arguments
            ],
            keywords=[])
        return expression_node
Ejemplo n.º 2
0
    def visit_Expr(self, expression_node):
        value = expression_node.value
        if not isinstance(value, _ast.Compare):
            if isinstance(value, _ast.BinOp):
                if hasattr(value,
                           'op') and not isinstance(value.op, _ast.MatMult):
                    return expression_node
            else:
                return expression_node

        left_value = value.left

        # TODO: Support multiple comparators (1 < 2 < 3). This may be tricky because one expression of multiple
        if hasattr(value, 'op'):
            comparison_operation = value.op
            right_value = value.right
        else:
            comparison_operation = value.ops[0]
            right_value = value.comparators[0]
        comparison_operation_type = type(comparison_operation)
        internal_comparison_type = self.comparator_methods[
            comparison_operation_type]

        expression_node.value = _ast.Call(
            func=_ast.Attribute(value=_ast.Name(id='self', ctx=_ast.Load()),
                                attr='_compare',
                                ctx=_ast.Load()),
            args=[
                left_value, right_value,
                ast_proxy.ast_str(s=internal_comparison_type.name)
            ],
            keywords=[])
        return expression_node
Ejemplo n.º 3
0
 def _assign_list_form_variables(self, where_function_ast_node):
     copy_of_body = copy.deepcopy(where_function_ast_node.body)
     where_function_ast_node.body = []
     for assignment_expression in copy_of_body:
         variable_name = assignment_expression.targets[0].id
         self.spec_metadata.add_feature_variable(self.feature_name, variable_name)
         variable_values = assignment_expression.value
         where_function_ast_node.body.append(
             _ast.Assign(
                 targets=[
                     _ast.Subscript(
                         value=_ast.Name(id='injectable_values', ctx=_ast.Load()),
                         slice=_ast.Index(value=ast_proxy.ast_str(s=variable_name)),
                         ctx=_ast.Store()
                     )
                 ],
                 value=variable_values
             ))
Ejemplo n.º 4
0
    def _assign_matrix_form_variables(self, where_function_ast_node):
        copy_of_body = copy.deepcopy(where_function_ast_node.body)
        where_function_ast_node.body = []

        variables_and_values = WhereBlockFunctions._get_variables_and_values(copy_of_body)

        # We might be screwing with line numbers here
        for variable_name, variable_values in variables_and_values.items():
            self.spec_metadata.add_feature_variable(self.feature_name, variable_name)
            where_function_ast_node.body.append(
                _ast.Assign(
                    targets=[
                        _ast.Subscript(
                            value=_ast.Name(id='injectable_values', ctx=_ast.Load()),
                            slice=_ast.Index(value=ast_proxy.ast_str(s=variable_name)),
                            ctx=_ast.Store()
                        )
                    ],
                    value=_ast.List(elts=variable_values, ctx=_ast.Load())
                ))
Ejemplo n.º 5
0
    def _transform_arg_if_wildcard(arg):
        if MockAssertionTransformer._value_is_a_wildcard(arg):
            return ast_proxy.ast_str(s='__nimoy_argument_wildcard')

        return arg
Ejemplo n.º 6
0
 def _replace_with_block_context(with_node, block_type):
     with_node.items[0].context_expr = _ast.Call(
         func=_ast.Attribute(value=_ast.Name(id='self', ctx=_ast.Load()), attr='_feature_block_context',
                             ctx=_ast.Load()), args=[ast_proxy.ast_str(s=block_type)], keywords=[])