Example #1
0
 def visit_default_test_case(self, test_case: dtc.DefaultTestCase) -> None:
     statement_visitor = stmt_to_ast.StatementToAstVisitor(
         self._module_aliases, NamingScope(), self._wrap_code
     )
     for statement in test_case.statements:
         statement.accept(statement_visitor)
     self._test_case_asts.append(statement_visitor.ast_nodes)
Example #2
0
    def executable_node_for(
        self,
        statement: stmt.Statement,
    ) -> ast.Module:
        """Transforms the given statement in an executable ast node.

        Args:
            statement: The statement that should be converted.

        Returns:
            An executable ast node.
        """
        modules_before = len(self._modules_aliases.known_name_indices)
        visitor = stmt_to_ast.StatementToAstVisitor(
            self._modules_aliases, self._variable_names
        )
        statement.accept(visitor)
        if modules_before != len(self._modules_aliases.known_name_indices):
            # new module added
            # TODO(fk) cleaner solution?
            self._global_namespace = ExecutionContext._create_global_namespace(
                self._modules_aliases
            )
        assert (
            len(visitor.ast_nodes) == 1
        ), "Expected statement to produce exactly one ast node"
        return ExecutionContext._wrap_node_in_module(visitor.ast_nodes[0])
Example #3
0
def test_statement_to_ast_with_wrap():
    var_names = NamingScope()
    module_aliases = NamingScope(prefix="module")
    statement_to_ast_visitor = stmt_to_ast.StatementToAstVisitor(
        module_aliases, var_names, True)
    int_stmt = MagicMock(stmt.Statement)
    int_stmt.value = 5
    statement_to_ast_visitor.visit_int_primitive_statement(int_stmt)
    assert (astor.to_source(Module(body=statement_to_ast_visitor.ast_nodes)) ==
            "try:\n    var0 = 5\nexcept BaseException:\n    pass\n")
Example #4
0
 def visit_default_test_case(self, test_case: dtc.DefaultTestCase) -> None:
     variables = NamingScope()
     statement_visitor = stmt_to_ast.StatementToAstVisitor(
         self._module_aliases, variables, self._wrap_code)
     for statement in test_case.statements:
         statement.accept(statement_visitor)
         # TODO(fk) better way. Nest visitors?
         assertion_visitor = ata.AssertionToAstVisitor(
             self._common_modules, variables)
         for assertion in statement.assertions:
             assertion.accept(assertion_visitor)
         statement_visitor.append_nodes(assertion_visitor.nodes)
     self._test_case_asts.append(statement_visitor.ast_nodes)
    def _to_ast_nodes(
        test_case: tc.TestCase,
        variable_names: NamingScope,
        modules_aliases: NamingScope,
    ) -> List[ast.stmt]:
        """Transforms the given test case into a list of ast nodes.

        :param test_case: The current test case
        :param variable_names: The scope of the variable names
        :param modules_aliases: The cope of the module alias names
        :return: A list of ast nodes
        """
        visitor = stmt_to_ast.StatementToAstVisitor(modules_aliases,
                                                    variable_names)
        for statement in test_case.statements:
            statement.accept(visitor)
        return visitor.ast_nodes
Example #6
0
def statement_to_ast_visitor() -> stmt_to_ast.StatementToAstVisitor:
    var_names = NamingScope()
    module_aliases = NamingScope(prefix="module")
    return stmt_to_ast.StatementToAstVisitor(module_aliases, var_names)