Skip to content

Commit 66aa180

Browse files
committed
Recursively comparing subtrees.
1 parent 9d81d7e commit 66aa180

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/ccg2xml/test_xml2ccg.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ def tree_sort_key(elem):
131131
return elem.tag + str(sorted('{}={}'.format(*a) for a in elem.attrib.items()))
132132

133133

134+
def recursive_tree_comparison(left, right):
135+
if left.tag != right.tag:
136+
return False
137+
elif left.attrib != right.attrib:
138+
return False
139+
elif left.find('*') is not None and right.find('*') is None:
140+
return False
141+
elif left.find('*') is None:
142+
return True
143+
else:
144+
return all(recursive_tree_comparison(l, r) for r, l in zip(iter(left), iter(right)))
145+
146+
134147
def compare_grammar_tree(left, right):
135148
"""Compares two XML tree structures.
136149
@@ -169,7 +182,7 @@ def compare_grammar_tree(left, right):
169182
except StopIteration:
170183
return False, (l, first_r)
171184

172-
if l.tag == r.tag and l.attrib == r.attrib:
185+
if recursive_tree_comparison(l, r):
173186
break
174187

175188
for comp in [compare_type_elements, compare_file_tags]:

0 commit comments

Comments
 (0)