Skip to content

Commit

Permalink
improve coverage and regex
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-kirkbride committed Jul 1, 2023
1 parent cd3841e commit 98d3497
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion porcupine/plugins/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _is_list_item(line: str) -> bool:
- https://pandoc.org/MANUAL.html#lists
"""
assert len(line.splitlines()) == 1
pattern = r"^\s*\d{1,9}[.)]|^\s*[-+*]|^\s*#\)|^\s*#\."
pattern = r"(^\s*\d{1,9}[.)]|^\s*[-+*]|^\s*#\)|^\s*#\.) .*"
regex = re.compile(pattern)
match = regex.search(line)
return bool(match)
Expand Down
58 changes: 45 additions & 13 deletions tests/test_markdown_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,52 @@ class IsListItemCase(NamedTuple):
IsListItemCase(id="# bad separator \\", line="#\\ item 1", expected=False),
IsListItemCase(id="ol bad separator |", line="8| item 1", expected=False),
IsListItemCase(id="ol bad separator /", line="8/ item 1", expected=False),
IsListItemCase(id="ol bad separator \\", line="8\\ item 1", expected=False),
IsListItemCase(
id="ol bad separator \\", line="8\\ item 1", expected=False
),
IsListItemCase(id="not a list 1", line="item 1", expected=False),
IsListItemCase(id="not a list 2", line=" item 1", expected=False),
IsListItemCase(id="not a list 3", line=" item 1", expected=False),
IsListItemCase(id="not a list 4", line="& item 1", expected=False),
IsListItemCase(id="not a list 5", line="^ item 1", expected=False),
IsListItemCase(id="duplicate token 1", line="-- item 1", expected=True),
IsListItemCase(id="duplicate token 2", line="--- item 1", expected=True),
IsListItemCase(id="duplicate token 1", line="-- item 1", expected=False),
IsListItemCase(id="duplicate token 2", line="--- item 1", expected=False),
IsListItemCase(id="duplicate token 3", line="- - - item 1", expected=True),
IsListItemCase(id="duplicate token 4", line=" - item -- 1 -", expected=True),
IsListItemCase(id="duplicate token 5", line=" -#) item -- 1 -", expected=True),
IsListItemCase(id="duplicate token 6", line=" *-#)1. item -- 1 -", expected=True),
IsListItemCase(
id="duplicate token 4", line=" - item -- 1 -", expected=True
),
IsListItemCase(
id="duplicate token 5", line=" -#) item -- 1 -", expected=False
),
IsListItemCase(
id="duplicate token 6", line=" *-#)1. item -- 1 -", expected=False
),
]

# test `#` and 0 to 99 numbered lists
# tests ol with `.` and `)`
IS_LIST_ITEM_CASES.extend(
[
IsListItemCase(id=f"numbered {i}", line=f"{i}{sep} item 1", expected=True)
for i, sep in itertools.product(itertools.chain(range(100), "#"), (".", ")"))
IsListItemCase(
id=f"numbered {i}", line=f"{i}{sep} item 1", expected=True
)
for i, sep in itertools.product(
itertools.chain(range(100), "#"), (".", ")")
)
]
)

# test raw li prefixes with and without space
IS_LIST_ITEM_CASES.extend(
[
IsListItemCase(
id=f"raw prexix {prefix} no space",
line=f"{prefix}{' ' if space else ''}",
expected=space,
)
for prefix, space in itertools.product(
["1.", "1)", "#.", "#)", "-", "*", "+"], [True, False]
)
]
)

Expand All @@ -69,7 +95,9 @@ class IsListItemCase(NamedTuple):
line=f"{' ' * preceding}{bullet} {' ' * following} item 1",
expected=True,
)
for bullet, preceding, following in itertools.product(("-", "*", "+"), range(11), range(11))
for bullet, preceding, following in itertools.product(
("-", "*", "+"), range(11), range(11)
)
]
)

Expand Down Expand Up @@ -101,8 +129,8 @@ def test_is_list(line: str, expected: bool, raises):
"- item 1",
"* item 1",
"+ item 1",
"++++++ weird",
"1)))))) still weird",
"+ +++++ weird",
"1) ))))) still weird",
"- [ ] unchecked task",
"- [X] checked task",
],
Expand All @@ -124,10 +152,14 @@ def test_filetype_switching(li: str, filetab, tmp_path):

filetab.textwidget.event_generate("<Tab>")
filetab.update()
assert filetab.textwidget.get("1.0", "end - 1 char") == f" {li}\n", "should indent"
assert (
filetab.textwidget.get("1.0", "end - 1 char") == f" {li}\n"
), "should indent"
filetab.textwidget.event_generate("<Shift-Tab>")
filetab.update()
assert filetab.textwidget.get("1.0", "end - 1 char") == f"{li}\n", "should dedent"
assert (
filetab.textwidget.get("1.0", "end - 1 char") == f"{li}\n"
), "should dedent"


@pytest.mark.parametrize(
Expand Down

0 comments on commit 98d3497

Please sign in to comment.