diff options
Diffstat (limited to 'test/serd_test_util')
-rw-r--r-- | test/serd_test_util/__init__.py | 71 |
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 |