summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_patma.py
diff options
context:
space:
mode:
authorCharles Burkland <charles.aburkland@gmail.com>2021-07-25 23:42:07 (GMT)
committerGitHub <noreply@github.com>2021-07-25 23:42:07 (GMT)
commit4214f470f0cb9b6fef9a90758756fbc00ba95b5a (patch)
tree69233bb8fafe19c2e778bfe3c3353e5e8f0b7a13 /Lib/test/test_patma.py
parent3e235e0447e373d81f195f4292959c7be9c013dc (diff)
downloadcpython-4214f470f0cb9b6fef9a90758756fbc00ba95b5a.zip
cpython-4214f470f0cb9b6fef9a90758756fbc00ba95b5a.tar.gz
cpython-4214f470f0cb9b6fef9a90758756fbc00ba95b5a.tar.bz2
bpo-44600: Fix line numbers for pattern matching cleanup code (GH-27346)
Diffstat (limited to 'Lib/test/test_patma.py')
-rw-r--r--Lib/test/test_patma.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py
index 69a648a..96c1726 100644
--- a/Lib/test/test_patma.py
+++ b/Lib/test/test_patma.py
@@ -3,6 +3,7 @@ import collections
import dataclasses
import enum
import inspect
+import sys
import unittest
@@ -3056,6 +3057,81 @@ class TestValueErrors(unittest.TestCase):
self.assertIs(z, None)
+class TestTracing(unittest.TestCase):
+
+ def _test_trace(self, func, expected_linenos, *f_args):
+ actual_linenos = set()
+ def trace(frame, event, arg):
+ if frame.f_code.co_name == func.__name__:
+ relative_lineno = frame.f_lineno - func.__code__.co_firstlineno
+ actual_linenos.add(relative_lineno)
+ return trace
+
+ sys.settrace(trace)
+ func(*f_args)
+ sys.settrace(None)
+ self.assertSetEqual(actual_linenos, expected_linenos)
+
+ def test_default_case_traces_correctly_a(self):
+ def default_no_assign(command): # 0
+ match command.split(): # 1
+ case ["go", direction] if direction in "nesw": # 2
+ return f"go {direction}" # 3
+ case ["go", _]: # 4
+ return "no go" # 5
+ case _: # 6
+ return "default" # 7
+
+ self._test_trace(default_no_assign, {0, 1, 2, 3}, "go n")
+ self._test_trace(default_no_assign, {0, 1, 2, 4, 5}, "go x")
+ self._test_trace(default_no_assign, {0, 1, 2, 4, 6, 7}, "spam")
+
+ def test_default_case_traces_correctly_b(self):
+ def default_wildcard_assign(command): # 0
+ match command.split(): # 1
+ case ["go", direction] if direction in "nesw": # 2
+ return f"go {direction}" # 3
+ case ["go", _]: # 4
+ return "no go" # 5
+ case x: # 6
+ return x # 7
+
+ self._test_trace(default_wildcard_assign, {0, 1, 2, 3}, "go n")
+ self._test_trace(default_wildcard_assign, {0, 1, 2, 4, 5}, "go x")
+ self._test_trace(default_wildcard_assign, {0, 1, 2, 4, 6, 7}, "spam")
+
+ def test_default_case_traces_correctly_c(self):
+ def no_default(command): # 0
+ match command.split(): # 1
+ case ["go", direction] if direction in "nesw": # 2
+ return f"go {direction}" # 3
+ case ["go", _]: # 4
+ return "no go" # 5
+
+ self._test_trace(no_default, {0, 1, 2, 3}, "go n")
+ self._test_trace(no_default, {0, 1, 2, 4, 5}, "go x")
+ self._test_trace(no_default, {0, 1, 2, 4}, "spam")
+
+ def test_default_case_traces_correctly_d(self):
+ def only_default_no_assign(command): # 0
+ match command.split(): # 1
+ case _: # 2
+ return "default" # 3
+
+ self._test_trace(only_default_no_assign, {0, 1, 2, 3}, "go n")
+ self._test_trace(only_default_no_assign, {0, 1, 2, 3} , "go x")
+ self._test_trace(only_default_no_assign, {0, 1, 2, 3}, "spam")
+
+ def test_default_case_traces_correctly_e(self):
+ def only_default_wildcard_assign(command): # 0
+ match command.split(): # 1
+ case x: # 2
+ return x # 3
+
+ self._test_trace(only_default_wildcard_assign, {0, 1, 2, 3}, "go n")
+ self._test_trace(only_default_wildcard_assign, {0, 1, 2, 3} , "go x")
+ self._test_trace(only_default_wildcard_assign, {0, 1, 2, 3}, "spam")
+
if __name__ == "__main__":
"""
# From inside environment using this Python, with pyperf installed: