1 /**
2 * Copyright (c) 2013, Timothy Stack
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of Timothy Stack nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @file test_yajlpp.cc
30 */
31
32 #include "config.h"
33
34 #include <stdio.h>
35 #include <assert.h>
36
37 #include "yajlpp/yajlpp.hh"
38 #include "yajlpp/yajlpp_def.hh"
39
40 const char *TEST_DATA = R"([{ "foo": 0 }, 2, { "foo": 1 }])";
41
42 const char *TEST_OBJ_DATA = "{ \"foo\": 0 }";
43
44 static int FOO_COUNT = 0;
45 static int CONST_COUNT = 0;
46
read_foo(yajlpp_parse_context * ypc,long long value)47 static int read_foo(yajlpp_parse_context *ypc, long long value)
48 {
49 assert(value == FOO_COUNT);
50 assert(ypc->ypc_array_index.empty() ||
51 ypc->ypc_array_index.back() == FOO_COUNT);
52
53 FOO_COUNT += 1;
54
55 return 1;
56 }
57
read_const(yajlpp_parse_context * ypc,long long value)58 static int read_const(yajlpp_parse_context *ypc, long long value)
59 {
60 CONST_COUNT += 1;
61
62 return 1;
63 }
64
main(int argc,char * argv[])65 int main(int argc, char *argv[])
66 {
67 struct json_path_container test_obj_handler = {
68 json_path_handler("foo", read_foo)
69 };
70
71 {
72 struct json_path_container test_array_handlers = {
73 json_path_handler("#")
74 .add_cb(read_const)
75 .with_children(test_obj_handler)
76 };
77
78 yajlpp_parse_context ypc("test_data", &test_array_handlers);
79 auto handle = yajl_alloc(&ypc.ypc_callbacks, nullptr, &ypc);
80 ypc.with_handle(handle);
81 ypc.parse((const unsigned char *) TEST_DATA, strlen(TEST_DATA));
82 yajl_free(handle);
83
84 assert(FOO_COUNT == 2);
85 assert(CONST_COUNT == 1);
86 }
87
88 {
89 FOO_COUNT = 0;
90
91 yajlpp_parse_context ypc("test_data", &test_obj_handler);
92 auto handle = yajl_alloc(&ypc.ypc_callbacks, nullptr, &ypc);
93 ypc.with_handle(handle);
94
95 ypc.parse(reinterpret_cast<const unsigned char *>(TEST_OBJ_DATA),
96 strlen(TEST_OBJ_DATA));
97 yajl_free(handle);
98
99 assert(FOO_COUNT == 1);
100 }
101 }
102