diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-06-06 12:10:38 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2021-06-06 12:42:07 -0400 |
commit | 95c582fd8038a7158ff96baff4186f5fb601afd4 (patch) | |
tree | b870edb23108bdd473df3781d65c74d547bc7a78 /tests | |
parent | 734ee8760df75427753e54b4d8078cfa06484da3 (diff) | |
download | python-coveragepy-git-95c582fd8038a7158ff96baff4186f5fb601afd4.tar.gz |
feat: add support for Python 3.10 match-case statements
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_arcs.py | 51 | ||||
-rw-r--r-- | tests/test_parser.py | 18 |
2 files changed, 69 insertions, 0 deletions
diff --git a/tests/test_arcs.py b/tests/test_arcs.py index 495a10f3..22446f6a 100644 --- a/tests/test_arcs.py +++ b/tests/test_arcs.py @@ -1198,6 +1198,57 @@ class YieldTest(CoverageTest): ) +@pytest.mark.skipif(not env.PYBEHAVIOR.match_case, reason="Match-case is new in 3.10") +class MatchCaseTest(CoverageTest): + """Tests of match-case.""" + def test_match_case_with_default(self): + self.check_coverage("""\ + 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" + case _: + match = "default" + print(match) + """, + arcz=".1 12 23 34 49 35 56 69 58 89 91 1.", + ) + assert self.stdout() == "default\nno go\ngo: n\n" + + def test_match_case_with_wildcard(self): + self.check_coverage("""\ + 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" + case x: + match = f"default: {x}" + print(match) + """, + arcz=".1 12 23 34 49 35 56 69 57 78 89 91 1.", + ) + assert self.stdout() == "default: ['huh']\nno go\ngo: n\n" + + def test_match_case_without_wildcard(self): + self.check_coverage("""\ + match = None + 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) + """, + arcz=".1 12 23 34 45 58 46 78 67 68 82 2.", + ) + assert self.stdout() == "None\nno go\ngo: n\n" + + class OptimizedIfTest(CoverageTest): """Tests of if statements being optimized away.""" diff --git a/tests/test_parser.py b/tests/test_parser.py index 7fd87bba..1b4e8aca 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -394,6 +394,24 @@ class ParserMissingArcDescriptionTest(CoverageTest): """) 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): + 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) + """) + 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(3, 5) == ( + "line 3 didn't jump to line 5, because the pattern on line 3 always matched" + ) + class ParserFileTest(CoverageTest): """Tests for coverage.py's code parsing from files.""" |