162 lines
8.8 KiB
Python
162 lines
8.8 KiB
Python
import pathlib
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from adf_codec import convert
|
|
from failure import Failure
|
|
from plan.touches import overlaps, Part, predicted, rendered, together
|
|
|
|
DETAIL = """A planning call.
|
|
|
|
Touches:
|
|
|
|
- `bin/a.py`: `main`, `Plan.branch` (small)
|
|
- `README.md`: `## Goals, and (more): things`
|
|
- `bin/run.sh`: lines 10-20
|
|
- `docs/new.md`
|
|
|
|
More prose, with `bin/b.py` named.
|
|
"""
|
|
|
|
def text(words: str, code: bool = False) -> dict:
|
|
return {"type": "text", "text": words, **({"marks": [{"type": "code"}]} if code else {})}
|
|
|
|
|
|
def bullet_of(*spans: dict) -> dict:
|
|
return {"type": "listItem", "attrs": {"localId": "i"}, "content": [{"type": "paragraph", "attrs": {"localId": "p"}, "content": list(spans)}]}
|
|
|
|
|
|
# A Touches list as someone types it in Jira's editor: a paragraph, then a bulleted list whose paths and names carry the code mark.
|
|
JIRA_TOUCHES = {"type": "doc", "version": 1, "content": [
|
|
{"type": "paragraph", "attrs": {"localId": "a"}, "content": [text("A planning call.")]},
|
|
{"type": "paragraph", "attrs": {"localId": "b"}, "content": [text("Touches:")]},
|
|
{"type": "bulletList", "attrs": {"localId": "c"}, "content": [
|
|
bullet_of(text("bin/a.py", True), text(": "), text("main", True), text(", "), text("Plan.branch", True), text(" (small)")),
|
|
bullet_of(text("a`b.md", True), text(": "), text("## The `foo` tool", True), text(" (small)"), text(", "), text("`x`", True)),
|
|
bullet_of(text("bin/run.sh", True), text(": lines 10-20")),
|
|
bullet_of(text("docs/new.md", True)),
|
|
]},
|
|
{"type": "paragraph", "attrs": {"localId": "d"}, "content": [text("More prose.")]},
|
|
]}
|
|
|
|
|
|
class PredictedTest(unittest.TestCase):
|
|
def test_reads_each_bullet_of_the_touches_list(self) -> None:
|
|
self.assertEqual(predicted(DETAIL), {
|
|
"bin/a.py": (Part("main"), Part("Plan.branch", small=True)),
|
|
"README.md": (Part("## Goals, and (more): things"),),
|
|
"bin/run.sh": (Part("lines 10-20"),),
|
|
"docs/new.md": (),
|
|
})
|
|
|
|
def test_a_detail_without_the_list_predicts_nothing(self) -> None:
|
|
self.assertIsNone(predicted("Only prose, with `bin/a.py` named."))
|
|
|
|
def test_a_bullet_it_cannot_read_is_a_failure_that_quotes_it(self) -> None:
|
|
for bullet in ("- bin/a.py: main", "- `bin/a.py`: main", "- `bin/a.py`: lines 20-10", "- `bin/a.py`: `main` `other`"):
|
|
with self.subTest(bullet):
|
|
found = predicted(f"Touches:\n\n{bullet}\n")
|
|
assert isinstance(found, Failure)
|
|
self.assertIn(bullet, found.reason)
|
|
|
|
def test_an_empty_list_is_a_failure(self) -> None:
|
|
self.assertIsInstance(predicted("Touches:\n\nProse.\n"), Failure)
|
|
found = predicted("Touches:\n")
|
|
assert isinstance(found, Failure)
|
|
self.assertEqual(found.reason, "no bullet follows `Touches:`")
|
|
|
|
def test_reads_a_list_right_under_its_label_a_one_line_range_and_a_name_of_any_letters(self) -> None:
|
|
self.assertEqual(predicted("Touches:\n- `a.py`: `Xray`, lines 5-5\n"), {"a.py": (Part("Xray"), Part("lines 5-5"))})
|
|
|
|
def test_rendered_reads_back(self) -> None:
|
|
touches = predicted(DETAIL)
|
|
assert isinstance(touches, dict)
|
|
self.assertEqual(predicted(f"Touches:\n\n{rendered(touches)}\n"), touches)
|
|
|
|
def test_a_name_that_holds_backticks_takes_a_longer_run_with_a_space_inside_each_end(self) -> None:
|
|
self.assertEqual(predicted("Touches:\n- `a.md`: `` ## The `foo` tool `` (small), ``` `` ```, `` `x` ``\n"), {"a.md": (Part("## The `foo` tool", small=True), Part("``"), Part("`x`"))})
|
|
self.assertEqual(rendered({"a.md": (Part("## The `foo` tool", small=True), Part("``"))}), "- `a.md`: `` ## The `foo` tool `` (small), ``` `` ```")
|
|
|
|
def test_a_longer_run_without_padding_spaces_is_the_text_between_its_runs(self) -> None:
|
|
self.assertEqual(predicted("Touches:\n- ``a`b.md``: ``## The `foo` tool`` (small), ``` `` ```, `` `x` ``\n"), {"a`b.md": (Part("## The `foo` tool", small=True), Part("``"), Part("`x`"))})
|
|
|
|
def test_a_longer_run_with_a_space_at_one_end_only_keeps_it(self) -> None:
|
|
self.assertEqual(predicted("Touches:\n- `a.md`: `` x``, ``y ``, `` a ``\n"), {"a.md": (Part(" x"), Part("y "), Part("a"))})
|
|
|
|
def test_a_list_written_in_markdown_and_read_back_from_a_jira_description_reads_as_written(self) -> None:
|
|
description = convert.markdown_to_adf(DETAIL)
|
|
assert isinstance(description, dict), description
|
|
self.assertEqual(predicted(convert.adf_to_markdown(description)), predicted(DETAIL))
|
|
|
|
def test_a_list_typed_in_jiras_editor_reads_as_its_files_and_parts(self) -> None:
|
|
read = convert.adf_to_markdown(JIRA_TOUCHES)
|
|
assert isinstance(read, str), read
|
|
self.assertEqual(predicted(read), {
|
|
"bin/a.py": (Part("main"), Part("Plan.branch", small=True)),
|
|
"a`b.md": (Part("## The `foo` tool", small=True), Part("`x`")),
|
|
"bin/run.sh": (Part("lines 10-20"),),
|
|
"docs/new.md": (),
|
|
})
|
|
|
|
def test_a_name_ends_only_at_a_run_as_long_as_the_one_that_opens_it(self) -> None:
|
|
self.assertEqual(predicted("Touches:\n- `a.md`: `` a ``` b ``, `c``d`\n"), {"a.md": (Part("a ``` b"), Part("c``d"))})
|
|
|
|
def test_a_path_holds_any_character_and_a_single_backtick_keeps_a_names_spaces(self) -> None:
|
|
self.assertEqual(predicted("Touches:\n- `my file.md`: ` x `\n- `` a`b.md ``\n"), {"my file.md": (Part(" x "),), "a`b.md": ()})
|
|
|
|
def test_rendered_reads_back_whatever_a_path_or_a_name_holds(self) -> None:
|
|
touches = {"my file.md": (Part("## todo.md: under `Touches:` in its detail"), Part("## ``a`` and `b`", small=True), Part("`"), Part("## ends in `x`"), Part(" x "), Part("lines 2-3")), "a`b.md": (), "c, d: e.md": (Part("# T"),)}
|
|
self.assertEqual(predicted(f"Touches:\n\n{rendered(touches)}\n"), touches)
|
|
|
|
def test_a_bullet_with_an_unclosed_run_a_blank_name_or_a_stray_separator_is_a_failure(self) -> None:
|
|
for bullet in ("- `a.md`: `` ## A", "- `a.md`: `` ## A `", "- `a.md`: `` ``", "- `a.md`: ", "- `a.md`:", "- `a.md`: `f`, ", "- `a.md`: `f`,`g`", "- ``", "- `a.md` (small)", "- `a.md`: `f` (small) (small)"):
|
|
with self.subTest(bullet):
|
|
self.assertIsInstance(predicted(f"Touches:\n\n{bullet}\n"), Failure)
|
|
|
|
def test_a_range_that_runs_backwards_is_a_failure_that_says_so(self) -> None:
|
|
found = predicted("Touches:\n\n- `bin/a.py`: `f`, lines 20-10\n")
|
|
assert isinstance(found, Failure)
|
|
self.assertEqual(found.reason, "the bullet '- `bin/a.py`: `f`, lines 20-10' under `Touches:` holds a line range whose first line comes after its last")
|
|
|
|
def test_together_joins_items_and_keeps_small_only_where_each_says_so(self) -> None:
|
|
joined = together([{"a.py": (Part("f", small=True), Part("g", small=True))}, {"a.py": (Part("f"),), "b.md": ()}, {"b.md": (Part("# T"),)}])
|
|
self.assertEqual(joined, {"a.py": (Part("f"), Part("g", small=True)), "b.md": ()})
|
|
|
|
def test_together_keeps_a_part_not_small_once_one_says_so_and_reads_on_past_a_whole_file(self) -> None:
|
|
joined = together([{"a.py": (Part("f"),), "b.md": ()}, {"b.md": (Part("# T"),), "a.py": (Part("f", small=True),), "c.py": (Part("h"),)}])
|
|
self.assertEqual(joined, {"a.py": (Part("f"),), "b.md": (), "c.py": (Part("h"),)})
|
|
|
|
|
|
class OverlapsTest(unittest.TestCase):
|
|
def test_other_files_and_other_parts_do_not_overlap(self) -> None:
|
|
self.assertEqual(overlaps({"a.py": (Part("f"),)}, {"a.py": (Part("g"),), "b.py": ()}), [])
|
|
|
|
def test_the_same_part_overlaps(self) -> None:
|
|
self.assertEqual(overlaps({"a.py": (Part("f"), Part("g"))}, {"a.py": (Part("g"),)}), ["`a.py` `g`"])
|
|
|
|
def test_a_class_overlaps_its_method_but_not_a_name_it_only_starts(self) -> None:
|
|
self.assertEqual(overlaps({"a.py": (Part("Plan"),)}, {"a.py": (Part("Plan.branch"), Part("Planned"))}), ["`a.py` `Plan`"])
|
|
|
|
def test_a_whole_file_overlaps_any_part_of_it(self) -> None:
|
|
self.assertEqual(overlaps({"a.py": ()}, {"a.py": (Part("g", small=True),)}), ["`a.py`"])
|
|
|
|
def test_reads_on_past_a_file_only_one_touches_and_past_a_whole_file(self) -> None:
|
|
theirs = {"a.py": (), "b.py": (Part("g"),)}
|
|
self.assertEqual(overlaps({"z.py": (Part("f"),), "a.py": (), "b.py": (Part("g"),)}, theirs), ["`a.py`", "`b.py` `g`"])
|
|
|
|
def test_line_ranges_overlap_where_they_share_a_line(self) -> None:
|
|
self.assertEqual(overlaps({"x.sh": (Part("lines 1-10"),)}, {"x.sh": (Part("lines 10-12"),)}), ["`x.sh` lines 1-10"])
|
|
self.assertEqual(overlaps({"x.sh": (Part("lines 1-9"),)}, {"x.sh": (Part("lines 10-12"),)}), [])
|
|
self.assertEqual(overlaps({"x.sh": (Part("lines 10-12"),)}, {"x.sh": (Part("lines 1-10"),)}), ["`x.sh` lines 10-12"])
|
|
self.assertEqual(overlaps({"x.sh": (Part("lines 11-12"),)}, {"x.sh": (Part("lines 1-10"),)}), [])
|
|
|
|
def test_a_line_range_overlaps_a_named_part_since_nothing_tells_them_apart(self) -> None:
|
|
self.assertEqual(overlaps({"a.py": (Part("lines 1-9"),)}, {"a.py": (Part("f"),)}), ["`a.py` lines 1-9"])
|
|
self.assertEqual(overlaps({"a.py": (Part("f"),)}, {"a.py": (Part("lines 1-9"),)}), ["`a.py` `f`"])
|
|
|
|
def test_a_small_change_on_either_side_is_taken(self) -> None:
|
|
self.assertEqual(overlaps({"a.py": (Part("f", small=True),)}, {"a.py": (Part("f"),)}), [])
|
|
self.assertEqual(overlaps({"a.py": (Part("f"),)}, {"a.py": (Part("f", small=True),)}), [])
|