xref: /third_party/cJSON/tests/parse_object.c (revision 9750e409)
1/*
2  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
3
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  THE SOFTWARE.
21*/
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "unity/examples/unity_config.h"
28#include "unity/src/unity.h"
29#include "common.h"
30
31static cJSON item[1];
32
33static void assert_is_object(cJSON *object_item)
34{
35    TEST_ASSERT_NOT_NULL_MESSAGE(object_item, "Item is NULL.");
36
37    assert_not_in_list(object_item);
38    assert_has_type(object_item, cJSON_Object);
39    assert_has_no_reference(object_item);
40    assert_has_no_const_string(object_item);
41    assert_has_no_valuestring(object_item);
42    assert_has_no_string(object_item);
43}
44
45static void assert_is_child(cJSON *child_item, const char *name, int type)
46{
47    TEST_ASSERT_NOT_NULL_MESSAGE(child_item, "Child item is NULL.");
48    TEST_ASSERT_NOT_NULL_MESSAGE(child_item->string, "Child item doesn't have a name.");
49    TEST_ASSERT_EQUAL_STRING_MESSAGE(name, child_item->string, "Child item has the wrong name.");
50    TEST_ASSERT_BITS(0xFF, type, child_item->type);
51}
52
53static void assert_not_object(const char *json)
54{
55    parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
56    parsebuffer.content = (const unsigned char*)json;
57    parsebuffer.length = strlen(json) + sizeof("");
58    parsebuffer.hooks = global_hooks;
59
60    TEST_ASSERT_FALSE(parse_object(item, &parsebuffer));
61    assert_is_invalid(item);
62    reset(item);
63}
64
65static void assert_parse_object(const char *json)
66{
67    parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
68    parsebuffer.content = (const unsigned char*)json;
69    parsebuffer.length = strlen(json) + sizeof("");
70    parsebuffer.hooks = global_hooks;
71
72    TEST_ASSERT_TRUE(parse_object(item, &parsebuffer));
73    assert_is_object(item);
74}
75
76static void parse_object_should_parse_empty_objects(void)
77{
78    assert_parse_object("{}");
79    assert_has_no_child(item);
80    reset(item);
81
82    assert_parse_object("{\n\t}");
83    assert_has_no_child(item);
84    reset(item);
85}
86
87static void parse_object_should_parse_objects_with_one_element(void)
88{
89
90    assert_parse_object("{\"one\":1}");
91    assert_is_child(item->child, "one", cJSON_Number);
92    reset(item);
93
94    assert_parse_object("{\"hello\":\"world!\"}");
95    assert_is_child(item->child, "hello", cJSON_String);
96    reset(item);
97
98    assert_parse_object("{\"array\":[]}");
99    assert_is_child(item->child, "array", cJSON_Array);
100    reset(item);
101
102    assert_parse_object("{\"null\":null}");
103    assert_is_child(item->child, "null", cJSON_NULL);
104    reset(item);
105}
106
107static void parse_object_should_parse_objects_with_multiple_elements(void)
108{
109    assert_parse_object("{\"one\":1\t,\t\"two\"\n:2, \"three\":3}");
110    assert_is_child(item->child, "one", cJSON_Number);
111    assert_is_child(item->child->next, "two", cJSON_Number);
112    assert_is_child(item->child->next->next, "three", cJSON_Number);
113    reset(item);
114
115    {
116        size_t i = 0;
117        cJSON *node = NULL;
118        int expected_types[7] =
119        {
120            cJSON_Number,
121            cJSON_NULL,
122            cJSON_True,
123            cJSON_False,
124            cJSON_Array,
125            cJSON_String,
126            cJSON_Object
127        };
128        const char *expected_names[7] =
129        {
130            "one",
131            "NULL",
132            "TRUE",
133            "FALSE",
134            "array",
135            "world",
136            "object"
137        };
138        assert_parse_object("{\"one\":1, \"NULL\":null, \"TRUE\":true, \"FALSE\":false, \"array\":[], \"world\":\"hello\", \"object\":{}}");
139
140        node = item->child;
141        for (
142                i = 0;
143                (i < (sizeof(expected_types)/sizeof(int)))
144                && (node != NULL);
145                (void)i++, node = node->next)
146        {
147            assert_is_child(node, expected_names[i], expected_types[i]);
148        }
149        TEST_ASSERT_EQUAL_INT(i, 7);
150        reset(item);
151    }
152}
153
154static void parse_object_should_not_parse_non_objects(void)
155{
156    assert_not_object("");
157    assert_not_object("{");
158    assert_not_object("}");
159    assert_not_object("[\"hello\",{}]");
160    assert_not_object("42");
161    assert_not_object("3.14");
162    assert_not_object("\"{}hello world!\n\"");
163}
164
165int CJSON_CDECL main(void)
166{
167    /* initialize cJSON item */
168    memset(item, 0, sizeof(cJSON));
169
170    UNITY_BEGIN();
171    RUN_TEST(parse_object_should_parse_empty_objects);
172    RUN_TEST(parse_object_should_not_parse_non_objects);
173    RUN_TEST(parse_object_should_parse_objects_with_multiple_elements);
174    RUN_TEST(parse_object_should_parse_objects_with_one_element);
175    return UNITY_END();
176}
177