summaryrefslogtreecommitdiffstats
path: root/tests/support/structural_diff.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 11:33:32 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 11:33:32 +0000
commit1f403ad2197fc7442409f434ee574f3e6b46fb73 (patch)
tree0299c6dd11d5edfa918a29b6456bc1875f1d288c /tests/support/structural_diff.py
parentInitial commit. (diff)
downloadpygments-1f403ad2197fc7442409f434ee574f3e6b46fb73.tar.xz
pygments-1f403ad2197fc7442409f434ee574f3e6b46fb73.zip
Adding upstream version 2.14.0+dfsg.upstream/2.14.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/support/structural_diff.py')
-rw-r--r--tests/support/structural_diff.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/support/structural_diff.py b/tests/support/structural_diff.py
new file mode 100644
index 0000000..cea27d1
--- /dev/null
+++ b/tests/support/structural_diff.py
@@ -0,0 +1,37 @@
+import html.parser
+
+
+class Parser(html.parser.HTMLParser):
+ def __init__(self):
+ super().__init__()
+ self._stream = []
+
+ def handle_starttag(self, tag, attrs):
+ attrs = sorted(attrs, key=lambda x: x[0])
+ attrs = '|'.join([k[0] + ':' + k[1] for k in attrs])
+ self._stream.append(('<', tag, attrs))
+
+ def handle_endtag(self, tag):
+ self._stream.append(('>', tag, ''))
+
+ def handle_data(self, data):
+ self._stream.append(('_', data, ''))
+
+ @property
+ def stream(self):
+ return self._stream
+
+
+def _serialize(t):
+ parser = Parser()
+ parser.feed(t)
+ return parser.stream
+
+
+def structural_diff(a, b):
+ """Check if there is a structural difference between two HTML files."""
+ a_s = _serialize(a)
+ b_s = _serialize(b)
+
+ for e, f in zip(a_s, b_s):
+ assert e == f, f'Expected: {e}, found: {f}'