From 767cda5151d5f53f73366431c865d6bc7b0ae66c Mon Sep 17 00:00:00 2001 From: ooxi Date: Thu, 19 Jan 2023 19:23:23 +0100 Subject: [PATCH] Testcase demonstrating issue #33 --- test/CMakeLists.txt | 26 +++++++++++ test/test-issue-33.c | 107 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 test/test-issue-33.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 09097d0..5f5b770 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -126,3 +126,29 @@ add_test( COMMAND "${PROJECT_NAME}-test-huitre39" ) + + +# Test space in attribute value (issue #33) +add_executable( + "${PROJECT_NAME}-test-issue-33" + "${CMAKE_CURRENT_LIST_DIR}/test-issue-33.c" +) + +target_compile_options( + "${PROJECT_NAME}-test-issue-33" + PRIVATE + -std=c11 +) + +target_link_libraries( + "${PROJECT_NAME}-test-issue-33" + PRIVATE + xml +) + + +add_test( + NAME "${PROJECT_NAME}-test-issue-33" + COMMAND "${PROJECT_NAME}-test-issue-33" +) + diff --git a/test/test-issue-33.c b/test/test-issue-33.c new file mode 100644 index 0000000..babe5e0 --- /dev/null +++ b/test/test-issue-33.c @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2023 ooxi/xml.c + * https://github.com/ooxi/xml.c + * + * This software is provided 'as-is', without any express or implied warranty. + * In no event will the authors be held liable for any damages arising from the + * use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software in a + * product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source distribution. + */ +#include +#include +#include +#include +#include +#include +#include +#include + + + + + +/** + * @return true iff xml string equals the c string + */ +static bool string_equals(struct xml_string* a, char const* b) { + size_t a_length = xml_string_length(a); + size_t b_length = strlen(b); + + uint8_t* a_buffer = alloca((a_length + 1) * sizeof(uint8_t)); + xml_string_copy(a, a_buffer, a_length); + a_buffer[a_length] = 0; + + if (a_length != b_length) { + fprintf(stderr, "string_equals: %s#%i <> %s#%i\n", a_buffer, (int)a_length, b, (int)b_length); + return false; + } + + size_t i = 0; for (; i < a_length; ++i) { + if (a_buffer[i] != b[i]) { + fprintf(stderr, "string_equals: %s <> %s\n", a_buffer, b); + return false; + } + } + + return true; +} + + + + + +/** + * Spaces in attribute values did not work at one time. + * + * @author nashidau + * @author ooxi + * + * @see https://github.com/ooxi/xml.c/issues/33 + */ +int main(int argc, char** argv) { + uint8_t* source = ""; + + struct xml_document* document = xml_parse_document(source, strlen(source)); + + if (!document) { + fprintf(stderr, "Could parse document\n"); + return EXIT_FAILURE; + } + struct xml_node* node_a = xml_document_root(document); + struct xml_string* attribute_foo_name = xml_node_attribute_name(node_a, 0); + struct xml_string* attribute_foo_content = xml_node_attribute_content(node_a, 0); + + if (!attribute_foo_name) { + fprintf(stderr, "Cannot load name of attribute #0\n"); + return EXIT_FAILURE; + } + if (!attribute_foo_content) { + fprintf(stderr, "Cannot load content of attribute #0\n"); + return EXIT_FAILURE; + } + + if (!string_equals(attribute_foo_name, "foo")) { + fprintf(stderr, "Name of attribute #0 must be `foo'\n"); + return EXIT_FAILURE; + } + if (!string_equals(attribute_foo_content, "Has Spaces")) { + fprintf(stderr, "Content of attribute #0 must be `Has Spaces'\n"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} +