From 1fb84760a8230637a806e8e83410fc7fb6d446d2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 4 Sep 2021 15:03:11 -0400 Subject: Add "contextual" output option This is mainly for developer or power-user cases, where one wants to look at some data for investigation or debugging. In such cases, it's common for the set of prefixes to be implicitly known (because they are baked in to the application, for example), so printing them just produces a large amount of redundant noise. That said, it can also be useful programmatically, because it allows several snippets to be written independently and ultimately concatenated (with a header to define the prefixes) without redundancy. --- test/meson.build | 8 ++++++++ test/test_contextual.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100755 test/test_contextual.py (limited to 'test') diff --git a/test/meson.build b/test/meson.build index 37bae479..ae66ba37 100644 --- a/test/meson.build +++ b/test/meson.build @@ -236,6 +236,14 @@ if is_variable('serd_pipe') env: test_env, should_fail: true, suite: ['tools', 'pipe', 'output']) + + # Write options + + test('contextual', files('test_contextual.py'), + args: pipe_test_script_args + files('../serd.ttl'), + env: test_env, + suite: ['tools', 'pipe', 'output']) + endif # Test specifics for serd-sort diff --git a/test/test_contextual.py b/test/test_contextual.py new file mode 100755 index 00000000..0902fa19 --- /dev/null +++ b/test/test_contextual.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +"""Test writing with -O contextual (SERD_WRITE_CONTEXTUAL).""" + +import argparse +import sys +import shlex +import subprocess +import tempfile + +parser = argparse.ArgumentParser(description=__doc__) + +parser.add_argument("--tool", default="tools/serd-pipe", help="executable") +parser.add_argument("--wrapper", default="", help="executable wrapper") +parser.add_argument("input", default="", help="input file") + +args = parser.parse_args(sys.argv[1:]) +command = shlex.split(args.wrapper) + [ + args.tool, + "-O", + "turtle", + "-O", + "contextual", + args.input, +] + +DOCUMENT = "<{0}s> <{0}p> <{0}o> .".format("http://example.org/") + +with tempfile.TemporaryFile() as out: + proc = subprocess.run( + command, + check=False, + encoding="utf-8", + input=DOCUMENT, + stdout=out, + stderr=subprocess.PIPE, + ) + + assert proc.returncode == 0 + assert args.wrapper or len(proc.stderr) == 0 + + out.seek(0) + lines = out.readlines() + + for line in lines: + assert "@prefix" not in line.decode("utf-8") -- cgit v1.2.1