Exemplo n.º 1
0
    def test_diff_with_render_false_renders_nothing(self):
        element = parser.Diff(
            name="example",
            content=dedent("""
                --- old
                +++ new

                @@ -1,2 +1,2 @@
                -x = 1
                +x = 2
                 print(x)

            """),
            render=False,
        )
        state = {
            "example": compiler.Code(
                language="python",
                content="x = 1\nprint(x)\n",
                pending_lines=(),
            ),
        }

        new_state, new_element = _execute(state, element)
        assert_that(new_element, is_empty_element)
        assert_that(new_state["example"], has_attrs(pending_lines=is_sequence("x = 2")))
Exemplo n.º 2
0
    def test_diff_updates_code_using_content_as_patch(self):
        element = parser.Diff(
            name="example",
            content=dedent("""
                --- old
                +++ new

                @@ -1,2 +1,2 @@
                -x = 1
                +x = 2
                 print(x)

            """),
            render=False,
        )
        state = {
            "example": _create_code(
                language="python",
                content="x = 1\nprint(x)\n",
            ),
        }

        new_state, new_element = _execute(state, element)
        assert_that(new_state, is_mapping({
            "example": is_code(
                language="python",
                content="x = 2\nprint(x)\n",
            ),
        }))
Exemplo n.º 3
0
    def test_invalid_diff_raises_error(self):
        element = parser.Diff(
            name="example",
            content=dedent("""
                --- old
                +++ new

                @@ -1,2 +1,2 @@
                -x = 3
                +x = 2
                 print(x)

            """),
            render=False,
        )
        state = {
            "example": _create_code(
                language="python",
                content="x = 1\nprint(x)\n",
                pending_lines=(),
            ),
        }

        error = pytest.raises(ValueError, lambda: _execute(state, element, line_number=42))
        assert_that(str(error.value), equal_to("cannot apply diff on line number 42, invalid patch"))
Exemplo n.º 4
0
    def test_diff_with_render_true_renders_content(self):
        # TODO: handle rendering when last_render is out of date
        content = dedent("""
            --- old
            +++ new

            @@ -1,2 +1,2 @@
            -x = 1
            +x = 2
             print(x)

        """)
        element = parser.Diff(
            name="example",
            content=content,
            render=True,
        )
        state = {
            "example": compiler.Code(
                language="python",
                content="x = 1\nprint(x)\n",
                pending_lines=(),
            ),
        }

        new_state, new_element = _execute(state, element)
        assert_that(new_element, is_literal_block(content=content))
        assert_that(new_state["example"], has_attrs(pending_lines=is_sequence()))
Exemplo n.º 5
0
    def test_converting_from_diff_to_replace_generates_replace_block(self):
        start = parser.Start(
            name="example",
            language="python",
            render=True,
            content=dedent("""
                x = 1
                print(x)

            """),
        )
        diff = parser.Diff(
            name="example",
            render=False,
            content=dedent("""
                --- old
                +++ new

                @@ -1,2 +1,2 @@
                -x = 1
                +x = 2
                 print(x)

            """),
        )

        output = compiler.convert_block(
            source=(
                (1, start),
                (2, diff),
            ),
            line_number=2,
            block_type="replace",
        )

        assert_that(output, is_sequence(
            is_start(),
            is_replace(
                name="example",
                render=False,
                content=dedent("""
                    x = 2
                    print(x)

                """)
            ),
        ))