Skip to content

Commit

Permalink
fix: correct the missing branch message for the last case of a match/…
Browse files Browse the repository at this point in the history
…case
  • Loading branch information
nedbat committed Mar 7, 2024
1 parent f466ced commit 5a031b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ upgrading your version of coverage.py.
Unreleased
----------

- Fix: the last case of a match/case statement had an incorrect message if the
branch was missed. It said the pattern never matched, when actually the
branch is missed if the last case always matched.

- Fix: clicking a line number in the HTML report now positions more accurately.


Expand Down
4 changes: 3 additions & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,9 @@ def _handle__Match(self, node: ast.Match) -> set[ArcStart]:
exits |= self.add_body_arcs(case.body, from_start=from_start)
last_start = case_start
if not had_wildcard:
exits.add(from_start)
exits.add(
ArcStart(case_start, cause="the pattern on line {lineno} always matched"),
)
return exits

def _handle__NodeList(self, node: NodeList) -> set[ArcStart]:
Expand Down
28 changes: 15 additions & 13 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,21 +902,23 @@ def test_missing_arc_descriptions_bug460(self) -> None:
assert parser.missing_arc_description(2, -3) == "line 3 didn't finish the lambda on line 3"

@pytest.mark.skipif(not env.PYBEHAVIOR.match_case, reason="Match-case is new in 3.10")
def test_match_case_with_default(self) -> None:
parser = self.parse_text("""\
for command in ["huh", "go home", "go n"]:
match command.split():
case ["go", direction] if direction in "nesw":
match = f"go: {direction}"
case ["go", _]:
match = "no go"
print(match)
def test_match_case(self) -> None:
parser = self.parse_text("""\
match command.split():
case ["go", direction] if direction in "nesw": # 2
match = f"go: {direction}"
case ["go", _]: # 4
match = "no go"
print(match) # 6
""")
assert parser.missing_arc_description(3, 4) == (
"line 3 didn't jump to line 4, because the pattern on line 3 never matched"
assert parser.missing_arc_description(2, 3) == (
"line 2 didn't jump to line 3, because the pattern on line 2 never matched"
)
assert parser.missing_arc_description(3, 5) == (
"line 3 didn't jump to line 5, because the pattern on line 3 always matched"
assert parser.missing_arc_description(2, 4) == (
"line 2 didn't jump to line 4, because the pattern on line 2 always matched"
)
assert parser.missing_arc_description(4, 6) == (
"line 4 didn't jump to line 6, because the pattern on line 4 always matched"
)


Expand Down

0 comments on commit 5a031b0

Please sign in to comment.