Browse Source

tests: test cJSON_GetObjectItem and cJSON_GetObjectItemCaseSensitive

Max Bruckner 8 years ago
parent
commit
56b819bfbc
1 changed files with 65 additions and 0 deletions
  1. 65 0
      tests/misc_tests.c

+ 65 - 0
tests/misc_tests.c

@@ -64,12 +64,77 @@ static void cjson_array_foreach_should_not_dereference_null_pointer(void)
     cJSON_ArrayForEach(element, array);
     cJSON_ArrayForEach(element, array);
 }
 }
 
 
+static void cjson_get_object_item_should_get_object_items(void)
+{
+    cJSON *item = NULL;
+    cJSON *found = NULL;
+
+    item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
+
+    found = cJSON_GetObjectItem(NULL, "test");
+    TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
+
+    found = cJSON_GetObjectItem(item, NULL);
+    TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
+
+
+    found = cJSON_GetObjectItem(item, "one");
+    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
+    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
+
+    found = cJSON_GetObjectItem(item, "tWo");
+    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
+    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
+
+    found = cJSON_GetObjectItem(item, "three");
+    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
+    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
+
+    found = cJSON_GetObjectItem(item, "four");
+    TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
+
+    cJSON_Delete(item);
+}
+
+static void cjson_get_object_item_case_sensitive_should_get_object_items(void)
+{
+    cJSON *item = NULL;
+    cJSON *found = NULL;
+
+    item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
+
+    found = cJSON_GetObjectItemCaseSensitive(NULL, "test");
+    TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
+
+    found = cJSON_GetObjectItemCaseSensitive(item, NULL);
+    TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
+
+    found = cJSON_GetObjectItemCaseSensitive(item, "one");
+    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
+    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
+
+    found = cJSON_GetObjectItemCaseSensitive(item, "Two");
+    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
+    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
+
+    found = cJSON_GetObjectItemCaseSensitive(item, "tHree");
+    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
+    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
+
+    found = cJSON_GetObjectItemCaseSensitive(item, "One");
+    TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
+
+    cJSON_Delete(item);
+}
+
 int main(void)
 int main(void)
 {
 {
     UNITY_BEGIN();
     UNITY_BEGIN();
 
 
     RUN_TEST(cjson_array_foreach_should_loop_over_arrays);
     RUN_TEST(cjson_array_foreach_should_loop_over_arrays);
     RUN_TEST(cjson_array_foreach_should_not_dereference_null_pointer);
     RUN_TEST(cjson_array_foreach_should_not_dereference_null_pointer);
+    RUN_TEST(cjson_get_object_item_should_get_object_items);
+    RUN_TEST(cjson_get_object_item_case_sensitive_should_get_object_items);
 
 
     return UNITY_END();
     return UNITY_END();
 }
 }