From 60bb4a81c03d6b0a08f9c858ff1a99bf3bcc602e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 26 Dec 2018 11:54:21 -0500 Subject: Add serd_new_boolean() --- serd/serd.h | 5 +++++ src/node.c | 6 ++++++ src/static_nodes.h | 1 + tests/serd_test.c | 17 +++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/serd/serd.h b/serd/serd.h index 433f38e8..cd2a7031 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -712,6 +712,11 @@ SERD_API SerdNode* serd_new_integer(int64_t i, const SerdNode* datatype); +/// Create a new node by serialising `b` into an xsd:boolean string +SERD_API +SerdNode* +serd_new_boolean(bool b); + /** Create a node by serialising `buf` into an xsd:base64Binary string diff --git a/src/node.c b/src/node.c index 6853feae..ea571922 100644 --- a/src/node.c +++ b/src/node.c @@ -674,6 +674,12 @@ serd_new_integer(int64_t i, const SerdNode* datatype) return node; } +SerdNode* +serd_new_boolean(bool b) +{ + return serd_new_typed_literal(b ? "true" : "false", &serd_xsd_boolean.node); +} + SerdNode* serd_new_blob(const void* buf, size_t size, diff --git a/src/static_nodes.h b/src/static_nodes.h index 430fa58c..1399f580 100644 --- a/src/static_nodes.h +++ b/src/static_nodes.h @@ -30,6 +30,7 @@ typedef struct StaticNode { {sizeof(NS_XSD #name) - 1, 0, SERD_URI}, NS_XSD #name}; DEFINE_XSD_NODE(base64Binary) +DEFINE_XSD_NODE(boolean) DEFINE_XSD_NODE(decimal) DEFINE_XSD_NODE(integer) diff --git a/tests/serd_test.c b/tests/serd_test.c index 7a9e67bd..34463f7e 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -304,6 +304,22 @@ test_integer_to_node(void) } } +static void +test_boolean(void) +{ + SerdNode* true_node = serd_new_boolean(true); + assert(!strcmp(serd_node_string(true_node), "true")); + assert(!strcmp(serd_node_string(serd_node_datatype(true_node)), + NS_XSD "boolean")); + serd_node_free(true_node); + + SerdNode* false_node = serd_new_boolean(false); + assert(!strcmp(serd_node_string(false_node), "false")); + assert(!strcmp(serd_node_string(serd_node_datatype(false_node)), + NS_XSD "boolean")); + serd_node_free(false_node); +} + static void test_blob_to_node(void) { @@ -773,6 +789,7 @@ main(void) test_double_to_node(); test_integer_to_node(); test_blob_to_node(); + test_boolean(); test_strlen(); test_strerror(); test_uri_parsing(); -- cgit v1.2.1