aboutsummaryrefslogtreecommitdiffstats
path: root/test/serd_test_util/__init__.py
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-10-21 15:38:10 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:07 -0500
commitb404312686874e539b617d1f27ccbaa5a82936af (patch)
treec2fdb2cc046e6da53071629cd1750dcc327e6cd9 /test/serd_test_util/__init__.py
parentd4aec28ba8ad24d5aef3ee12beeb1b805148eab1 (diff)
downloadserd-b404312686874e539b617d1f27ccbaa5a82936af.tar.gz
serd-b404312686874e539b617d1f27ccbaa5a82936af.tar.bz2
serd-b404312686874e539b617d1f27ccbaa5a82936af.zip
Replace serdi with more fine-grained tools
Especially with the new functionality, the complexity of the command-line interface alone was really becoming unmanageable. The serdi implementation also had the highest cyclomatic complexity of the entire codebase by a huge margin. So, take a page from the Unix philosophy and split serdi into several more finely-honed tools that can be freely composed. Though there is still unfortunately quite a bit of option overlap between them due to the common details of reading RDF, I think the resulting tools are a lot easier to understand, both from a user and a developer perspective.
Diffstat (limited to 'test/serd_test_util/__init__.py')
-rw-r--r--test/serd_test_util/__init__.py71
1 files changed, 70 insertions, 1 deletions
diff --git a/test/serd_test_util/__init__.py b/test/serd_test_util/__init__.py
index f0b1c19a..45cc6e64 100644
--- a/test/serd_test_util/__init__.py
+++ b/test/serd_test_util/__init__.py
@@ -3,8 +3,48 @@
"""Utilities for data-driven tests."""
import datetime
+import difflib
+import os
import re
import subprocess
+import sys
+
+
+def error(message):
+ """Log an error message to stderr"""
+
+ sys.stderr.write("error: ")
+ sys.stderr.write(message)
+
+
+def test_input_syntax(test_class):
+ """Return the output syntax use for a given test class."""
+
+ if "NTriples" in test_class:
+ return "NTriples"
+
+ if "Turtle" in test_class:
+ return "Turtle"
+
+ if "NQuads" in test_class:
+ return "NQuads"
+
+ if "Trig" in test_class:
+ return "Trig"
+
+ raise Exception("Unknown test class <{}>".format(test_class))
+
+
+def test_output_syntax(test_class):
+ """Return the output syntax use for a given test class."""
+
+ if "NTriples" in test_class or "Turtle" in test_class:
+ return "NTriples"
+
+ if "NQuads" in test_class or "Trig" in test_class:
+ return "NQuads"
+
+ raise Exception("Unknown test class <{}>".format(test_class))
def earl_assertion(test, passed, asserter):
@@ -33,7 +73,7 @@ def earl_assertion(test, passed, asserter):
def load_rdf(command_prefix, filename):
- """Load an RDF file as dictionaries via serdi (only supports URIs)."""
+ """Load an RDF file as dictionaries via serd-pipe (only supports URIs)."""
rdf_type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
model = {}
@@ -61,3 +101,32 @@ def load_rdf(command_prefix, filename):
instances[o].update([s])
return model, instances
+
+
+def file_equals(patha, pathb):
+ """Return true if the file at patha is the same as the file at pathb."""
+
+ for path in (patha, pathb):
+ if not os.access(path, os.F_OK):
+ error("missing file {}\n".format(path))
+ return False
+
+ with open(patha, "r", encoding="utf-8") as fa:
+ with open(pathb, "r", encoding="utf-8") as fb:
+ return show_diff(fa.readlines(), fb.readlines(), patha, pathb)
+
+
+def show_diff(from_lines, to_lines, from_filename, to_filename):
+ """Print a diff between files to stderr."""
+
+ same = True
+ for line in difflib.unified_diff(
+ from_lines,
+ to_lines,
+ fromfile=os.path.abspath(from_filename),
+ tofile=os.path.abspath(to_filename),
+ ):
+ sys.stderr.write(line)
+ same = False
+
+ return same