Bladeren bron

Change name from Configuration to Context

Max Bruckner 7 jaren geleden
bovenliggende
commit
064eec8208

File diff suppressed because it is too large
+ 153 - 153
cJSON.c


+ 20 - 17
cJSON.h

@@ -87,7 +87,6 @@ typedef struct cJSON_Allocators
 } cJSON_Allocators;
 
 typedef int cJSON_bool;
-typedef void* cJSON_Configuration;
 
 #if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
 #define __WINDOWS__
@@ -141,36 +140,40 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ
 /* returns the version of cJSON as a string */
 CJSON_PUBLIC(const char*) cJSON_Version(void);
 
+typedef void* cJSON_Context;
 /*
- * Create a configuration object that can be passed to several functions
- * to configure their behavior. It will be set to the default values initially, they
- * can be changed later.
+ * Create a context object that can be passed to several functions
+ * to configure their behavior and/or take their output. It will be set to the default values
+ * initially, they can be changed later using the builder pattern by passing it to functions
+ * that change one setting.
  *
- * A cJSON_Configuration object is dynamically allocated and you are responsible to free it
+ * A cJSON_Context object is dynamically allocated and you are responsible to free it
  * after use.
  *
  * If allocators is a NULL pointer, malloc and free are used.
  *
  * allocator_userdata can be used to pass custom data to your allocator (e.g. for pool allocators).
  * */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON_Allocators * const allocators, void *allocator_userdata);
-/* Create a copy of an existing configuration */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_DuplicateConfiguration(const cJSON_Configuration, const cJSON_Allocators * const allocators, void *allocator_userdata);
-/* Change the allocators of a cJSON_Configuration and reset the userdata */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeAllocators(cJSON_Configuration configuration, const cJSON_Allocators allocators);
-/* Change the allocator userdata attached to a cJSON_Configuration */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Configuration configuration, void *userdata);
+CJSON_PUBLIC(cJSON_Context) cJSON_CreateContext(const cJSON_Allocators * const allocators, void *allocator_userdata);
+/* Create a copy of an existing context */
+CJSON_PUBLIC(cJSON_Context) cJSON_DuplicateContext(const cJSON_Context, const cJSON_Allocators * const allocators, void *allocator_userdata);
+
+/* The following functions work on a context in order to set and retrieve data: */
+/* Change the allocators of a cJSON_Context and reset the userdata */
+CJSON_PUBLIC(cJSON_Context) cJSON_SetAllocators(cJSON_Context context, const cJSON_Allocators allocators);
+/* Change the allocator userdata attached to a cJSON_Context */
+CJSON_PUBLIC(cJSON_Context) cJSON_SetUserdata(cJSON_Context context, void *userdata);
 /* Get the position relative to the JSON where the parser stopped, return 0 if invalid. */
-CJSON_PUBLIC(size_t) cJSON_ConfigurationGetParseEnd(cJSON_Configuration configuration);
+CJSON_PUBLIC(size_t) cJSON_GetParseEnd(cJSON_Context context);
 /* Set how many bytes should be initially allocated for printing */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size);
+CJSON_PUBLIC(cJSON_Context) cJSON_SetPrebufferSize(cJSON_Context context, const size_t buffer_size);
 typedef enum { CJSON_FORMAT_MINIFIED = 0, CJSON_FORMAT_DEFAULT = 1 } cJSON_Format;
 /* Change the format for printing */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeFormat(cJSON_Configuration configuration, cJSON_Format format);
+CJSON_PUBLIC(cJSON_Context) cJSON_SetFormat(cJSON_Context context, cJSON_Format format);
 /* Change the case sensitivity */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeCaseSensitivity(cJSON_Configuration configuration, cJSON_bool case_sensitive);
+CJSON_PUBLIC(cJSON_Context) cJSON_MakeCaseSensitive(cJSON_Context context, cJSON_bool case_sensitive);
 /* Change if data is allowed after the JSON */
-CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeAllowDataAfterJson(cJSON_Configuration configuration, cJSON_bool allow_data_after_json);
+CJSON_PUBLIC(cJSON_Context) cJSON_AllowDataAfterJson(cJSON_Context context, cJSON_bool allow_data_after_json);
 
 /* Supply malloc and free functions to cJSON globally */
 CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);

+ 1 - 1
tests/CMakeLists.txt

@@ -57,7 +57,7 @@ if(ENABLE_CJSON_TEST)
         compare_tests
         cjson_add
         readme_examples
-        configuration_tests
+        context_tests
     )
 
     option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.")

+ 2 - 2
tests/common.h

@@ -33,11 +33,11 @@ void reset(cJSON *item) {
     }
     if ((item->valuestring != NULL) && !(item->type & cJSON_IsReference))
     {
-        global_configuration.allocators.deallocate(item->valuestring, global_configuration.userdata);
+        global_context.allocators.deallocate(item->valuestring, global_context.userdata);
     }
     if ((item->string != NULL) && !(item->type & cJSON_StringIsConst))
     {
-        global_configuration.allocators.deallocate(item->string, global_configuration.userdata);
+        global_context.allocators.deallocate(item->string, global_context.userdata);
     }
 
     memset(item, 0, sizeof(cJSON));

+ 0 - 264
tests/configuration_tests.c

@@ -1,264 +0,0 @@
-/*
-  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
-
-  Permission is hereby granted, free of charge, to any person obtaining a copy
-  of this software and associated documentation files (the "Software"), to deal
-  in the Software without restriction, including without limitation the rights
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-  copies of the Software, and to permit persons to whom the Software is
-  furnished to do so, subject to the following conditions:
-
-  The above copyright notice and this permission notice shall be included in
-  all copies or substantial portions of the Software.
-
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-  THE SOFTWARE.
-*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "unity/examples/unity_config.h"
-#include "unity/src/unity.h"
-#include "common.h"
-
-static void create_configuration_should_create_a_configuration(void)
-{
-    internal_configuration *configuration = NULL;
-
-    configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-    TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 256, "buffer_size has an incorrect value.");
-    TEST_ASSERT_TRUE_MESSAGE(configuration->format, "format has an incorrect value.");
-    TEST_ASSERT_TRUE_MESSAGE(configuration->case_sensitive, "case_sensitive has an incorrect value.");
-    TEST_ASSERT_TRUE_MESSAGE(configuration->allow_data_after_json, "allow_data_after_json has an incorrect value.");
-    TEST_ASSERT_NULL_MESSAGE(configuration->userdata, "Userdata should be NULL");
-    TEST_ASSERT_TRUE_MESSAGE(malloc_wrapper == configuration->allocators.allocate, "Wrong malloc.");
-    TEST_ASSERT_TRUE_MESSAGE(realloc_wrapper == configuration->allocators.reallocate, "Wrong realloc.");
-    TEST_ASSERT_TRUE_MESSAGE(free_wrapper == configuration->allocators.deallocate, "Wrong free.");
-
-    free(configuration);
-}
-
-static void* custom_allocator(size_t size, void *userdata)
-{
-    *((size_t*)userdata) = size;
-    return malloc(size);
-}
-static void custom_deallocator(void *pointer, void *userdata)
-{
-    *((size_t*)userdata) = (size_t)pointer;
-    free(pointer);
-}
-
-static void create_configuration_should_take_custom_allocators(void)
-{
-    internal_configuration *configuration = NULL;
-    cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
-    size_t userdata = 0;
-
-    configuration = (internal_configuration*)cJSON_CreateConfiguration(&allocators, &userdata);
-    TEST_ASSERT_NOT_NULL(configuration);
-    TEST_ASSERT_EQUAL_MESSAGE(userdata, sizeof(internal_configuration), "custom allocator wasn't run properly.");
-    TEST_ASSERT_TRUE_MESSAGE(global_default_configuration.allocators.allocate == configuration->allocators.allocate, "Wrong allocator.");
-    TEST_ASSERT_TRUE_MESSAGE(global_default_configuration.allocators.deallocate == configuration->allocators.deallocate, "Wrong deallocator.");
-    TEST_ASSERT_TRUE_MESSAGE(global_default_configuration.allocators.reallocate == configuration->allocators.reallocate, "Wrong reallocator.");
-
-    custom_deallocator(configuration, &userdata);
-}
-
-static void create_configuration_should_not_take_incomplete_allocators(void)
-{
-    cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
-    cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
-    size_t userdata = 0;
-
-    TEST_ASSERT_NULL(cJSON_CreateConfiguration(&allocators1, &userdata));
-    TEST_ASSERT_NULL(cJSON_CreateConfiguration(&allocators2, &userdata));
-}
-
-static void duplicate_configuration_should_duplicate_a_configuration(void)
-{
-    internal_configuration *configuration = NULL;
-
-    configuration = (internal_configuration*)cJSON_DuplicateConfiguration(&global_configuration, NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    TEST_ASSERT_EQUAL_MEMORY(&global_configuration, configuration, sizeof(internal_configuration));
-
-    free(configuration);
-}
-
-static void duplicate_configuration_should_take_custom_allocators(void)
-{
-    internal_configuration *configuration = NULL;
-    cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
-    size_t userdata = 0;
-
-    configuration = (internal_configuration*)cJSON_DuplicateConfiguration(&global_configuration, &allocators, &userdata);
-    TEST_ASSERT_NOT_NULL(configuration);
-    TEST_ASSERT_EQUAL_MESSAGE(userdata, sizeof(internal_configuration), "custom allocator wasn't run properly");
-
-    TEST_ASSERT_EQUAL_MEMORY(&global_configuration, configuration, sizeof(internal_configuration));
-    free(configuration);
-}
-
-static void duplicate_configuration_should_not_take_incomplete_allocators(void)
-{
-    cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
-    cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
-    size_t userdata = 0;
-
-    TEST_ASSERT_NULL(cJSON_DuplicateConfiguration(&global_configuration, &allocators1, &userdata));
-    TEST_ASSERT_NULL(cJSON_DuplicateConfiguration(&global_configuration, &allocators2, &userdata));
-}
-
-static void configuration_change_allocators_should_change_allocators(void)
-{
-    internal_configuration *configuration = NULL;
-    cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
-    size_t userdata = 0;
-
-    configuration = (internal_configuration*)cJSON_CreateConfiguration(&allocators, &userdata);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    configuration = (internal_configuration*)cJSON_ConfigurationChangeAllocators(configuration, allocators);
-    TEST_ASSERT_NOT_NULL(configuration);
-    TEST_ASSERT_TRUE_MESSAGE(custom_allocator == configuration->allocators.allocate, "Wrong allocator.");
-    TEST_ASSERT_TRUE_MESSAGE(custom_deallocator == configuration->allocators.deallocate, "Wrong deallocator.");
-    TEST_ASSERT_NULL_MESSAGE(configuration->allocators.reallocate, "Reallocator is not null");
-
-    custom_deallocator(configuration, &userdata);
-}
-
-static void configuration_change_allocators_should_not_change_incomplete_allocators(void)
-{
-    internal_configuration *configuration = NULL;
-    cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
-    cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
-
-    configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    TEST_ASSERT_NULL(cJSON_ConfigurationChangeAllocators(configuration, allocators1));
-    TEST_ASSERT_NULL(cJSON_ConfigurationChangeAllocators(configuration, allocators2));
-
-    free(configuration);
-}
-
-static void configuration_change_userdata_should_change_userdata(void)
-{
-    internal_configuration *configuration = NULL;
-    size_t userdata = 0;
-    configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    configuration = (internal_configuration*)cJSON_ConfigurationChangeUserdata(configuration, &userdata);
-    TEST_ASSERT_TRUE_MESSAGE(configuration->userdata == &userdata, "Userdata is incorrect.");
-
-    free(configuration);
-}
-
-static void configuration_get_parse_end_should_get_the_parse_end(void)
-{
-    internal_configuration configuration = global_default_configuration;
-    configuration.end_position = 42;
-
-    TEST_ASSERT_EQUAL_MESSAGE(cJSON_ConfigurationGetParseEnd(&configuration), 42, "Failed to get parse end.");
-}
-
-static void configuration_change_prebuffer_size_should_change_buffer_size(void)
-{
-    internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    configuration = (internal_configuration*)cJSON_ConfigurationChangePrebufferSize(configuration, 1024);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 1024, "Didn't set the buffer size correctly.");
-
-    free(configuration);
-}
-
-static void configuration_change_prebuffer_size_should_not_allow_empty_sizes(void)
-{
-    internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    TEST_ASSERT_NULL(cJSON_ConfigurationChangePrebufferSize(configuration, 0));
-
-    free(configuration);
-}
-
-static void configuration_change_format_should_change_format(void)
-{
-    internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    configuration = (internal_configuration*)cJSON_ConfigurationChangeFormat(configuration, CJSON_FORMAT_MINIFIED);
-    TEST_ASSERT_NOT_NULL(configuration);
-    TEST_ASSERT_FALSE_MESSAGE(configuration->format, "Failed to set CJSON_FORMAT_MINIFIED.");
-
-    configuration = (internal_configuration*)cJSON_ConfigurationChangeFormat(configuration, CJSON_FORMAT_DEFAULT);
-    TEST_ASSERT_NOT_NULL(configuration);
-    TEST_ASSERT_TRUE_MESSAGE(configuration->format, "Failed to set CJSON_FORMAT_DEFAULT.");
-
-    TEST_ASSERT_NULL_MESSAGE(cJSON_ConfigurationChangeFormat(configuration, (cJSON_Format)3), "Failed to detect invalid format.");
-
-    free(configuration);
-}
-
-static void configuration_change_case_sensitivity_should_change_case_sensitivity(void)
-{
-    internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    configuration = (internal_configuration*)cJSON_ConfigurationChangeCaseSensitivity(configuration, false);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    TEST_ASSERT_FALSE_MESSAGE(configuration->case_sensitive, "Didn't set the case sensitivity correctly.");
-
-    free(configuration);
-}
-
-static void configuration_change_allow_data_after_json_should_change_allow_data_after_json(void)
-{
-    internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    configuration = (internal_configuration*)cJSON_ConfigurationChangeAllowDataAfterJson(configuration, false);
-    TEST_ASSERT_NOT_NULL(configuration);
-
-    TEST_ASSERT_FALSE_MESSAGE(configuration->allow_data_after_json, "Didn't set allow_data_after_json property correctly.");
-
-    free(configuration);
-}
-
-int main(void)
-{
-    UNITY_BEGIN();
-
-    RUN_TEST(create_configuration_should_create_a_configuration);
-    RUN_TEST(create_configuration_should_take_custom_allocators);
-    RUN_TEST(create_configuration_should_not_take_incomplete_allocators);
-    RUN_TEST(duplicate_configuration_should_duplicate_a_configuration);
-    RUN_TEST(duplicate_configuration_should_take_custom_allocators);
-    RUN_TEST(duplicate_configuration_should_not_take_incomplete_allocators);
-    RUN_TEST(configuration_change_allocators_should_change_allocators);
-    RUN_TEST(configuration_change_allocators_should_not_change_incomplete_allocators);
-    RUN_TEST(configuration_change_userdata_should_change_userdata);
-    RUN_TEST(configuration_get_parse_end_should_get_the_parse_end);
-    RUN_TEST(configuration_change_prebuffer_size_should_change_buffer_size);
-    RUN_TEST(configuration_change_prebuffer_size_should_not_allow_empty_sizes);
-    RUN_TEST(configuration_change_format_should_change_format);
-    RUN_TEST(configuration_change_case_sensitivity_should_change_case_sensitivity);
-    RUN_TEST(configuration_change_allow_data_after_json_should_change_allow_data_after_json);
-
-    return UNITY_END();
-}

+ 264 - 0
tests/context_tests.c

@@ -0,0 +1,264 @@
+/*
+  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
+
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "unity/examples/unity_config.h"
+#include "unity/src/unity.h"
+#include "common.h"
+
+static void create_context_should_create_a_context(void)
+{
+    internal_context *context = NULL;
+
+    context = (internal_context*)cJSON_CreateContext(NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+    TEST_ASSERT_EQUAL_MESSAGE(context->buffer_size, 256, "buffer_size has an incorrect value.");
+    TEST_ASSERT_TRUE_MESSAGE(context->format, "format has an incorrect value.");
+    TEST_ASSERT_TRUE_MESSAGE(context->case_sensitive, "case_sensitive has an incorrect value.");
+    TEST_ASSERT_TRUE_MESSAGE(context->allow_data_after_json, "allow_data_after_json has an incorrect value.");
+    TEST_ASSERT_NULL_MESSAGE(context->userdata, "Userdata should be NULL");
+    TEST_ASSERT_TRUE_MESSAGE(malloc_wrapper == context->allocators.allocate, "Wrong malloc.");
+    TEST_ASSERT_TRUE_MESSAGE(realloc_wrapper == context->allocators.reallocate, "Wrong realloc.");
+    TEST_ASSERT_TRUE_MESSAGE(free_wrapper == context->allocators.deallocate, "Wrong free.");
+
+    free(context);
+}
+
+static void* custom_allocator(size_t size, void *userdata)
+{
+    *((size_t*)userdata) = size;
+    return malloc(size);
+}
+static void custom_deallocator(void *pointer, void *userdata)
+{
+    *((size_t*)userdata) = (size_t)pointer;
+    free(pointer);
+}
+
+static void create_context_should_take_custom_allocators(void)
+{
+    internal_context *context = NULL;
+    cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
+    size_t userdata = 0;
+
+    context = (internal_context*)cJSON_CreateContext(&allocators, &userdata);
+    TEST_ASSERT_NOT_NULL(context);
+    TEST_ASSERT_EQUAL_MESSAGE(userdata, sizeof(internal_context), "custom allocator wasn't run properly.");
+    TEST_ASSERT_TRUE_MESSAGE(global_default_context.allocators.allocate == context->allocators.allocate, "Wrong allocator.");
+    TEST_ASSERT_TRUE_MESSAGE(global_default_context.allocators.deallocate == context->allocators.deallocate, "Wrong deallocator.");
+    TEST_ASSERT_TRUE_MESSAGE(global_default_context.allocators.reallocate == context->allocators.reallocate, "Wrong reallocator.");
+
+    custom_deallocator(context, &userdata);
+}
+
+static void create_context_should_not_take_incomplete_allocators(void)
+{
+    cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
+    cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
+    size_t userdata = 0;
+
+    TEST_ASSERT_NULL(cJSON_CreateContext(&allocators1, &userdata));
+    TEST_ASSERT_NULL(cJSON_CreateContext(&allocators2, &userdata));
+}
+
+static void duplicate_context_should_duplicate_a_context(void)
+{
+    internal_context *context = NULL;
+
+    context = (internal_context*)cJSON_DuplicateContext(&global_context, NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+
+    TEST_ASSERT_EQUAL_MEMORY(&global_context, context, sizeof(internal_context));
+
+    free(context);
+}
+
+static void duplicate_context_should_take_custom_allocators(void)
+{
+    internal_context *context = NULL;
+    cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
+    size_t userdata = 0;
+
+    context = (internal_context*)cJSON_DuplicateContext(&global_context, &allocators, &userdata);
+    TEST_ASSERT_NOT_NULL(context);
+    TEST_ASSERT_EQUAL_MESSAGE(userdata, sizeof(internal_context), "custom allocator wasn't run properly");
+
+    TEST_ASSERT_EQUAL_MEMORY(&global_context, context, sizeof(internal_context));
+    free(context);
+}
+
+static void duplicate_context_should_not_take_incomplete_allocators(void)
+{
+    cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
+    cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
+    size_t userdata = 0;
+
+    TEST_ASSERT_NULL(cJSON_DuplicateContext(&global_context, &allocators1, &userdata));
+    TEST_ASSERT_NULL(cJSON_DuplicateContext(&global_context, &allocators2, &userdata));
+}
+
+static void set_allocators_should_set_allocators(void)
+{
+    internal_context *context = NULL;
+    cJSON_Allocators allocators = {custom_allocator, custom_deallocator, NULL};
+    size_t userdata = 0;
+
+    context = (internal_context*)cJSON_CreateContext(&allocators, &userdata);
+    TEST_ASSERT_NOT_NULL(context);
+
+    context = (internal_context*)cJSON_SetAllocators(context, allocators);
+    TEST_ASSERT_NOT_NULL(context);
+    TEST_ASSERT_TRUE_MESSAGE(custom_allocator == context->allocators.allocate, "Wrong allocator.");
+    TEST_ASSERT_TRUE_MESSAGE(custom_deallocator == context->allocators.deallocate, "Wrong deallocator.");
+    TEST_ASSERT_NULL_MESSAGE(context->allocators.reallocate, "Reallocator is not null");
+
+    custom_deallocator(context, &userdata);
+}
+
+static void set_allocators_should_not_set_incomplete_allocators(void)
+{
+    internal_context *context = NULL;
+    cJSON_Allocators allocators1 = {custom_allocator, NULL, NULL};
+    cJSON_Allocators allocators2 = {NULL, custom_deallocator, NULL};
+
+    context = (internal_context*)cJSON_CreateContext(NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+
+    TEST_ASSERT_NULL(cJSON_SetAllocators(context, allocators1));
+    TEST_ASSERT_NULL(cJSON_SetAllocators(context, allocators2));
+
+    free(context);
+}
+
+static void set_userdata_should_set_userdata(void)
+{
+    internal_context *context = NULL;
+    size_t userdata = 0;
+    context = (internal_context*)cJSON_CreateContext(NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+
+    context = (internal_context*)cJSON_SetUserdata(context, &userdata);
+    TEST_ASSERT_TRUE_MESSAGE(context->userdata == &userdata, "Userdata is incorrect.");
+
+    free(context);
+}
+
+static void get_parse_end_should_get_the_parse_end(void)
+{
+    internal_context context = global_default_context;
+    context.end_position = 42;
+
+    TEST_ASSERT_EQUAL_MESSAGE(cJSON_GetParseEnd(&context), 42, "Failed to get parse end.");
+}
+
+static void set_prebuffer_size_should_set_buffer_size(void)
+{
+    internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+
+    context = (internal_context*)cJSON_SetPrebufferSize(context, 1024);
+    TEST_ASSERT_NOT_NULL(context);
+
+    TEST_ASSERT_EQUAL_MESSAGE(context->buffer_size, 1024, "Didn't set the buffer size correctly.");
+
+    free(context);
+}
+
+static void set_prebuffer_size_should_not_allow_empty_sizes(void)
+{
+    internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+
+    TEST_ASSERT_NULL(cJSON_SetPrebufferSize(context, 0));
+
+    free(context);
+}
+
+static void set_format_should_set_format(void)
+{
+    internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+
+    context = (internal_context*)cJSON_SetFormat(context, CJSON_FORMAT_MINIFIED);
+    TEST_ASSERT_NOT_NULL(context);
+    TEST_ASSERT_FALSE_MESSAGE(context->format, "Failed to set CJSON_FORMAT_MINIFIED.");
+
+    context = (internal_context*)cJSON_SetFormat(context, CJSON_FORMAT_DEFAULT);
+    TEST_ASSERT_NOT_NULL(context);
+    TEST_ASSERT_TRUE_MESSAGE(context->format, "Failed to set CJSON_FORMAT_DEFAULT.");
+
+    TEST_ASSERT_NULL_MESSAGE(cJSON_SetFormat(context, (cJSON_Format)3), "Failed to detect invalid format.");
+
+    free(context);
+}
+
+static void make_case_sensitive_should_change_case_sensitivity(void)
+{
+    internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+
+    context = (internal_context*)cJSON_MakeCaseSensitive(context, false);
+    TEST_ASSERT_NOT_NULL(context);
+
+    TEST_ASSERT_FALSE_MESSAGE(context->case_sensitive, "Didn't set the case sensitivity correctly.");
+
+    free(context);
+}
+
+static void allow_data_after_json_should_change_allow_data_after_json(void)
+{
+    internal_context *context = (internal_context*)cJSON_CreateContext(NULL, NULL);
+    TEST_ASSERT_NOT_NULL(context);
+
+    context = (internal_context*)cJSON_AllowDataAfterJson(context, false);
+    TEST_ASSERT_NOT_NULL(context);
+
+    TEST_ASSERT_FALSE_MESSAGE(context->allow_data_after_json, "Didn't set allow_data_after_json property correctly.");
+
+    free(context);
+}
+
+int main(void)
+{
+    UNITY_BEGIN();
+
+    RUN_TEST(create_context_should_create_a_context);
+    RUN_TEST(create_context_should_take_custom_allocators);
+    RUN_TEST(create_context_should_not_take_incomplete_allocators);
+    RUN_TEST(duplicate_context_should_duplicate_a_context);
+    RUN_TEST(duplicate_context_should_take_custom_allocators);
+    RUN_TEST(duplicate_context_should_not_take_incomplete_allocators);
+    RUN_TEST(set_allocators_should_set_allocators);
+    RUN_TEST(set_allocators_should_not_set_incomplete_allocators);
+    RUN_TEST(set_userdata_should_set_userdata);
+    RUN_TEST(get_parse_end_should_get_the_parse_end);
+    RUN_TEST(set_prebuffer_size_should_set_buffer_size);
+    RUN_TEST(set_prebuffer_size_should_not_allow_empty_sizes);
+    RUN_TEST(set_format_should_set_format);
+    RUN_TEST(make_case_sensitive_should_change_case_sensitivity);
+    RUN_TEST(allow_data_after_json_should_change_allow_data_after_json);
+
+    return UNITY_END();
+}

+ 6 - 6
tests/misc_tests.c

@@ -420,7 +420,7 @@ static void *failing_realloc(void *pointer, size_t size, void *userdata)
 static void ensure_should_fail_on_failed_realloc(void)
 {
     printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, true, true, {malloc_wrapper, free_wrapper, failing_realloc}, NULL, 0 } };
-    buffer.configuration.userdata = &buffer;
+    buffer.context.userdata = &buffer;
     buffer.buffer = (unsigned char*)malloc(100);
     TEST_ASSERT_NOT_NULL(buffer.buffer);
 
@@ -430,10 +430,10 @@ static void ensure_should_fail_on_failed_realloc(void)
 static void skip_utf8_bom_should_skip_bom(void)
 {
     const unsigned char string[] = "\xEF\xBB\xBF{}";
-    parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer buffer = { 0, 0, 0, 0, default_context };
     buffer.content = string;
     buffer.length = sizeof(string);
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
 
     TEST_ASSERT_TRUE(skip_utf8_bom(&buffer) == &buffer);
     TEST_ASSERT_EQUAL_UINT(3U, (unsigned int)buffer.offset);
@@ -442,10 +442,10 @@ static void skip_utf8_bom_should_skip_bom(void)
 static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
 {
     const unsigned char string[] = " \xEF\xBB\xBF{}";
-    parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer buffer = { 0, 0, 0, 0, default_context };
     buffer.content = string;
     buffer.length = sizeof(string);
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
     buffer.offset = 1;
 
     TEST_ASSERT_NULL(skip_utf8_bom(&buffer));
@@ -513,7 +513,7 @@ static void cjson_add_item_to_object_should_not_use_after_free_when_string_is_al
 {
     cJSON *object = cJSON_CreateObject();
     cJSON *number = cJSON_CreateNumber(42);
-    char *name = (char*)custom_strdup((const unsigned char*)"number", &global_configuration);
+    char *name = (char*)custom_strdup((const unsigned char*)"number", &global_context);
 
     TEST_ASSERT_NOT_NULL(object);
     TEST_ASSERT_NOT_NULL(number);

+ 4 - 4
tests/parse_array.c

@@ -44,10 +44,10 @@ static void assert_is_array(cJSON *array_item)
 
 static void assert_not_array(const char *json)
 {
-    parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer buffer = { 0, 0, 0, 0, default_context };
     buffer.content = (const unsigned char*)json;
     buffer.length = strlen(json) + sizeof("");
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
 
     TEST_ASSERT_FALSE(parse_array(item, &buffer));
     assert_is_invalid(item);
@@ -55,10 +55,10 @@ static void assert_not_array(const char *json)
 
 static void assert_parse_array(const char *json)
 {
-    parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer buffer = { 0, 0, 0, 0, default_context };
     buffer.content = (const unsigned char*)json;
     buffer.length = strlen(json) + sizeof("");
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
 
     TEST_ASSERT_TRUE(parse_array(item, &buffer));
     assert_is_array(item);

+ 1 - 1
tests/parse_number.c

@@ -45,7 +45,7 @@ static void assert_is_number(cJSON *number_item)
 
 static void assert_parse_number(const char *string, int integer, double real)
 {
-    parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer buffer = { 0, 0, 0, 0, default_context };
     buffer.content = (const unsigned char*)string;
     buffer.length = strlen(string) + sizeof("");
 

+ 4 - 4
tests/parse_object.c

@@ -52,10 +52,10 @@ static void assert_is_child(cJSON *child_item, const char *name, int type)
 
 static void assert_not_object(const char *json)
 {
-    parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
     parsebuffer.content = (const unsigned char*)json;
     parsebuffer.length = strlen(json) + sizeof("");
-    parsebuffer.configuration = global_configuration;
+    parsebuffer.context = global_context;
 
     TEST_ASSERT_FALSE(parse_object(item, &parsebuffer));
     assert_is_invalid(item);
@@ -64,10 +64,10 @@ static void assert_not_object(const char *json)
 
 static void assert_parse_object(const char *json)
 {
-    parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
     parsebuffer.content = (const unsigned char*)json;
     parsebuffer.length = strlen(json) + sizeof("");
-    parsebuffer.configuration = global_configuration;
+    parsebuffer.context = global_context;
 
     TEST_ASSERT_TRUE(parse_object(item, &parsebuffer));
     assert_is_object(item);

+ 5 - 5
tests/parse_string.c

@@ -45,24 +45,24 @@ static void assert_is_string(cJSON *string_item)
 
 static void assert_parse_string(const char *string, const char *expected)
 {
-    parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer buffer = { 0, 0, 0, 0, default_context };
     buffer.content = (const unsigned char*)string;
     buffer.length = strlen(string) + sizeof("");
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
 
     TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer), "Couldn't parse string.");
     assert_is_string(item);
     TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected.");
-    global_configuration.allocators.deallocate(item->valuestring, global_configuration.userdata);
+    global_context.allocators.deallocate(item->valuestring, global_context.userdata);
     item->valuestring = NULL;
 }
 
 static void assert_not_parse_string(const char * const string)
 {
-    parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer buffer = { 0, 0, 0, 0, default_context };
     buffer.content = (const unsigned char*)string;
     buffer.length = strlen(string) + sizeof("");
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
 
     TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer), "Malformed string should not be accepted.");
     assert_is_invalid(item);

+ 2 - 2
tests/parse_value.c

@@ -43,10 +43,10 @@ static void assert_is_value(cJSON *value_item, int type)
 
 static void assert_parse_value(const char *string, int type)
 {
-    parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer buffer = { 0, 0, 0, 0, default_context };
     buffer.content = (const unsigned char*) string;
     buffer.length = strlen(string) + sizeof("");
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
 
     TEST_ASSERT_TRUE(parse_value(item, &buffer));
     assert_is_value(item, type);

+ 8 - 8
tests/print_array.c

@@ -31,36 +31,36 @@ static void assert_print_array(const char * const expected, const char * const i
 
     cJSON item[1];
 
-    printbuffer formatted_buffer = { 0, 0, 0, 0, 0, default_configuration };
-    printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, default_configuration };
+    printbuffer formatted_buffer = { 0, 0, 0, 0, 0, default_context };
+    printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, default_context };
 
-    parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration };
+    parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
     parsebuffer.content = (const unsigned char*)input;
     parsebuffer.length = strlen(input) + sizeof("");
-    parsebuffer.configuration = global_configuration;
+    parsebuffer.context = global_context;
 
     /* buffer for formatted printing */
     formatted_buffer.buffer = printed_formatted;
     formatted_buffer.length = sizeof(printed_formatted);
     formatted_buffer.offset = 0;
     formatted_buffer.noalloc = true;
-    formatted_buffer.configuration = global_configuration;
+    formatted_buffer.context = global_context;
 
     /* buffer for unformatted printing */
     unformatted_buffer.buffer = printed_unformatted;
     unformatted_buffer.length = sizeof(printed_unformatted);
     unformatted_buffer.offset = 0;
     unformatted_buffer.noalloc = true;
-    unformatted_buffer.configuration = global_configuration;
+    unformatted_buffer.context = global_context;
 
     memset(item, 0, sizeof(item));
     TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer), "Failed to parse array.");
 
-    unformatted_buffer.configuration.format = false;
+    unformatted_buffer.context.format = false;
     TEST_ASSERT_TRUE_MESSAGE(print_array(item, &unformatted_buffer), "Failed to print unformatted string.");
     TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted array is not correct.");
 
-    formatted_buffer.configuration.format = true;
+    formatted_buffer.context.format = true;
     TEST_ASSERT_TRUE_MESSAGE(print_array(item, &formatted_buffer), "Failed to print formatted string.");
     TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted array is not correct.");
 

+ 2 - 2
tests/print_number.c

@@ -28,12 +28,12 @@ static void assert_print_number(const char *expected, double input)
 {
     unsigned char printed[1024];
     cJSON item[1];
-    printbuffer buffer = { 0, 0, 0, 0, 0, default_configuration };
+    printbuffer buffer = { 0, 0, 0, 0, 0, default_context };
     buffer.buffer = printed;
     buffer.length = sizeof(printed);
     buffer.offset = 0;
     buffer.noalloc = true;
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
 
     memset(item, 0, sizeof(item));
     cJSON_SetNumberValue(item, input);

+ 8 - 8
tests/print_object.c

@@ -31,37 +31,37 @@ static void assert_print_object(const char * const expected, const char * const
 
     cJSON item[1];
 
-    printbuffer formatted_buffer = { 0, 0, 0, 0, 0, default_configuration };
-    printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, default_configuration };
-    parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration };
+    printbuffer formatted_buffer = { 0, 0, 0, 0, 0, default_context };
+    printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, default_context };
+    parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
 
     /* buffer for parsing */
     parsebuffer.content = (const unsigned char*)input;
     parsebuffer.length = strlen(input) + sizeof("");
-    parsebuffer.configuration = global_configuration;
+    parsebuffer.context = global_context;
 
     /* buffer for formatted printing */
     formatted_buffer.buffer = printed_formatted;
     formatted_buffer.length = sizeof(printed_formatted);
     formatted_buffer.offset = 0;
     formatted_buffer.noalloc = true;
-    formatted_buffer.configuration = global_configuration;
+    formatted_buffer.context = global_context;
 
     /* buffer for unformatted printing */
     unformatted_buffer.buffer = printed_unformatted;
     unformatted_buffer.length = sizeof(printed_unformatted);
     unformatted_buffer.offset = 0;
     unformatted_buffer.noalloc = true;
-    unformatted_buffer.configuration = global_configuration;
+    unformatted_buffer.context = global_context;
 
     memset(item, 0, sizeof(item));
     TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer), "Failed to parse object.");
 
-    unformatted_buffer.configuration.format = false;
+    unformatted_buffer.context.format = false;
     TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer), "Failed to print unformatted string.");
     TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct.");
 
-    formatted_buffer.configuration.format = true;
+    formatted_buffer.context.format = true;
     TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer), "Failed to print formatted string.");
     TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted ojbect is not correct.");
 

+ 2 - 2
tests/print_string.c

@@ -27,12 +27,12 @@
 static void assert_print_string(const char *expected, const char *input)
 {
     unsigned char printed[1024];
-    printbuffer buffer = { 0, 0, 0, 0, 0, default_configuration };
+    printbuffer buffer = { 0, 0, 0, 0, 0, default_context };
     buffer.buffer = printed;
     buffer.length = sizeof(printed);
     buffer.offset = 0;
     buffer.noalloc = true;
-    buffer.configuration = global_configuration;
+    buffer.context = global_context;
 
     TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer), "Failed to print string.");
     TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected.");

+ 5 - 5
tests/print_value.c

@@ -32,18 +32,18 @@ static void assert_print_value(const char *input)
 {
     unsigned char printed[1024];
     cJSON item[1];
-    printbuffer buffer = { 0, 0, 0, 0, 0, default_configuration };
-    parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration };
+    printbuffer buffer = { 0, 0, 0, 0, 0, default_context };
+    parse_buffer parsebuffer = { 0, 0, 0, 0, default_context };
     buffer.buffer = printed;
     buffer.length = sizeof(printed);
     buffer.offset = 0;
     buffer.noalloc = true;
-    buffer.configuration = global_configuration;
-    buffer.configuration.format = false;
+    buffer.context = global_context;
+    buffer.context.format = false;
 
     parsebuffer.content = (const unsigned char*)input;
     parsebuffer.length = strlen(input) + sizeof("");
-    parsebuffer.configuration = global_configuration;
+    parsebuffer.context = global_context;
 
     memset(item, 0, sizeof(item));
 

Some files were not shown because too many files changed in this diff