From b5956c4dc6b065d664908104d5fc6752a87e3364 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 31 Mar 2023 17:17:41 -0400 Subject: Add model and serd-sort utility With all the new functionality, the complexity of the serd-pipe command-line interface is starting to push the limits of available flags. So, instead of grafting on further options to control a model, this commit adds a new tool, serd-sort, which acts somewhat like a stripped-down serd-pipe that stores statements in a model in memory. This keeps the complexity (including the user-facing complexity) of any one tool down, since other more focused tools can be used for streaming tasks in a pipeline. In other words, abandon Swissarmyknifeism, take a page from the Unix philosophy, and try to expose the model functionality to the command-line in a dedicated focused tool. The model implementation is tested by using this tool to run a subset of the usual test suites, and a special suite to test statement sorting. --- test/test_sort.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100755 test/test_sort.py (limited to 'test/test_sort.py') diff --git a/test/test_sort.py b/test/test_sort.py new file mode 100755 index 00000000..78147fbc --- /dev/null +++ b/test/test_sort.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 + +# Copyright 2022-2023 David Robillard +# SPDX-License-Identifier: ISC + +"""Run the collation tests for serd-sort.""" + +import os +import shlex +import subprocess +import sys + +import serd_test_util as util + +collations = [ + "GOPS", + "GOSP", + "GPSO", + "GSOP", + "GSPO", + "OPS", + "OSP", + "POS", + "PSO", + "SOP", + "SPO", + "pretty", +] + + +def run_sort_test(command, in_path, good_path): + """Sort a single input in the named order and check the output. + + The expected output is assumed to exist at test_dir/NAME.untyped.nq. + """ + + result_name = os.path.basename(good_path) + options = [] + if result_name not in ["pretty.nq", "untyped.nq"]: + options += ["-c", os.path.splitext(result_name)[0]] + + command = command + options + [in_path] + + proc = subprocess.run( + command, check=True, encoding="utf-8", capture_output=True + ) + + lines = proc.stdout.splitlines(True) + with open(good_path, "r", encoding="utf-8") as good: + return util.lines_equal(list(good), lines, good_path, result_name) + + +def run_tests(test_dir, command): + """Run all the tests in the suite.""" + + n_failures = 0 + in_path = os.path.join(test_dir, "input.trig") + + # Test all the basic collations, and "pretty" with type first + for name in collations: + good_path = os.path.join(test_dir, name + ".nq") + prefixes = [command, command + ["-I", "trig"]] + for prefix in prefixes: + if not run_sort_test(prefix, in_path, good_path): + n_failures += 1 + + # Test "pretty" without type first + if not run_sort_test( + command + ["-O", "longhand"], + in_path, + os.path.join(test_dir, "untyped.nq"), + ): + n_failures += 1 + + return n_failures + + +def main(): + """Run the command line tool.""" + + args = util.wrapper_args(__doc__, True) + wrapper_prefix = shlex.split(args.wrapper) + command_prefix = wrapper_prefix + [args.tool] + + return run_tests(os.path.dirname(args.input), command_prefix) + + +if __name__ == "__main__": + try: + sys.exit(main()) + except subprocess.CalledProcessError as error: + if error.stderr is not None: + sys.stderr.write(error.stderr) + + sys.stderr.write(sys.argv[0]) + sys.stderr.write(": error: ") + sys.stderr.write(str(error)) + sys.stderr.write("\n") + sys.exit(error.returncode) -- cgit v1.2.1