diff options
-rw-r--r-- | include/serd/serd.h | 5 | ||||
-rw-r--r-- | src/node.c | 8 | ||||
-rw-r--r-- | src/static_nodes.h | 1 | ||||
-rw-r--r-- | test/test_node.c | 21 |
4 files changed, 35 insertions, 0 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h index 697674c9..fdae8ac7 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -607,6 +607,11 @@ SERD_API SerdNode* SERD_ALLOCATED serd_new_file_uri(SerdStringView path, SerdStringView hostname); +/// Create a new node by serialising `b` into an xsd:boolean string +SERD_API +SerdNode* SERD_ALLOCATED +serd_new_boolean(bool b); + /** Create a new node by serialising `d` into an xsd:decimal string @@ -519,6 +519,14 @@ write_variant_literal(const void* const user_data, } SerdNode* +serd_new_boolean(bool b) +{ + return serd_new_typed_literal(b ? SERD_STATIC_STRING("true") + : SERD_STATIC_STRING("false"), + serd_node_string_view(&serd_xsd_boolean.node)); +} + +SerdNode* serd_new_decimal(const double d, const SerdNode* const datatype) { // Use given datatype, or xsd:decimal as a default if it is null diff --git a/src/static_nodes.h b/src/static_nodes.h index 18950444..952c0634 100644 --- a/src/static_nodes.h +++ b/src/static_nodes.h @@ -32,6 +32,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/test/test_node.c b/test/test_node.c index 81ac3378..1e38ee9b 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -50,6 +50,26 @@ test_integer_to_node(void) } static void +test_boolean(void) +{ + SerdNode* const true_node = serd_new_boolean(true); + assert(!strcmp(serd_node_string(true_node), "true")); + + const SerdNode* const true_datatype = serd_node_datatype(true_node); + assert(true_datatype); + assert(!strcmp(serd_node_string(true_datatype), NS_XSD "boolean")); + serd_node_free(true_node); + + SerdNode* const false_node = serd_new_boolean(false); + assert(!strcmp(serd_node_string(false_node), "false")); + + const SerdNode* const false_datatype = serd_node_datatype(false_node); + assert(false_datatype); + assert(!strcmp(serd_node_string(false_datatype), NS_XSD "boolean")); + serd_node_free(false_node); +} + +static void test_blob_to_node(void) { assert(!serd_new_blob(&SERD_URI_NULL, 0, NULL)); @@ -201,6 +221,7 @@ main(void) { test_integer_to_node(); test_blob_to_node(); + test_boolean(); test_node_equals(); test_node_from_string(); test_node_from_substring(); |