xref: /vim-8.2.3635/src/json_test.c (revision cb03397a)
1 /* 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 
24 #if defined(FEAT_EVAL)
25 /*
26  * Test json_find_end() with imcomplete items.
27  */
28     static void
29 test_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, 0) == OK);
39     reader.js_buf = (char_u *)"  \"hello\" ";
40     assert(json_find_end(&reader, 0) == OK);
41     reader.js_buf = (char_u *)"\"hello";
42     assert(json_find_end(&reader, 0) == MAYBE);
43 
44     /* number and dash (incomplete number) */
45     reader.js_buf = (char_u *)"123";
46     assert(json_find_end(&reader, 0) == OK);
47     reader.js_buf = (char_u *)"-";
48     assert(json_find_end(&reader, 0) == MAYBE);
49 
50     /* false, true and null, also incomplete */
51     reader.js_buf = (char_u *)"false";
52     assert(json_find_end(&reader, 0) == OK);
53     reader.js_buf = (char_u *)"f";
54     assert(json_find_end(&reader, 0) == MAYBE);
55     reader.js_buf = (char_u *)"fa";
56     assert(json_find_end(&reader, 0) == MAYBE);
57     reader.js_buf = (char_u *)"fal";
58     assert(json_find_end(&reader, 0) == MAYBE);
59     reader.js_buf = (char_u *)"fals";
60     assert(json_find_end(&reader, 0) == MAYBE);
61 
62     reader.js_buf = (char_u *)"true";
63     assert(json_find_end(&reader, 0) == OK);
64     reader.js_buf = (char_u *)"t";
65     assert(json_find_end(&reader, 0) == MAYBE);
66     reader.js_buf = (char_u *)"tr";
67     assert(json_find_end(&reader, 0) == MAYBE);
68     reader.js_buf = (char_u *)"tru";
69     assert(json_find_end(&reader, 0) == MAYBE);
70 
71     reader.js_buf = (char_u *)"null";
72     assert(json_find_end(&reader, 0) == OK);
73     reader.js_buf = (char_u *)"n";
74     assert(json_find_end(&reader, 0) == MAYBE);
75     reader.js_buf = (char_u *)"nu";
76     assert(json_find_end(&reader, 0) == MAYBE);
77     reader.js_buf = (char_u *)"nul";
78     assert(json_find_end(&reader, 0) == MAYBE);
79 
80     /* object without white space */
81     reader.js_buf = (char_u *)"{\"a\":123}";
82     assert(json_find_end(&reader, 0) == OK);
83     reader.js_buf = (char_u *)"{\"a\":123";
84     assert(json_find_end(&reader, 0) == MAYBE);
85     reader.js_buf = (char_u *)"{\"a\":";
86     assert(json_find_end(&reader, 0) == MAYBE);
87     reader.js_buf = (char_u *)"{\"a\"";
88     assert(json_find_end(&reader, 0) == MAYBE);
89     reader.js_buf = (char_u *)"{\"a";
90     assert(json_find_end(&reader, 0) == MAYBE);
91     reader.js_buf = (char_u *)"{\"";
92     assert(json_find_end(&reader, 0) == MAYBE);
93     reader.js_buf = (char_u *)"{";
94     assert(json_find_end(&reader, 0) == MAYBE);
95 
96     /* object with white space */
97     reader.js_buf = (char_u *)"  {  \"a\"  :  123  }  ";
98     assert(json_find_end(&reader, 0) == OK);
99     reader.js_buf = (char_u *)"  {  \"a\"  :  123  ";
100     assert(json_find_end(&reader, 0) == MAYBE);
101     reader.js_buf = (char_u *)"  {  \"a\"  :  ";
102     assert(json_find_end(&reader, 0) == MAYBE);
103     reader.js_buf = (char_u *)"  {  \"a\"  ";
104     assert(json_find_end(&reader, 0) == MAYBE);
105     reader.js_buf = (char_u *)"  {  \"a  ";
106     assert(json_find_end(&reader, 0) == MAYBE);
107     reader.js_buf = (char_u *)"  {   ";
108     assert(json_find_end(&reader, 0) == MAYBE);
109 
110     /* array without white space */
111     reader.js_buf = (char_u *)"[\"a\",123]";
112     assert(json_find_end(&reader, 0) == OK);
113     reader.js_buf = (char_u *)"[\"a\",123";
114     assert(json_find_end(&reader, 0) == MAYBE);
115     reader.js_buf = (char_u *)"[\"a\",";
116     assert(json_find_end(&reader, 0) == MAYBE);
117     reader.js_buf = (char_u *)"[\"a\"";
118     assert(json_find_end(&reader, 0) == MAYBE);
119     reader.js_buf = (char_u *)"[\"a";
120     assert(json_find_end(&reader, 0) == MAYBE);
121     reader.js_buf = (char_u *)"[\"";
122     assert(json_find_end(&reader, 0) == MAYBE);
123     reader.js_buf = (char_u *)"[";
124     assert(json_find_end(&reader, 0) == MAYBE);
125 
126     /* array with white space */
127     reader.js_buf = (char_u *)"  [  \"a\"  ,  123  ]  ";
128     assert(json_find_end(&reader, 0) == OK);
129     reader.js_buf = (char_u *)"  [  \"a\"  ,  123  ";
130     assert(json_find_end(&reader, 0) == MAYBE);
131     reader.js_buf = (char_u *)"  [  \"a\"  ,  ";
132     assert(json_find_end(&reader, 0) == MAYBE);
133     reader.js_buf = (char_u *)"  [  \"a\"  ";
134     assert(json_find_end(&reader, 0) == MAYBE);
135     reader.js_buf = (char_u *)"  [  \"a  ";
136     assert(json_find_end(&reader, 0) == MAYBE);
137     reader.js_buf = (char_u *)"  [  ";
138     assert(json_find_end(&reader, 0) == MAYBE);
139 }
140 
141     static int
142 fill_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
152 test_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, 0) == OK);
161     reader.js_buf = (char_u *)"  [  \"a\"  ,  ";
162     assert(json_find_end(&reader, 0) == OK);
163     reader.js_buf = (char_u *)"  [  \"a\"  ";
164     assert(json_find_end(&reader, 0) == OK);
165     reader.js_buf = (char_u *)"  [  \"a";
166     assert(json_find_end(&reader, 0) == OK);
167     reader.js_buf = (char_u *)"  [  ";
168     assert(json_find_end(&reader, 0) == OK);
169 }
170 
171 /*
172  * Test json_find_end with an incomplete string, calling the fill function.
173  */
174     static void
175 test_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 }
186 #endif
187 
188     int
189 main(void)
190 {
191 #if defined(FEAT_EVAL)
192     test_decode_find_end();
193     test_fill_called_on_find_end();
194     test_fill_called_on_string();
195 #endif
196     return 0;
197 }
198