-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmatcher.py
58 lines (42 loc) · 1.89 KB
/
matcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import unittest
import re
class CodeMatcher(unittest.TestCase):
def matchSnipped(self, snipped, template, removeLinebreaks: bool = False):
res, mapping, templateReplaced, reason = CodeMatcher.doMatchSnipped(snipped.strip(), template.strip(),removeLinebreaks)
if not res:
mapstr = "with mapping:\n"
for templ,tVar in mapping.items():
mapstr += f"\t{templ} -> {tVar}\n"
if not templateReplaced:
templateReplaced = "N/A"
self.fail(f"Mismatch\nFound: {snipped}\nExpected: {template}\nReplaced: {templateReplaced}\nReason:\t{reason}\n{mapstr}")
@staticmethod
def doMatchSnipped(snipped, template, removeLinebreaks):
pattern = re.compile(r"\$t[0-9]+")
pattern2 = re.compile("t[0-9]+")
placeholders = pattern.findall(template)
occurences = pattern2.findall(snipped)
mapping = {}
for p,o in zip(placeholders, occurences):
if p not in mapping:
mapping[p] = o
elif p in mapping and mapping[p] != o:
return False, mapping, "", f"Mapping error: {p} -> {mapping[p]} exists, but {p} -> {o} found"
# if we get here, the occurences match the templates
if len(placeholders) != len(occurences):
return False, mapping, "", f"number of placeholders {len(placeholders)} does not match occurences {len(occurences)}"
for (k,v) in mapping.items():
template = template.replace(k,v)
templateClean = template.replace("\n","").replace(" ","").lower()
snippedClean = snipped.replace("\n","").replace(" ","").lower()
matches = snippedClean == templateClean
if not matches:
print()
print(snippedClean)
for i in range(len(snippedClean)):
if snippedClean[i] != templateClean[i]:
print("^")
break
print(" ",end="")
print(templateClean)
return matches, mapping, template, "Snipped does not match template" if not matches else ""