[docs]  1# @Test suite for needextend RST generation from extracted markers, TEST_WRITE_1, test, [IMPL_CLI_WRITE]
  2import pytest
  3
  4from sphinx_codelinks.analyse.models import MarkedContentType
  5from sphinx_codelinks.needextend_write import (
  6    MarkedContentSchema,
  7    convert_marked_content,
  8)
  9
 10
 11@pytest.mark.parametrize(
 12    ("markers", "texts"),
 13    [
 14        (
 15            [
 16                {
 17                    "filepath": "/home/jui-wen/git_repo/ub/sphinx-codelinks/tests/data/need_id_refs/dummy_1.cpp",
 18                    "remote_url": "https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L3",
 19                    "source_map": {
 20                        "start": {"row": 2, "column": 13},
 21                        "end": {"row": 2, "column": 51},
 22                    },
 23                    "tagged_scope": "void dummy_func1(){\n     //...\n }",
 24                    "need_ids": ["NEED_001", "NEED_002", "NEED_003", "NEED_004"],
 25                    "marker": "@need-ids:",
 26                    "type": "need-id-refs",
 27                },
 28            ],
 29            [
 30                ".. needextend:: NEED_001\n   :remote-url: https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L3\n\n",
 31                ".. needextend:: NEED_002\n   :remote-url: https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L3\n\n",
 32                ".. needextend:: NEED_003\n   :remote-url: https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L3\n\n",
 33                ".. needextend:: NEED_004\n   :remote-url: https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L3\n\n",
 34            ],
 35        ),
 36        (
 37            [
 38                {
 39                    "filepath": "/home/jui-wen/git_repo/ub/sphinx-codelinks/tests/data/need_id_refs/dummy_1.cpp",
 40                    "remote_url": "https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L3",
 41                    "source_map": {
 42                        "start": {"row": 2, "column": 13},
 43                        "end": {"row": 2, "column": 51},
 44                    },
 45                    "tagged_scope": "void dummy_func1(){\n     //...\n }",
 46                    "need_ids": ["NEED_001"],
 47                    "marker": "@need-ids:",
 48                    "type": "need-id-refs",
 49                },
 50                {
 51                    "filepath": "/home/jui-wen/git_repo/ub/sphinx-codelinks/tests/data/need_id_refs/dummy_1.cpp",
 52                    "remote_url": "https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L10",
 53                    "source_map": {
 54                        "start": {"row": 2, "column": 13},
 55                        "end": {"row": 2, "column": 51},
 56                    },
 57                    "tagged_scope": "void dummy_func1(){\n     //...\n }",
 58                    "need_ids": ["NEED_001"],
 59                    "marker": "@need-ids:",
 60                    "type": "need-id-refs",
 61                },
 62            ],
 63            [
 64                ".. needextend:: NEED_001\n   :remote-url: https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L3,https://github.com/useblocks/sphinx-codelinks/blob/main/tests/data/need_id_refs/dummy_1.cpp#L10\n\n"
 65            ],
 66        ),
 67    ],
 68)
 69def test_convert_marked_content(markers, texts):
 70    # Normalize line endings
 71    texts = [line.replace("\r\n", "\n").replace("\r", "\n") for line in texts]
 72    needextend_texts, errors = convert_marked_content(markers)
 73
 74    assert not errors
 75
 76    assert needextend_texts == texts
 77
 78
 79def test_convert_marked_content_ignores_multiline_needs() -> None:
 80    """``write rst`` converts the references only: a multi-line need record is valid
 81    input and yields nothing."""
 82    markers = [
 83        {
 84            "filepath": "src/demo.cpp",
 85            "remote_url": None,
 86            "source_map": {
 87                "start": {"row": 0, "column": 3},
 88                "end": {"row": 5, "column": 11},
 89            },
 90            "tagged_scope": None,
 91            "type": "multiline-need",
 92            "need": {"type": "impl", "title": "Demo", "content": "", "doctype": ".rst"},
 93            "markup": "rst",
 94            "source": {"project": "p", "path": "src/demo.cpp", "root": "git"},
 95        },
 96        {
 97            "filepath": "src/demo.cpp",
 98            "remote_url": "https://example.com/src/demo.cpp#L8",
 99            "source_map": {
100                "start": {"row": 7, "column": 13},
101                "end": {"row": 7, "column": 21},
102            },
103            "tagged_scope": None,
104            "need_ids": ["NEED_001"],
105            "marker": "@need-ids:",
106            "type": "need-id-refs",
107        },
108    ]
109
110    needextend_texts, errors = convert_marked_content(markers)
111
112    assert not errors
113    assert needextend_texts == [
114        ".. needextend:: NEED_001\n   :remote-url: https://example.com/src/demo.cpp#L8\n\n"
115    ]
116
117
118def test_a_multiline_need_record_needs_its_need() -> None:
119    schema = MarkedContentSchema(
120        filepath="src/demo.cpp",
121        remote_url=None,
122        source_map={"start": {"row": 0, "column": 0}, "end": {"row": 1, "column": 0}},
123        tagged_scope=None,
124        type=MarkedContentType.multiline_need,
125    )
126
127    assert schema.check_conditional_required_fields() == [
128        "Need definition is required for marked content of type 'multiline-need'"
129    ]