aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-03-10 22:50:31 +0100
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:58 +0100
commite56e7291095bc9ba2368054601297c2cc65cdf90 (patch)
treee52cb576759a4dbf1c58eaab37fbe8b2b7b52623 /tests
parentc5284b46fddd76a9c45f6dfe41ee541b04451ace (diff)
downloadserd-e56e7291095bc9ba2368054601297c2cc65cdf90.tar.gz
serd-e56e7291095bc9ba2368054601297c2cc65cdf90.tar.bz2
serd-e56e7291095bc9ba2368054601297c2cc65cdf90.zip
Add support for writing terse collections
Diffstat (limited to 'tests')
-rw-r--r--tests/terse/blank_object.ttl2
-rw-r--r--tests/terse/blank_subject.ttl2
-rw-r--r--tests/terse/collection_object.ttl2
-rw-r--r--tests/terse/collection_subject.ttl2
-rw-r--r--tests/terse/manifest.ttl38
-rw-r--r--tests/terse_write_test.c117
6 files changed, 163 insertions, 0 deletions
diff --git a/tests/terse/blank_object.ttl b/tests/terse/blank_object.ttl
new file mode 100644
index 00000000..270e406b
--- /dev/null
+++ b/tests/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/tests/terse/blank_subject.ttl b/tests/terse/blank_subject.ttl
new file mode 100644
index 00000000..5e3303f9
--- /dev/null
+++ b/tests/terse/blank_subject.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+[] eg:p1 1 ; eg:p2 2 .
diff --git a/tests/terse/collection_object.ttl b/tests/terse/collection_object.ttl
new file mode 100644
index 00000000..3310c418
--- /dev/null
+++ b/tests/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/tests/terse/collection_subject.ttl b/tests/terse/collection_subject.ttl
new file mode 100644
index 00000000..43670620
--- /dev/null
+++ b/tests/terse/collection_subject.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+( 1 2 ) eg:p3 3 ; eg:p4 4 .
diff --git a/tests/terse/manifest.ttl b/tests/terse/manifest.ttl
new file mode 100644
index 00000000..f8dca816
--- /dev/null
+++ b/tests/terse/manifest.ttl
@@ -0,0 +1,38 @@
+@prefix mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> .
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix rdft: <http://www.w3.org/ns/rdftest#> .
+
+<>
+ rdf:type mf:Manifest ;
+ rdfs:comment "Serd terse serialisation test cases" ;
+ mf:entries (
+ <#blank_object>
+ <#blank_subject>
+ <#collection_object>
+ <#collection_subject>
+ ) .
+
+<#blank_object>
+ rdf:type rdft:TestTurtleEval ;
+ mf:name "blank_object" ;
+ mf:action <blank_object.ttl> ;
+ mf:result <blank_object.ttl> .
+
+<#blank_subject>
+ rdf:type rdft:TestTurtleEval ;
+ mf:name "blank_subject" ;
+ mf:action <blank_subject.ttl> ;
+ mf:result <blank_subject.ttl> .
+
+<#collection_object>
+ rdf:type rdft:TestTurtleEval ;
+ mf:name "collection_object" ;
+ mf:action <collection_object.ttl> ;
+ mf:result <collection_object.ttl> .
+
+<#collection_subject>
+ rdf:type rdft:TestTurtleEval ;
+ mf:name "collection_subject" ;
+ mf:action <collection_subject.ttl> ;
+ mf:result <collection_subject.ttl> .
diff --git a/tests/terse_write_test.c b/tests/terse_write_test.c
new file mode 100644
index 00000000..4b6a80c4
--- /dev/null
+++ b/tests/terse_write_test.c
@@ -0,0 +1,117 @@
+/*
+ Copyright 2019-2020 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#undef NDEBUG
+
+#include "serd/serd.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;
+ const int valid = !strcmp(output, expected);
+ if (valid) {
+ fprintf(stderr, "%s", output);
+ } else {
+ fprintf(stderr, "error: Invalid output:\n%s", output);
+ fprintf(stderr, "note: Expected output:\n%s", expected);
+ }
+ assert(valid);
+ buffer->len = 0;
+}
+
+static int
+test(void)
+{
+ SerdBuffer buffer = { NULL, 0 };
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(NULL);
+ SerdNodes* nodes = serd_nodes_new();
+ const SerdNode* b1 = serd_nodes_manage(nodes, serd_new_blank("b1"));
+ const SerdNode* l1 = serd_nodes_manage(nodes, serd_new_blank("l1"));
+ const SerdNode* l2 = serd_nodes_manage(nodes, serd_new_blank("l2"));
+ const SerdNode* s1 = serd_nodes_manage(nodes, serd_new_string("s1"));
+ const SerdNode* s2 = serd_nodes_manage(nodes, serd_new_string("s2"));
+
+ const SerdNode* rdf_first =
+ serd_nodes_manage(nodes, serd_new_uri(NS_RDF "first"));
+ const SerdNode* rdf_rest =
+ serd_nodes_manage(nodes, serd_new_uri(NS_RDF "rest"));
+ const SerdNode* rdf_nil =
+ serd_nodes_manage(nodes, serd_new_uri(NS_RDF "nil"));
+ const SerdNode* rdf_value =
+ serd_nodes_manage(nodes, serd_new_uri(NS_RDF "value"));
+
+ serd_env_set_prefix_from_strings(env, "rdf", NS_RDF);
+
+ SerdWriter* writer = serd_writer_new(world,
+ SERD_TURTLE,
+ 0,
+ env,
+ (SerdWriteFunc)serd_buffer_sink,
+ &buffer);
+
+ const SerdSink* sink = serd_writer_get_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, "[]\n\trdf:value ( \"s1\" \"s2\" ) .\n");
+
+ serd_buffer_sink_finish(&buffer);
+ serd_writer_free(writer);
+ serd_nodes_free(nodes);
+ serd_env_free(env);
+ serd_world_free(world);
+ free(buffer.buf);
+
+ return 0;
+}
+
+int
+main(void)
+{
+ return test();
+}