Test files for libxml2 output
[yaz-moved-to-github.git] / test / tst_json.c
1 /**
2  * \file 
3  * \brief JSON test
4  */
5 #include <yaz/test.h>
6 #include <yaz/json.h>
7 #include <string.h>
8 #include <yaz/log.h>
9
10 static int expect(json_parser_t p, const char *input, 
11                   const char *output)
12 {
13     int ret = 0;
14     struct json_node *n;
15
16     n = json_parser_parse(p, input);
17     if (n == 0 && output == 0)
18         ret = 1;
19     else if (n && output)
20     {
21         WRBUF result = wrbuf_alloc();
22
23         json_write_wrbuf(n, result);
24         if (strcmp(wrbuf_cstr(result), output) == 0)
25             ret = 1;
26         else
27         {
28             yaz_log(YLOG_WARN, "expected '%s' but got '%s'",
29                     output, wrbuf_cstr(result));
30         }
31         wrbuf_destroy(result);
32     }
33     else if (!n)
34     {
35         yaz_log(YLOG_WARN, "expected '%s' but got error '%s'",
36                 output, json_parser_get_errmsg(p));
37     }
38     json_remove_node(n);
39     return ret;
40 }
41
42 static void tst1(void)
43 {
44     json_parser_t p = json_parser_create();
45
46     YAZ_CHECK(p);
47     if (!p)
48         return;
49
50     YAZ_CHECK(expect(p, "", 0));
51
52     YAZ_CHECK(expect(p, "1234", "1234"));
53
54     YAZ_CHECK(expect(p, "[ 1234 ]", "[1234]"));
55
56     YAZ_CHECK(expect(p, "{\"k\":tru}", 0));
57
58     YAZ_CHECK(expect(p, "{\"k\":null", 0));
59
60     YAZ_CHECK(expect(p, "{\"k\":nullx}", 0));
61
62     YAZ_CHECK(expect(p, "{\"k\":-", 0));
63
64     YAZ_CHECK(expect(p, "{\"k\":+", 0));
65
66     YAZ_CHECK(expect(p, "{\"k\":\"a}", 0));
67
68     YAZ_CHECK(expect(p, "{\"k\":\"a", 0));
69
70     YAZ_CHECK(expect(p, "{\"k\":\"", 0));
71
72     YAZ_CHECK(expect(p, "{", 0));
73
74     YAZ_CHECK(expect(p, "{}", "{}"));
75
76     YAZ_CHECK(expect(p, "{}  extra", 0));
77
78     YAZ_CHECK(expect(p, "{\"a\":[1,2,3}", 0));
79     
80     YAZ_CHECK(expect(p, "{\"a\":[1,2,", 0));
81
82     YAZ_CHECK(expect(p, "{\"k\":\"wa\"}", "{\"k\":\"wa\"}"));
83
84     YAZ_CHECK(expect(p, "{\"k\":null}", "{\"k\":null}"));
85
86     YAZ_CHECK(expect(p, "{\"k\":false}", "{\"k\":false}"));
87
88     YAZ_CHECK(expect(p, "{\"k\":true}", "{\"k\":true}"));
89
90     YAZ_CHECK(expect(p, "{\"k\":12}", "{\"k\":12}"));
91
92     YAZ_CHECK(expect(p, "{\"k\":-12}", "{\"k\":-12}"));
93
94     YAZ_CHECK(expect(p, "{\"k\":1.2e6}", "{\"k\":1.2e+06}"));
95
96     YAZ_CHECK(expect(p, "{\"k\":1e3}", "{\"k\":1000}"));
97
98     YAZ_CHECK(expect(p, "{\"k\":\"\"}", "{\"k\":\"\"}"));
99
100     YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":2}"));
101
102     YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2,\"c\":3}",
103                      "{\"a\":1,\"b\":2,\"c\":3}"));
104
105     YAZ_CHECK(expect(p, "{\"a\":[]}", "{\"a\":[]}"));
106
107     YAZ_CHECK(expect(p, "{\"a\":[1]}", "{\"a\":[1]}"));
108
109     YAZ_CHECK(expect(p, "{\"a\":[1,2]}", "{\"a\":[1,2]}"));
110
111     YAZ_CHECK(expect(p, "{\"a\":[1,2,3]}", "{\"a\":[1,2,3]}"));
112
113     YAZ_CHECK(expect(p, "{\"k\":\"\\t\"}", "{\"k\":\"\x09\"}"));
114
115     YAZ_CHECK(expect(p, "{\"k\":\"\\u0009\"}", "{\"k\":\"\x09\"}"));
116
117     json_parser_destroy(p);
118 }
119
120 static void tst2(void)
121 {
122     struct json_node *n, *n1;
123
124     n = json_parse("{\"a\":1,\"b\":2,\"c\":[true,false,null]}", 0);
125     YAZ_CHECK(n);
126     if (!n)
127         return;
128
129     YAZ_CHECK_EQ(json_count_children(n), 3);
130     
131     n1 = json_get_object(n, "a");
132     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 1.0);
133     YAZ_CHECK_EQ(json_count_children(n1), 0);
134
135     n1 = json_get_object(n, "b");
136     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
137     YAZ_CHECK_EQ(json_count_children(n1), 0);
138
139     n1 = json_get_object(n, "b");
140     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
141     YAZ_CHECK_EQ(json_count_children(n1), 0);
142
143     n1 = json_get_object(n, "c");
144     YAZ_CHECK(n1 && n1->type == json_node_array);
145     YAZ_CHECK_EQ(json_count_children(n1), 3);
146
147     n1 = json_get_elem(json_get_object(n, "c"), 0);
148     YAZ_CHECK(n1 && n1->type == json_node_true);
149
150     n1 = json_get_elem(json_get_object(n, "c"), 1);
151     YAZ_CHECK(n1 && n1->type == json_node_false);
152
153     n1 = json_get_elem(json_get_object(n, "c"), 2);
154     YAZ_CHECK(n1 && n1->type == json_node_null);
155
156     n1 = json_get_elem(json_get_object(n, "c"), 3);
157     YAZ_CHECK(n1 == 0);
158
159     json_remove_node(n);
160 }
161
162 static int append_check(const char *a, const char *b, const char *exp)
163 {
164     WRBUF w = wrbuf_alloc();
165     struct json_node *n_a, *n_b;
166     int ret = 0;
167
168     n_a = json_parse(a, 0);
169     n_b = json_parse(b, 0);
170     json_append_array(json_get_object(n_a, "a"),
171                       json_detach_object(n_b, "b"));
172
173     json_write_wrbuf(n_a, w);
174
175     if (!strcmp(wrbuf_cstr(w), exp))
176         ret = 1;
177     wrbuf_destroy(w);
178     json_remove_node(n_a);
179     json_remove_node(n_b);
180     return ret;
181 }
182
183 static void tst3(void)
184 {
185     YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[5,6,7]}",
186                            "{\"a\":[1,2,3,5,6,7]}"));
187
188     YAZ_CHECK(append_check("{\"a\":[]}", "{\"b\":[5,6,7]}",
189                            "{\"a\":[5,6,7]}"));
190
191     YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[]}",
192                            "{\"a\":[1,2,3]}"));
193 }
194
195 int main (int argc, char **argv)
196 {
197     YAZ_CHECK_INIT(argc, argv);
198     tst1();
199     tst2();
200     tst3();
201     YAZ_CHECK_TERM;
202 }
203
204 /*
205  * Local variables:
206  * c-basic-offset: 4
207  * c-file-style: "Stroustrup"
208  * indent-tabs-mode: nil
209  * End:
210  * vim: shiftwidth=4 tabstop=8 expandtab
211  */
212
213