parse_array.c 4.7 KB
Newer Older
M
Max Bruckner 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
  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"
29
#include "common.h"
M
Max Bruckner 已提交
30 31 32 33 34

static cJSON item[1];

static const unsigned char *error_pointer = NULL;

M
Max Bruckner 已提交
35
static void assert_is_array(cJSON *array_item)
M
Max Bruckner 已提交
36
{
M
Max Bruckner 已提交
37 38 39 40 41 42 43 44
    TEST_ASSERT_NOT_NULL_MESSAGE(array_item, "Item is NULL.");

    assert_not_in_list(array_item);
    assert_has_type(array_item, cJSON_Array);
    assert_has_no_reference(array_item);
    assert_has_no_const_string(array_item);
    assert_has_no_valuestring(array_item);
    assert_has_no_string(array_item);
M
Max Bruckner 已提交
45 46 47 48
}

static void assert_not_array(const char *json)
{
49
    TEST_ASSERT_NULL(parse_array(item, (const unsigned char*)json, &error_pointer, &global_hooks));
50
    assert_is_invalid(item);
M
Max Bruckner 已提交
51 52 53 54
}

static void assert_parse_array(const char *json)
{
55
    TEST_ASSERT_NOT_NULL(parse_array(item, (const unsigned char*)json, &error_pointer, &global_hooks));
M
Max Bruckner 已提交
56 57 58 59 60 61
    assert_is_array(item);
}

static void parse_array_should_parse_empty_arrays(void)
{
    assert_parse_array("[]");
M
Max Bruckner 已提交
62 63
    assert_has_no_child(item);

M
Max Bruckner 已提交
64
    assert_parse_array("[\n\t]");
M
Max Bruckner 已提交
65
    assert_has_no_child(item);
M
Max Bruckner 已提交
66 67 68 69 70 71 72
}


static void parse_array_should_parse_arrays_with_one_element(void)
{

    assert_parse_array("[1]");
M
Max Bruckner 已提交
73 74
    assert_has_child(item);
    assert_has_type(item->child, cJSON_Number);
75
    reset(item);
M
Max Bruckner 已提交
76 77

    assert_parse_array("[\"hello!\"]");
M
Max Bruckner 已提交
78 79
    assert_has_child(item);
    assert_has_type(item->child, cJSON_String);
M
Max Bruckner 已提交
80
    TEST_ASSERT_EQUAL_STRING("hello!", item->child->valuestring);
81
    reset(item);
M
Max Bruckner 已提交
82 83

    assert_parse_array("[[]]");
M
Max Bruckner 已提交
84
    assert_has_child(item);
M
Max Bruckner 已提交
85
    assert_is_array(item->child);
M
Max Bruckner 已提交
86
    assert_has_no_child(item->child);
87
    reset(item);
M
Max Bruckner 已提交
88 89

    assert_parse_array("[null]");
M
Max Bruckner 已提交
90 91
    assert_has_child(item);
    assert_has_type(item->child, cJSON_NULL);
92
    reset(item);
M
Max Bruckner 已提交
93 94 95 96 97
}

static void parse_array_should_parse_arrays_with_multiple_elements(void)
{
    assert_parse_array("[1\t,\n2, 3]");
M
Max Bruckner 已提交
98
    assert_has_child(item);
M
Max Bruckner 已提交
99 100 101
    TEST_ASSERT_NOT_NULL(item->child->next);
    TEST_ASSERT_NOT_NULL(item->child->next->next);
    TEST_ASSERT_NULL(item->child->next->next->next);
M
Max Bruckner 已提交
102 103 104
    assert_has_type(item->child, cJSON_Number);
    assert_has_type(item->child->next, cJSON_Number);
    assert_has_type(item->child->next->next, cJSON_Number);
105
    reset(item);
M
Max Bruckner 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

    {
        size_t i = 0;
        cJSON *node = NULL;
        int expected_types[7] =
        {
            cJSON_Number,
            cJSON_NULL,
            cJSON_True,
            cJSON_False,
            cJSON_Array,
            cJSON_String,
            cJSON_Object
        };
        assert_parse_array("[1, null, true, false, [], \"hello\", {}]");

        node = item->child;
        for (
                i = 0;
                (i < (sizeof(expected_types)/sizeof(int)))
                && (node != NULL);
                i++, node = node->next)
        {
            TEST_ASSERT_BITS(0xFF, expected_types[i], node->type);
        }
        TEST_ASSERT_EQUAL_INT(i, 7);
132
        reset(item);
M
Max Bruckner 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    }
}

static void parse_array_should_not_parse_non_arrays(void)
{
    assert_not_array("");
    assert_not_array("[");
    assert_not_array("]");
    assert_not_array("{\"hello\":[]}");
    assert_not_array("42");
    assert_not_array("3.14");
    assert_not_array("\"[]hello world!\n\"");
}

int main(void)
{
    /* initialize cJSON item */
    memset(item, 0, sizeof(cJSON));

    UNITY_BEGIN();
    RUN_TEST(parse_array_should_parse_empty_arrays);
    RUN_TEST(parse_array_should_parse_arrays_with_one_element);
    RUN_TEST(parse_array_should_parse_arrays_with_multiple_elements);
    RUN_TEST(parse_array_should_not_parse_non_arrays);
    return UNITY_END();
}