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