Repository: pytest-dev/pytest
File: testing/test_assertrewrite.py (Python)

Code before:
        assert isinstance(m.body[3], ast.Expr)

    def test_location_is_set(self) -> None:
        s = textwrap.dedent(
            """

        assert False, (

            "Ouch"
          )

        """
        )
        m = rewrite(s)
        for node in m.body:
            if isinstance(node, ast.Import):
                continue
            for n in [node, *ast.iter_child_nodes(node)]:
                assert isinstance(n, (ast.stmt, ast.expr))
                for location in [
                    (n.lineno, n.col_offset),
                    (n.end_lineno, n.end_col_offset),
                ]:
                    assert (3, 0) <= location <= (6, 3)

    def test_positions_are_preserved(self) -> None:
        def preserved(code):
            s = textwrap.dedent(code)
            locations = []

            def loc(msg=None):
                frame = inspect.currentframe()
                assert frame
                frame = frame.f_back
                assert frame
                frame = frame.f_back
                assert frame

                offset = frame.f_lasti

                instructions = {i.offset: i for i in dis.get_instructions(frame.f_code)}

                # skip CACHE instructions
                while offset not in instructions and offset >= 0:
                    offset -= 1

                instruction = instructions[offset]
                if sys.version_info >= (3, 11):
                    position = instruction.positions
                else:
                    position = instruction.starts_line

Review comment (suggestion):
```suggestion
    def test_positions_are_preserved(self) -> None:
        """Ensure AST positions are preserved during rewriting (#12818)."""
```

Code after:
        assert isinstance(m.body[3], ast.Expr)

    def test_location_is_set(self) -> None:
        s = textwrap.dedent(
            """

        assert False, (

            "Ouch"
          )

        """
        )
        m = rewrite(s)
        for node in m.body:
            if isinstance(node, ast.Import):
                continue
            for n in [node, *ast.iter_child_nodes(node)]:
                assert isinstance(n, (ast.stmt, ast.expr))
                for location in [
                    (n.lineno, n.col_offset),
                    (n.end_lineno, n.end_col_offset),
                ]:
                    assert (3, 0) <= location <= (6, 3)

    def test_positions_are_preserved(self) -> None:
        """Ensure AST positions are preserved during rewriting (#12818)."""

        def preserved(code: str) -> None:
            s = textwrap.dedent(code)
            locations = []

            def loc(msg: str | None = None) -> None:
                frame = inspect.currentframe()
                assert frame
                frame = frame.f_back
                assert frame
                frame = frame.f_back
                assert frame

                offset = frame.f_lasti

                instructions = {i.offset: i for i in dis.get_instructions(frame.f_code)}

                # skip CACHE instructions
                while offset not in instructions and offset >= 0:
                    offset -= 1

                instruction = instructions[offset]
                if sys.version_info >= (3, 11):
                    position = instruction.positions