blob: f50c95608b34f4e74907668ea3a7b788187af2d0 [file] [log] [blame]
Bram Moolenaar56ead342016-02-02 18:20:08 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * json_test.c: Unittests for json.c
12 */
13
14#undef NDEBUG
15#include <assert.h>
16
17/* Must include main.c because it contains much more than just main() */
18#define NO_VIM_MAIN
19#include "main.c"
20
21/* This file has to be included because the tested functions are static */
22#include "json.c"
23
Bram Moolenaar8d8c5092016-02-02 19:15:38 +010024#if defined(FEAT_EVAL)
Bram Moolenaar56ead342016-02-02 18:20:08 +010025/*
26 * Test json_find_end() with imcomplete items.
27 */
28 static void
29test_decode_find_end(void)
30{
31 js_read_T reader;
32
33 reader.js_fill = NULL;
34 reader.js_used = 0;
35
36 /* string and incomplete string */
37 reader.js_buf = (char_u *)"\"hello\"";
38 assert(json_find_end(&reader) == OK);
39 reader.js_buf = (char_u *)" \"hello\" ";
40 assert(json_find_end(&reader) == OK);
41 reader.js_buf = (char_u *)"\"hello";
42 assert(json_find_end(&reader) == MAYBE);
43
44 /* number and dash (incomplete number) */
45 reader.js_buf = (char_u *)"123";
46 assert(json_find_end(&reader) == OK);
47 reader.js_buf = (char_u *)"-";
48 assert(json_find_end(&reader) == MAYBE);
49
50 /* false, true and null, also incomplete */
51 reader.js_buf = (char_u *)"false";
52 assert(json_find_end(&reader) == OK);
53 reader.js_buf = (char_u *)"f";
54 assert(json_find_end(&reader) == MAYBE);
55 reader.js_buf = (char_u *)"fa";
56 assert(json_find_end(&reader) == MAYBE);
57 reader.js_buf = (char_u *)"fal";
58 assert(json_find_end(&reader) == MAYBE);
59 reader.js_buf = (char_u *)"fals";
60 assert(json_find_end(&reader) == MAYBE);
61
62 reader.js_buf = (char_u *)"true";
63 assert(json_find_end(&reader) == OK);
64 reader.js_buf = (char_u *)"t";
65 assert(json_find_end(&reader) == MAYBE);
66 reader.js_buf = (char_u *)"tr";
67 assert(json_find_end(&reader) == MAYBE);
68 reader.js_buf = (char_u *)"tru";
69 assert(json_find_end(&reader) == MAYBE);
70
71 reader.js_buf = (char_u *)"null";
72 assert(json_find_end(&reader) == OK);
73 reader.js_buf = (char_u *)"n";
74 assert(json_find_end(&reader) == MAYBE);
75 reader.js_buf = (char_u *)"nu";
76 assert(json_find_end(&reader) == MAYBE);
77 reader.js_buf = (char_u *)"nul";
78 assert(json_find_end(&reader) == MAYBE);
79
80 /* object without white space */
81 reader.js_buf = (char_u *)"{\"a\":123}";
82 assert(json_find_end(&reader) == OK);
83 reader.js_buf = (char_u *)"{\"a\":123";
84 assert(json_find_end(&reader) == MAYBE);
85 reader.js_buf = (char_u *)"{\"a\":";
86 assert(json_find_end(&reader) == MAYBE);
87 reader.js_buf = (char_u *)"{\"a\"";
88 assert(json_find_end(&reader) == MAYBE);
89 reader.js_buf = (char_u *)"{\"a";
90 assert(json_find_end(&reader) == MAYBE);
91 reader.js_buf = (char_u *)"{\"";
92 assert(json_find_end(&reader) == MAYBE);
93 reader.js_buf = (char_u *)"{";
94 assert(json_find_end(&reader) == MAYBE);
95
96 /* object with white space */
97 reader.js_buf = (char_u *)" { \"a\" : 123 } ";
98 assert(json_find_end(&reader) == OK);
99 reader.js_buf = (char_u *)" { \"a\" : 123 ";
100 assert(json_find_end(&reader) == MAYBE);
101 reader.js_buf = (char_u *)" { \"a\" : ";
102 assert(json_find_end(&reader) == MAYBE);
103 reader.js_buf = (char_u *)" { \"a\" ";
104 assert(json_find_end(&reader) == MAYBE);
105 reader.js_buf = (char_u *)" { \"a ";
106 assert(json_find_end(&reader) == MAYBE);
107 reader.js_buf = (char_u *)" { ";
108 assert(json_find_end(&reader) == MAYBE);
109
110 /* array without white space */
111 reader.js_buf = (char_u *)"[\"a\",123]";
112 assert(json_find_end(&reader) == OK);
113 reader.js_buf = (char_u *)"[\"a\",123";
114 assert(json_find_end(&reader) == MAYBE);
115 reader.js_buf = (char_u *)"[\"a\",";
116 assert(json_find_end(&reader) == MAYBE);
117 reader.js_buf = (char_u *)"[\"a\"";
118 assert(json_find_end(&reader) == MAYBE);
119 reader.js_buf = (char_u *)"[\"a";
120 assert(json_find_end(&reader) == MAYBE);
121 reader.js_buf = (char_u *)"[\"";
122 assert(json_find_end(&reader) == MAYBE);
123 reader.js_buf = (char_u *)"[";
124 assert(json_find_end(&reader) == MAYBE);
125
126 /* array with white space */
127 reader.js_buf = (char_u *)" [ \"a\" , 123 ] ";
128 assert(json_find_end(&reader) == OK);
129 reader.js_buf = (char_u *)" [ \"a\" , 123 ";
130 assert(json_find_end(&reader) == MAYBE);
131 reader.js_buf = (char_u *)" [ \"a\" , ";
132 assert(json_find_end(&reader) == MAYBE);
133 reader.js_buf = (char_u *)" [ \"a\" ";
134 assert(json_find_end(&reader) == MAYBE);
135 reader.js_buf = (char_u *)" [ \"a ";
136 assert(json_find_end(&reader) == MAYBE);
137 reader.js_buf = (char_u *)" [ ";
138 assert(json_find_end(&reader) == MAYBE);
139}
140
141 static int
142fill_from_cookie(js_read_T *reader)
143{
144 reader->js_buf = reader->js_cookie;
145 return TRUE;
146}
147
148/*
149 * Test json_find_end with an incomplete array, calling the fill function.
150 */
151 static void
152test_fill_called_on_find_end(void)
153{
154 js_read_T reader;
155
156 reader.js_fill = fill_from_cookie;
157 reader.js_used = 0;
158 reader.js_buf = (char_u *)" [ \"a\" , 123 ";
159 reader.js_cookie = " [ \"a\" , 123 ] ";
160 assert(json_find_end(&reader) == OK);
161 reader.js_buf = (char_u *)" [ \"a\" , ";
162 assert(json_find_end(&reader) == OK);
163 reader.js_buf = (char_u *)" [ \"a\" ";
164 assert(json_find_end(&reader) == OK);
165 reader.js_buf = (char_u *)" [ \"a";
166 assert(json_find_end(&reader) == OK);
167 reader.js_buf = (char_u *)" [ ";
168 assert(json_find_end(&reader) == OK);
169}
170
171/*
172 * Test json_find_end with an incomplete string, calling the fill function.
173 */
174 static void
175test_fill_called_on_string(void)
176{
177 js_read_T reader;
178
179 reader.js_fill = fill_from_cookie;
180 reader.js_used = 0;
181 reader.js_buf = (char_u *)" \"foo";
182 reader.js_end = reader.js_buf + STRLEN(reader.js_buf);
183 reader.js_cookie = " \"foobar\" ";
184 assert(json_decode_string(&reader, NULL) == OK);
185}
Bram Moolenaar8d8c5092016-02-02 19:15:38 +0100186#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100187
188 int
189main(void)
190{
Bram Moolenaar8d8c5092016-02-02 19:15:38 +0100191#if defined(FEAT_EVAL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100192 test_decode_find_end();
193 test_fill_called_on_find_end();
194 test_fill_called_on_string();
Bram Moolenaar8d8c5092016-02-02 19:15:38 +0100195#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100196 return 0;
197}