aboutsummaryrefslogtreecommitdiffstats
path: root/test/run_pretty_suite.py
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-08-12 22:28:59 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:07 -0500
commit1409f161793a289819df1f31eccb579b71f45475 (patch)
tree4076c291a397f0a0a2ea560305ee87c47810df33 /test/run_pretty_suite.py
parent6ffe331cfc8af0735b263ab7af6f84b1b4418102 (diff)
downloadserd-1409f161793a289819df1f31eccb579b71f45475.tar.gz
serd-1409f161793a289819df1f31eccb579b71f45475.tar.bz2
serd-1409f161793a289819df1f31eccb579b71f45475.zip
Fix pretty-printing nested empty lists and add test suite
The earlier "test" was just hitting the code without actually checking the output. This new suite is a set of pretty-printed documents which serd must reproduce from a model exactly to pass. This should make it easy to add cases in the future, since each case is just a document, as it should look.
Diffstat (limited to 'test/run_pretty_suite.py')
-rwxr-xr-xtest/run_pretty_suite.py153
1 files changed, 153 insertions, 0 deletions
diff --git a/test/run_pretty_suite.py b/test/run_pretty_suite.py
new file mode 100755
index 00000000..a17dd0f8
--- /dev/null
+++ b/test/run_pretty_suite.py
@@ -0,0 +1,153 @@
+#!/usr/bin/env python3
+
+"""Run the RDF-based test suite for serd-filter."""
+
+import serd_test_util
+
+import argparse
+import datetime
+import difflib
+import itertools
+import os
+import re
+import shlex
+import subprocess
+import sys
+import tempfile
+import urllib.parse
+
+
+def log_error(message):
+ """Log an error message to stderr"""
+
+ sys.stderr.write("error: ")
+ sys.stderr.write(message)
+
+
+def _uri_path(test_dir, uri):
+ path = urllib.parse.urlparse(uri).path
+ drive = os.path.splitdrive(path[1:])[0]
+ path = path if not drive else path[1:]
+ return os.path.join(test_dir, os.path.basename(path))
+
+
+def test_suite(
+ manifest_path,
+ base_uri,
+ command_prefix,
+ out_dir,
+):
+ """Run all tests in the manifest."""
+
+ mf = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#"
+ suite_dir = os.path.dirname(manifest_path)
+
+ model, instances = serd_test_util.load_rdf(
+ command_prefix + ["-B", base_uri], manifest_path
+ )
+
+ os.makedirs(out_dir, exist_ok=True)
+
+ class Results:
+ def __init__(self):
+ self.n_tests = 0
+ self.n_failures = 0
+
+ def run_test(entry, results):
+ """Run a single test entry from the manifest."""
+
+ input_uri = model[entry][mf + "action"][0]
+ input_path = _uri_path(suite_dir, input_uri)
+
+ output_path = os.path.join(out_dir, os.path.basename(input_path))
+
+ command = command_prefix + [
+ "-B",
+ base_uri,
+ "-O",
+ "turtle",
+ "-o",
+ output_path,
+ input_path,
+ ]
+
+ # Read the flat input and pretty-print output
+ results.n_tests += 1
+ try:
+ subprocess.run(command, check=True)
+
+ # Check that the output is exactly the same as the expected result
+ if not serd_test_util.file_equals(input_path, output_path):
+ results.n_failures += 1
+ log_error(
+ "Output {} differs from {}\n".format(
+ output_path, input_path
+ )
+ )
+
+ except Exception as e:
+ log_error(str(e))
+ results.n_failures += 1
+
+ # Run all test types in the test suite
+ results = Results()
+ for klass, instances in instances.items():
+ if klass == "http://drobilla.net/ns/serd#TestTurtlePrint":
+ for entry in instances:
+ run_test(entry, results)
+
+ # Print result summary
+ if results.n_failures > 0:
+ log_error(
+ "{}/{} tests failed\n".format(results.n_failures, results.n_tests)
+ )
+ else:
+ sys.stdout.write("All {} tests passed\n".format(results.n_tests))
+
+ return results.n_failures
+
+
+def main():
+ """Run the command line tool."""
+
+ parser = argparse.ArgumentParser(
+ usage="%(prog)s [OPTION]... MANIFEST -- [TOOL_OPTION]...",
+ description=__doc__,
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ )
+
+ parser.add_argument("--tool", default="tools/serd-sort", help="executable")
+ parser.add_argument("--wrapper", default="", help="executable wrapper")
+
+ parser.add_argument(
+ "-o", "--out-dir", default="test/pretty", help="output directory"
+ )
+
+ parser.add_argument(
+ "--base-uri",
+ default="http://drobilla.net/sw/serd/test/",
+ help="base URI",
+ )
+ parser.add_argument("manifest", help="test suite manifest.ttl file")
+
+ args = parser.parse_args(sys.argv[1:])
+ wrapper_prefix = shlex.split(args.wrapper)
+ command_prefix = wrapper_prefix + [args.tool]
+
+ return test_suite(
+ args.manifest,
+ args.base_uri,
+ command_prefix,
+ args.out_dir,
+ )
+
+
+if __name__ == "__main__":
+ try:
+ sys.exit(main())
+ except subprocess.CalledProcessError as e:
+ if e.stderr is not None:
+ sys.stderr.write(e.stderr.decode("utf-8"))
+
+ sys.stderr.write("error: %s\n" % e)
+ sys.exit(e.returncode)