test_prose_rst.py¶
tests/test_prose_rst.py
reStructuredText is prose, chunked by the sections its grammar finds.
test_a_named_target_opens_the_section_it_labels
None test_prose_rst.test_a_named_target_opens_the_section_it_labels()
A label travels with its section.
Checks
assert ".. _install:" in chunks["Install"]assert ".. _install:" not in chunks["Intro"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_a_named_target_opens_the_section_it_labels → chunk_rst |
| REQ-DDB-PIPE-012 | measured | test_a_named_target_opens_the_section_it_labels → chunk_rst → has_language |
Source · 12 lines
def test_a_named_target_opens_the_section_it_labels() -> None:
"""`.. _install:` above a title is that section's label (`:ref:`install``), so it is the
first line of the section's chunk and not the last line of the one before.
@brief A label travels with its section. @version 1
"""
text = "Intro\n=====\n\nHello.\n\n.. _install:\n\nInstall\n=======\n\nSteps.\n"
chunks = dict(chunk_rst(text, "docs/guide.rst"))
assert ".. _install:" in chunks["Install"]
assert ".. _install:" not in chunks["Intro"]
Activity · 4 steps
test_a_scanned_rst_file_is_ingested_as_prose
None test_prose_rst.test_a_scanned_rst_file_is_ingested_as_prose(Path tmp_path)
A scanned `.rst` anywhere in the tree becomes prose.
Checks
assert _chunks(db) == [("docs/install.rst", "Install")]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | test_a_scanned_rst_file_is_ingested_as_prose → ingest_supplementary_docs → _ingest_one_doc_file → memoized |
| REQ-DDB-PIPE-001 | measured | test_a_scanned_rst_file_is_ingested_as_prose → ingest_supplementary_docs |
| REQ-DDB-PIPE-012 | measured | |
| REQ-DDB-PIPE-014 | measured | test_a_scanned_rst_file_is_ingested_as_prose → ingest_supplementary_docs → within_repo |
Source · 15 lines
def test_a_scanned_rst_file_is_ingested_as_prose(tmp_path: Path) -> None:
"""The gap: `docs/install.rst` sits in the scanned set and no glob names it.
@brief A scanned `.rst` anywhere in the tree becomes prose. @version 1
"""
root = tmp_path / "repo"
_write(root, "docs/install.rst", "Install\n=======\n\nRun the installer.\n")
db = tmp_path / "clew.db"
db.touch()
ingest_supplementary_docs(db, root, files=["docs/install.rst"])
assert _chunks(db) == [("docs/install.rst", "Install")]
Activity · 6 steps
test_a_target_with_a_link_stays_with_the_paragraph_it_serves
None test_prose_rst.test_a_target_with_a_link_stays_with_the_paragraph_it_serves(str target)
Only internal targets label a section.
Checks
assert target in chunks["Intro"]assert target not in chunks["Sub"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_a_target_with_a_link_stays_with_the_paragraph_it_serves → chunk_rst |
| REQ-DDB-PIPE-012 | measured | test_a_target_with_a_link_stays_with_the_paragraph_it_serves → chunk_rst → has_language |
Source · 11 lines
def test_a_target_with_a_link_stays_with_the_paragraph_it_serves(target: str) -> None:
"""docutils labels a section only with an INTERNAL target: a name and nothing after
it. An external (`.. _ext: https://...`) or indirect (`.. _a: b_`) target is a link
destination for the text that references it, and must not move into the next section.
@brief Only internal targets label a section. @version 1
"""
text = f"Intro\n=====\n\nSee ext_ and a_.\n\n{target}\n\nSub\n===\n\nBody.\n"
chunks = dict(chunk_rst(text, "docs/guide.rst"))
assert target in chunks["Intro"]
assert target not in chunks["Sub"]1 more lines, not shown
Activity · 4 steps
test_a_title_shaped_line_inside_a_literal_block_does_not_split
None test_prose_rst.test_a_title_shaped_line_inside_a_literal_block_does_not_split()
Literal blocks are not titles.
Checks
assert [h for h, _ in chunks] == ["Real"]assert "Fake" in chunks[0][1]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_a_title_shaped_line_inside_a_literal_block_does_not_split → chunk_rst |
| REQ-DDB-PIPE-012 | measured | test_a_title_shaped_line_inside_a_literal_block_does_not_split → chunk_rst → has_language |
Source · 11 lines
def test_a_title_shaped_line_inside_a_literal_block_does_not_split() -> None:
"""The grammar knows a literal block; a regex over `====` lines would not.
@brief Literal blocks are not titles. @version 1
"""
text = "Real\n====\n\nExample::\n\n Fake\n ====\n\nDone.\n"
chunks = chunk_rst(text, "docs/guide.rst")
assert [h for h, _ in chunks] == ["Real"]
assert "Fake" in chunks[0][1]
Activity · 4 steps
test_an_anonymous_target_stays_with_the_paragraph_it_serves
None test_prose_rst.test_an_anonymous_target_stays_with_the_paragraph_it_serves()
Anonymous targets are not labels.
Checks
assert "__ http://example.org" in chunks["Intro"]assert "__ http" not in chunks["Next"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_an_anonymous_target_stays_with_the_paragraph_it_serves → chunk_rst |
| REQ-DDB-PIPE-012 | measured | test_an_anonymous_target_stays_with_the_paragraph_it_serves → chunk_rst → has_language |
Source · 11 lines
def test_an_anonymous_target_stays_with_the_paragraph_it_serves() -> None:
"""`__ http://...` is a link destination for the text before it, not a section label.
@brief Anonymous targets are not labels. @version 1
"""
text = "Intro\n=====\n\nSee `docs`__.\n\n__ http://example.org\n\nNext\n====\n\nMore.\n"
chunks = dict(chunk_rst(text, "docs/guide.rst"))
assert "__ http://example.org" in chunks["Intro"]
assert "__ http" not in chunks["Next"]
Activity · 4 steps
test_an_overlined_title_is_its_own_style
None test_prose_rst.test_an_overlined_title_is_its_own_style()
Overline distinguishes a style.
Checks
assert _headings(text) == ["Title", "Part"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_an_overlined_title_is_its_own_style → _headings → chunk_rst |
| REQ-DDB-PIPE-012 | measured | test_an_overlined_title_is_its_own_style → _headings → chunk_rst → has_language |
Source · 10 lines
def test_an_overlined_title_is_its_own_style() -> None:
"""An overlined `=` title and an underlined `=` title are two styles, so two levels;
the overlined one appears first and is the document title.
@brief Overline distinguishes a style. @version 1
"""
text = "=====\nTitle\n=====\n\nIntro.\n\nPart\n====\n\nBody.\n"
assert _headings(text) == ["Title", "Part"]
Activity · 2 steps
test_content_before_the_first_title_is_headed_by_the_file_name
None test_prose_rst.test_content_before_the_first_title_is_headed_by_the_file_name()
The preamble chunk, as `chunk_markdown` names it.
Checks
assert chunks[0] == ("guide.rst", ":Author: someone")
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_content_before_the_first_title_is_headed_by_the_file_name → chunk_rst |
| REQ-DDB-PIPE-012 | measured | test_content_before_the_first_title_is_headed_by_the_file_name → chunk_rst → has_language |
Source · 6 lines
def test_content_before_the_first_title_is_headed_by_the_file_name() -> None:
"""@brief The preamble chunk, as `chunk_markdown` names it. @version 1"""
chunks = chunk_rst(":Author: someone\n\nTitle\n=====\n\nBody.\n", "docs/guide.rst")
assert chunks[0] == ("guide.rst", ":Author: someone")
Activity · 2 steps
test_levels_follow_the_order_styles_first_appear
None test_prose_rst.test_levels_follow_the_order_styles_first_appear()
Order of appearance, not character, decides a level.
Checks
assert [h for h, _ in chunks] == ["Top", "Two", "Three", "Again two"]assert "Four" in dict(chunks)["Three"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_levels_follow_the_order_styles_first_appear → chunk_rst |
| REQ-DDB-PIPE-012 | measured | test_levels_follow_the_order_styles_first_appear → chunk_rst → has_language |
Source · 15 lines
def test_levels_follow_the_order_styles_first_appear() -> None:
"""docutils' rule: `~` used first is level 1, whatever the character. Level 4 (`-`
here) stays inside its parent's chunk, as a markdown `####` does.
@brief Order of appearance, not character, decides a level. @version 1
"""
text = (
"Top\n~~~\n\nA.\n\nTwo\n===\n\nB.\n\nThree\n^^^^^\n\nC.\n\n"
"Four\n----\n\nD.\n\nAgain two\n=========\n\nE.\n"
)
chunks = chunk_rst(text, "docs/guide.rst")
assert [h for h, _ in chunks] == ["Top", "Two", "Three", "Again two"]
assert "Four" in dict(chunks)["Three"]
Activity · 4 steps
test_readme_rst_is_split_by_the_rst_grammar_not_markdown
None test_prose_rst.test_readme_rst_is_split_by_the_rst_grammar_not_markdown(Path tmp_path)
README.rst splits at an rst-only title style.
Checks
assert _chunks(db) == [("README.rst", "Tool"), ("README.rst", "Usage")]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-INDEX-002 | measured | test_readme_rst_is_split_by_the_rst_grammar_not_markdown → ingest_supplementary_docs → _ingest_one_doc_file → memoized |
| REQ-DDB-PIPE-001 | measured | test_readme_rst_is_split_by_the_rst_grammar_not_markdown → ingest_supplementary_docs |
| REQ-DDB-PIPE-012 | measured | |
| REQ-DDB-PIPE-014 | measured | test_readme_rst_is_split_by_the_rst_grammar_not_markdown → ingest_supplementary_docs → within_repo |
Source · 16 lines
def test_readme_rst_is_split_by_the_rst_grammar_not_markdown(tmp_path: Path) -> None:
"""`README.rst` was always globbed, and was chunked as markdown. A `~` underline is no
markdown heading at all, so the second section never split.
@brief README.rst splits at an rst-only title style. @version 1
"""
root = tmp_path / "repo"
_write(root, "README.rst", "Tool\n~~~~\n\nIntro.\n\nUsage\n^^^^^\n\nRun it.\n")
db = tmp_path / "clew.db"
db.touch()
ingest_supplementary_docs(db, root, files=[])
assert _chunks(db) == [("README.rst", "Tool"), ("README.rst", "Usage")]
Activity · 6 steps
test_without_the_grammar_the_file_is_one_chunk
None test_prose_rst.test_without_the_grammar_the_file_is_one_chunk(pytest.MonkeyPatch monkeypatch)
Degrade to one chunk when the grammar is absent.
Checks
assert chunk_rst(text, "docs/guide.rst") == [("guide.rst", text.strip())]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-001 | measured | test_without_the_grammar_the_file_is_one_chunk → chunk_rst |
| REQ-DDB-PIPE-012 | measured | test_without_the_grammar_the_file_is_one_chunk → chunk_rst → has_language |
Source · 11 lines
def test_without_the_grammar_the_file_is_one_chunk(monkeypatch: pytest.MonkeyPatch) -> None:
"""A substrate built without `lang-rst` must not crash the build, and must not split by
a guess. The file is still searchable, whole, under its name.
@brief Degrade to one chunk when the grammar is absent. @version 1
"""
monkeypatch.setattr(substrate, "compiled_languages", lambda: frozenset({"markdown"}))
text = "Title\n=====\n\nBody.\n"
assert chunk_rst(text, "docs/guide.rst") == [("guide.rst", text.strip())]