• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
assert_print_value(const char * input)31 static void assert_print_value(const char *input)
32 {
33     unsigned char printed[1024];
34     cJSON item[1];
35     printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
36     parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } };
37     buffer.buffer = printed;
38     buffer.length = sizeof(printed);
39     buffer.offset = 0;
40     buffer.noalloc = true;
41     buffer.hooks = global_hooks;
42 
43     parsebuffer.content = (const unsigned char*)input;
44     parsebuffer.length = strlen(input) + sizeof("");
45     parsebuffer.hooks = global_hooks;
46 
47     memset(item, 0, sizeof(item));
48 
49     TEST_ASSERT_TRUE_MESSAGE(parse_value(item, &parsebuffer), "Failed to parse value.");
50 
51     TEST_ASSERT_TRUE_MESSAGE(print_value(item, &buffer), "Failed to print value.");
52     TEST_ASSERT_EQUAL_STRING_MESSAGE(input, buffer.buffer, "Printed value is not as expected.");
53 
54     reset(item);
55 }
56 
print_value_should_print_null(void)57 static void print_value_should_print_null(void)
58 {
59     assert_print_value("null");
60 }
61 
print_value_should_print_true(void)62 static void print_value_should_print_true(void)
63 {
64     assert_print_value("true");
65 }
66 
print_value_should_print_false(void)67 static void print_value_should_print_false(void)
68 {
69     assert_print_value("false");
70 }
71 
print_value_should_print_number(void)72 static void print_value_should_print_number(void)
73 {
74     assert_print_value("1.5");
75 }
76 
print_value_should_print_string(void)77 static void print_value_should_print_string(void)
78 {
79     assert_print_value("\"\"");
80     assert_print_value("\"hello\"");
81 }
82 
print_value_should_print_array(void)83 static void print_value_should_print_array(void)
84 {
85     assert_print_value("[]");
86 }
87 
print_value_should_print_object(void)88 static void print_value_should_print_object(void)
89 {
90     assert_print_value("{}");
91 }
92 
main(void)93 int CJSON_CDECL main(void)
94 {
95     /* initialize cJSON item */
96     UNITY_BEGIN();
97 
98     RUN_TEST(print_value_should_print_null);
99     RUN_TEST(print_value_should_print_true);
100     RUN_TEST(print_value_should_print_false);
101     RUN_TEST(print_value_should_print_number);
102     RUN_TEST(print_value_should_print_string);
103     RUN_TEST(print_value_should_print_array);
104     RUN_TEST(print_value_should_print_object);
105 
106     return UNITY_END();
107 }
108