aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-03-31 20:32:26 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commit0db18e483f11ac2f9518d96e137d217040ed1339 (patch)
tree8c1c77bdae31d55f311c89356483e873be81bb90 /test
parenta1b677851274b7e5295962658e723cab007f9b85 (diff)
downloadserd-0db18e483f11ac2f9518d96e137d217040ed1339.tar.gz
serd-0db18e483f11ac2f9518d96e137d217040ed1339.tar.bz2
serd-0db18e483f11ac2f9518d96e137d217040ed1339.zip
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.
Diffstat (limited to 'test')
-rw-r--r--test/meson.build11
-rwxr-xr-xtest/test_contextual.py51
2 files changed, 62 insertions, 0 deletions
diff --git a/test/meson.build b/test/meson.build
index 1f736afc..6bb0e033 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -19,6 +19,7 @@ simple_script_paths = [
'run_filter_suite.py',
'run_suite.py',
'test_base.py',
+ 'test_contextual.py',
'test_empty.py',
'test_multifile.py',
'test_patterns.py',
@@ -446,6 +447,16 @@ if is_variable('serd_pipe')
suite: 'io',
)
endif
+
+ # Write options
+
+ test(
+ 'contextual',
+ files('test_contextual.py'),
+ args: pipe_script_args + files('../serd.ttl'),
+ env: test_env,
+ suite: ['tools', 'pipe', 'output'],
+ )
endif
# Test specifics to serd-filter
diff --git a/test/test_contextual.py b/test/test_contextual.py
new file mode 100755
index 00000000..6a584c5b
--- /dev/null
+++ b/test/test_contextual.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+
+# Copyright 2022 David Robillard <d@drobilla.net>
+# SPDX-License-Identifier: ISC
+
+"""Test writing with -O contextual (SERD_WRITE_CONTEXTUAL)."""
+
+# pylint: disable=consider-using-f-string
+
+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,
+]
+
+DOC = "<{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=DOC,
+ 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")