diff options
author | David Robillard <d@drobilla.net> | 2020-06-21 18:19:48 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-14 19:37:51 -0500 |
commit | 4ac6d94d9a8826e09b81c6505408bed44d11e567 (patch) | |
tree | c14d3545d56c1e10be516db1838c1811bc83bcc9 /test | |
parent | 4436f80225c441a878c4baf856d21dee0394c8dc (diff) | |
download | serd-4ac6d94d9a8826e09b81c6505408bed44d11e567.tar.gz serd-4ac6d94d9a8826e09b81c6505408bed44d11e567.tar.bz2 serd-4ac6d94d9a8826e09b81c6505408bed44d11e567.zip |
Add serd_node_from_syntax() and serd_node_to_syntax()
Diffstat (limited to 'test')
-rw-r--r-- | test/meson.build | 1 | ||||
-rw-r--r-- | test/test_node.c | 4 | ||||
-rw-r--r-- | test/test_node_syntax.c | 154 |
3 files changed, 157 insertions, 2 deletions
diff --git a/test/meson.build b/test/meson.build index 4d9d2b43..77a9ba57 100644 --- a/test/meson.build +++ b/test/meson.build @@ -12,6 +12,7 @@ unit_tests = [ 'free_null', 'log', 'node', + 'node_syntax', 'nodes', 'overflow', 'read_chunk', diff --git a/test/test_node.c b/test/test_node.c index 8de81e08..466655ef 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -429,7 +429,7 @@ test_node_equals(void) } static void -test_node_from_string(void) +test_node_from_syntax(void) { SerdNode* const hello = serd_new_string(SERD_STRING("hello\"")); assert(serd_node_length(hello) == 6); @@ -547,7 +547,7 @@ main(void) test_base64(); test_get_base64(); test_node_equals(); - test_node_from_string(); + test_node_from_syntax(); test_node_from_substring(); test_simple_node(); test_literal(); diff --git a/test/test_node_syntax.c b/test/test_node_syntax.c new file mode 100644 index 00000000..bb394c43 --- /dev/null +++ b/test/test_node_syntax.c @@ -0,0 +1,154 @@ +/* + Copyright 2020 David Robillard <d@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 <stdbool.h> +#include <stdio.h> +#include <string.h> + +static bool +test(const SerdSyntax syntax, SerdNode* const node, const char* const expected) +{ + SerdEnv* const env = serd_env_new(SERD_STRING("http://example.org/base/")); + char* const str = serd_node_to_syntax(node, syntax, env); + SerdNode* const copy = serd_node_from_syntax(str, syntax, env); + + const bool success = !strcmp(str, expected) && serd_node_equals(copy, node); + + serd_node_free(copy); + serd_free(str); + serd_node_free(node); + serd_env_free(env); + return success; +} + +static void +test_common(const SerdSyntax syntax) +{ + static const int data[] = {4, 2}; + + static const SerdStringView datatype = + SERD_STRING("http://example.org/Datatype"); + + SerdNode* const num_type = + serd_new_uri(SERD_STRING("http://example.org/Decimal")); + + assert(test(syntax, serd_new_string(SERD_STRING("node")), "\"node\"")); + + assert(test(syntax, + serd_new_plain_literal(SERD_STRING("hallo"), SERD_STRING("de")), + "\"hallo\"@de")); + + assert(test(syntax, + serd_new_typed_literal(SERD_STRING("X"), datatype), + "\"X\"^^<http://example.org/Datatype>")); + + assert(test(syntax, serd_new_blank(SERD_STRING("blank")), "_:blank")); + assert(test(syntax, serd_new_blank(SERD_STRING("b0")), "_:b0")); + + assert(test(syntax, + serd_new_uri(SERD_STRING("http://example.org/")), + "<http://example.org/>")); + + assert(test(syntax, + serd_new_decimal(1.25, num_type), + "\"1.25\"^^<http://example.org/Decimal>")); + + assert(test(syntax, + serd_new_double(1.25), + "\"1.25E0\"^^<http://www.w3.org/2001/XMLSchema#double>")); + + assert(test(syntax, + serd_new_float(1.25), + "\"1.25E0\"^^<http://www.w3.org/2001/XMLSchema#float>")); + + assert(test(syntax, + serd_new_integer(1234, num_type), + "\"1234\"^^<http://example.org/Decimal>")); + + assert( + test(syntax, + serd_new_base64(data, sizeof(data), NULL), + "\"BAAAAAIAAAA=\"^^<http://www.w3.org/2001/XMLSchema#base64Binary>")); + + serd_node_free(num_type); +} + +static void +test_ntriples(void) +{ + test_common(SERD_NTRIPLES); + + { + // No relative URIs in NTriples, so converting one fails without an env + SerdNode* const rel = serd_new_uri(SERD_STRING("rel/uri")); + assert(!serd_node_to_syntax(rel, SERD_NTRIPLES, NULL)); + assert(!serd_node_from_syntax("<rel/uri>", SERD_NTRIPLES, NULL)); + + // If a relative URI can be expanded then all's well + SerdEnv* const env = serd_env_new(SERD_STRING("http://example.org/base/")); + char* const str = serd_node_to_syntax(rel, SERD_NTRIPLES, env); + assert(!strcmp(str, "<http://example.org/base/rel/uri>")); + + SerdNode* const copy = serd_node_from_syntax(str, SERD_NTRIPLES, env); + assert(!strcmp(serd_node_string(copy), "http://example.org/base/rel/uri")); + + serd_node_free(copy); + serd_env_free(env); + serd_free(str); + serd_node_free(rel); + } + + assert(test(SERD_NTRIPLES, + serd_new_decimal(1.25, NULL), + "\"1.25\"^^<http://www.w3.org/2001/XMLSchema#decimal>")); + + assert(test(SERD_NTRIPLES, + serd_new_integer(1234, NULL), + "\"1234\"^^<http://www.w3.org/2001/XMLSchema#integer>")); + + assert(test(SERD_NTRIPLES, + serd_new_boolean(true), + "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>")); + + assert(test(SERD_NTRIPLES, + serd_new_boolean(false), + "\"false\"^^<http://www.w3.org/2001/XMLSchema#boolean>")); +} + +static void +test_turtle(void) +{ + test_common(SERD_TURTLE); + test(SERD_TURTLE, serd_new_uri(SERD_STRING("rel/uri")), "<rel/uri>"); + assert(test(SERD_TURTLE, serd_new_decimal(1.25, NULL), "1.25")); + assert(test(SERD_TURTLE, serd_new_integer(1234, NULL), "1234")); + assert(test(SERD_TURTLE, serd_new_boolean(true), "true")); + assert(test(SERD_TURTLE, serd_new_boolean(false), "false")); +} + +int +main(void) +{ + test_ntriples(); + test_turtle(); + + return 0; +} |