aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-04-04 06:35:51 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:07 -0500
commit7219a81ad6cff840eb604983b3752d4f2097532a (patch)
tree5c071ae9575f05a75f17a2d8280e9e9aed6440ac /test
parent1c7a8efa90003d21e19b47bbad185390cea5963c (diff)
downloadserd-7219a81ad6cff840eb604983b3752d4f2097532a.tar.gz
serd-7219a81ad6cff840eb604983b3752d4f2097532a.tar.bz2
serd-7219a81ad6cff840eb604983b3752d4f2097532a.zip
Add support for writing terse output with minimal newlines
Diffstat (limited to 'test')
-rw-r--r--test/extra/abbreviate/collapse-graphs.trig1
-rw-r--r--test/extra/terse/blank_object.ttl2
-rw-r--r--test/extra/terse/blank_subject.ttl2
-rw-r--r--test/extra/terse/collection_object.ttl2
-rw-r--r--test/extra/terse/collection_subject.ttl2
-rw-r--r--test/extra/terse/manifest.ttl39
-rw-r--r--test/extra/terse/nil_object.ttl2
-rw-r--r--test/extra/terse/nil_subject.ttl2
-rw-r--r--test/meson.build8
-rw-r--r--test/serd_test_util/__init__.py2
-rw-r--r--test/test_terse_write.c112
11 files changed, 172 insertions, 2 deletions
diff --git a/test/extra/abbreviate/collapse-graphs.trig b/test/extra/abbreviate/collapse-graphs.trig
index 5bdf55ce..4f8e4344 100644
--- a/test/extra/abbreviate/collapse-graphs.trig
+++ b/test/extra/abbreviate/collapse-graphs.trig
@@ -1,4 +1,3 @@
-
<http://example.org/g1> {
<http://example.org/s1>
<http://example.org/p1> <http://example.org/o1> ;
diff --git a/test/extra/terse/blank_object.ttl b/test/extra/terse/blank_object.ttl
new file mode 100644
index 00000000..270e406b
--- /dev/null
+++ b/test/extra/terse/blank_object.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+eg:s eg:p1 [ eg:p11 1 ; eg:p12 2 ] ; eg:p2 [ eg:p23 3 ; eg:p24 4 ] .
diff --git a/test/extra/terse/blank_subject.ttl b/test/extra/terse/blank_subject.ttl
new file mode 100644
index 00000000..5e3303f9
--- /dev/null
+++ b/test/extra/terse/blank_subject.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+[] eg:p1 1 ; eg:p2 2 .
diff --git a/test/extra/terse/collection_object.ttl b/test/extra/terse/collection_object.ttl
new file mode 100644
index 00000000..3310c418
--- /dev/null
+++ b/test/extra/terse/collection_object.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+eg:s eg:p1 ( 1 2 ) ; eg:p2 ( 3 4 ) .
diff --git a/test/extra/terse/collection_subject.ttl b/test/extra/terse/collection_subject.ttl
new file mode 100644
index 00000000..43670620
--- /dev/null
+++ b/test/extra/terse/collection_subject.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+( 1 2 ) eg:p3 3 ; eg:p4 4 .
diff --git a/test/extra/terse/manifest.ttl b/test/extra/terse/manifest.ttl
new file mode 100644
index 00000000..6773b97d
--- /dev/null
+++ b/test/extra/terse/manifest.ttl
@@ -0,0 +1,39 @@
+@prefix mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix rdft: <http://www.w3.org/ns/rdftest#> .
+
+<>
+ a mf:Manifest ;
+ rdfs:comment "Serd terse writing test suite" ;
+ mf:entries (
+ <#blank_object>
+ <#blank_subject>
+ <#collection_object>
+ <#collection_subject>
+ <#nil_object>
+ <#nil_subject>
+ ) .
+
+<#blank_object>
+ a rdft:TestTurtleEval ;
+ mf:action <blank_object.ttl> ;
+ mf:name "blank_object" ;
+ mf:result <blank_object.ttl> .
+
+<#blank_subject>
+ a rdft:TestTurtleEval ;
+ mf:action <blank_subject.ttl> ;
+ mf:name "blank_subject" ;
+ mf:result <blank_subject.ttl> .
+
+<#collection_object>
+ a rdft:TestTurtleEval ;
+ mf:action <collection_object.ttl> ;
+ mf:name "collection_object" ;
+ mf:result <collection_object.ttl> .
+
+<#collection_subject>
+ a rdft:TestTurtleEval ;
+ mf:action <collection_subject.ttl> ;
+ mf:name "collection_subject" ;
+ mf:result <collection_subject.ttl> .
diff --git a/test/extra/terse/nil_object.ttl b/test/extra/terse/nil_object.ttl
new file mode 100644
index 00000000..4a0f93bd
--- /dev/null
+++ b/test/extra/terse/nil_object.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+eg:s eg:p1 () ; eg:p2 [] , () .
diff --git a/test/extra/terse/nil_subject.ttl b/test/extra/terse/nil_subject.ttl
new file mode 100644
index 00000000..a08f7d82
--- /dev/null
+++ b/test/extra/terse/nil_subject.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+() eg:p1 1 ; eg:p2 2 .
diff --git a/test/meson.build b/test/meson.build
index 937b9038..2ed73e63 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -34,6 +34,7 @@ ttl_metadata_file_paths = [
'extra/pretty/manifest.ttl',
'extra/qualify/manifest.ttl',
'extra/root/manifest.ttl',
+ 'extra/terse/manifest.ttl',
]
plot_scripts = files(plot_script_paths)
@@ -129,6 +130,7 @@ unit_tests = [
'statement',
'string',
'syntax',
+ 'terse_write',
'uri',
'world',
'writer',
@@ -407,6 +409,12 @@ test_suites = {
'--',
['-r', 'http://example.org/top/root/'],
],
+ 'terse': [
+ files('extra/terse/manifest.ttl'),
+ ns_serdtest + 'terse/',
+ '--',
+ '-t',
+ ],
}
# Run every test suite with serdi
diff --git a/test/serd_test_util/__init__.py b/test/serd_test_util/__init__.py
index 8027462b..04876f98 100644
--- a/test/serd_test_util/__init__.py
+++ b/test/serd_test_util/__init__.py
@@ -60,7 +60,7 @@ def print_result_summary(results):
failed, total = (results.n_failures, results.n_tests)
if failed == 0:
- sys.stdout.write("All {} tests passed\n".format(total))
+ print("All {} tests passed".format(total))
else:
error("{}/{} tests failed".format(failed, total))
diff --git a/test/test_terse_write.c b/test/test_terse_write.c
new file mode 100644
index 00000000..277f025e
--- /dev/null
+++ b/test/test_terse_write.c
@@ -0,0 +1,112 @@
+// Copyright 2019-2020 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#undef NDEBUG
+
+#include "serd/buffer.h"
+#include "serd/env.h"
+#include "serd/node.h"
+#include "serd/sink.h"
+#include "serd/statement.h"
+#include "serd/stream.h"
+#include "serd/string_view.h"
+#include "serd/syntax.h"
+#include "serd/world.h"
+#include "serd/writer.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+
+static void
+check_output(SerdWriter* writer, SerdBuffer* buffer, const char* expected)
+{
+ serd_writer_finish(writer);
+ serd_buffer_sink_finish(buffer);
+
+ const char* output = (const char*)buffer->buf;
+
+ assert(!strcmp(output, expected));
+
+ buffer->len = 0;
+}
+
+static int
+test(void)
+{
+ SerdBuffer buffer = {NULL, 0};
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(serd_empty_string());
+
+ SerdNode* b1 = serd_new_blank(serd_string("b1"));
+ SerdNode* l1 = serd_new_blank(serd_string("l1"));
+ SerdNode* l2 = serd_new_blank(serd_string("l2"));
+ SerdNode* s1 = serd_new_string(serd_string("s1"));
+ SerdNode* s2 = serd_new_string(serd_string("s2"));
+
+ SerdNode* rdf_first = serd_new_uri(serd_string(NS_RDF "first"));
+ SerdNode* rdf_value = serd_new_uri(serd_string(NS_RDF "value"));
+ SerdNode* rdf_rest = serd_new_uri(serd_string(NS_RDF "rest"));
+ SerdNode* rdf_nil = serd_new_uri(serd_string(NS_RDF "nil"));
+
+ serd_env_set_prefix(env, serd_string("rdf"), serd_string(NS_RDF));
+
+ SerdWriter* writer = serd_writer_new(
+ world, SERD_TURTLE, 0, env, (SerdWriteFunc)serd_buffer_sink, &buffer);
+
+ const SerdSink* sink = serd_writer_sink(writer);
+
+ // Simple lone list
+ serd_sink_write(sink, SERD_TERSE_S | SERD_LIST_S, l1, rdf_first, s1, NULL);
+ serd_sink_write(sink, 0, l1, rdf_rest, l2, NULL);
+ serd_sink_write(sink, 0, l2, rdf_first, s2, NULL);
+ serd_sink_write(sink, 0, l2, rdf_rest, rdf_nil, NULL);
+ check_output(writer, &buffer, "( \"s1\" \"s2\" ) .\n");
+
+ // Nested terse lists
+ serd_sink_write(sink,
+ SERD_TERSE_S | SERD_LIST_S | SERD_TERSE_O | SERD_LIST_O,
+ l1,
+ rdf_first,
+ l2,
+ NULL);
+ serd_sink_write(sink, 0, l2, rdf_first, s1, NULL);
+ serd_sink_write(sink, 0, l1, rdf_rest, rdf_nil, NULL);
+ serd_sink_write(sink, 0, l2, rdf_rest, rdf_nil, NULL);
+ check_output(writer, &buffer, "( ( \"s1\" ) ) .\n");
+
+ // List as object
+ serd_sink_write(
+ sink, SERD_EMPTY_S | SERD_LIST_O | SERD_TERSE_O, b1, rdf_value, l1, NULL);
+ serd_sink_write(sink, 0, l1, rdf_first, s1, NULL);
+ serd_sink_write(sink, 0, l1, rdf_rest, l2, NULL);
+ serd_sink_write(sink, 0, l2, rdf_first, s2, NULL);
+ serd_sink_write(sink, 0, l2, rdf_rest, rdf_nil, NULL);
+ check_output(writer, &buffer, "[] rdf:value ( \"s1\" \"s2\" ) .\n");
+
+ serd_buffer_sink_finish(&buffer);
+ serd_writer_free(writer);
+ serd_node_free(rdf_nil);
+ serd_node_free(rdf_rest);
+ serd_node_free(rdf_value);
+ serd_node_free(rdf_first);
+ serd_node_free(s2);
+ serd_node_free(s1);
+ serd_node_free(l2);
+ serd_node_free(l1);
+ serd_node_free(b1);
+ serd_env_free(env);
+ serd_world_free(world);
+ free(buffer.buf);
+
+ return 0;
+}
+
+int
+main(void)
+{
+ return test();
+}