comparison src/json_test.c @ 7967:45ea5ebf3a98 v7.4.1279

commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a Author: Bram Moolenaar <Bram@vim.org> Date: Sun Feb 7 19:19:53 2016 +0100 patch 7.4.1279 Problem: jsonencode() is not producing strict JSON. Solution: Add jsencode() and jsdecode(). Make jsonencode() and jsondecode() strict.
author Christian Brabandt <cb@256bit.org>
date Sun, 07 Feb 2016 19:30:05 +0100
parents d14cf20b44dc
children 4aead6a9b7a9
comparison
equal deleted inserted replaced
7966:79c5a86fcdfe 7967:45ea5ebf3a98
33 reader.js_fill = NULL; 33 reader.js_fill = NULL;
34 reader.js_used = 0; 34 reader.js_used = 0;
35 35
36 /* string and incomplete string */ 36 /* string and incomplete string */
37 reader.js_buf = (char_u *)"\"hello\""; 37 reader.js_buf = (char_u *)"\"hello\"";
38 assert(json_find_end(&reader) == OK); 38 assert(json_find_end(&reader, 0) == OK);
39 reader.js_buf = (char_u *)" \"hello\" "; 39 reader.js_buf = (char_u *)" \"hello\" ";
40 assert(json_find_end(&reader) == OK); 40 assert(json_find_end(&reader, 0) == OK);
41 reader.js_buf = (char_u *)"\"hello"; 41 reader.js_buf = (char_u *)"\"hello";
42 assert(json_find_end(&reader) == MAYBE); 42 assert(json_find_end(&reader, 0) == MAYBE);
43 43
44 /* number and dash (incomplete number) */ 44 /* number and dash (incomplete number) */
45 reader.js_buf = (char_u *)"123"; 45 reader.js_buf = (char_u *)"123";
46 assert(json_find_end(&reader) == OK); 46 assert(json_find_end(&reader, 0) == OK);
47 reader.js_buf = (char_u *)"-"; 47 reader.js_buf = (char_u *)"-";
48 assert(json_find_end(&reader) == MAYBE); 48 assert(json_find_end(&reader, 0) == MAYBE);
49 49
50 /* false, true and null, also incomplete */ 50 /* false, true and null, also incomplete */
51 reader.js_buf = (char_u *)"false"; 51 reader.js_buf = (char_u *)"false";
52 assert(json_find_end(&reader) == OK); 52 assert(json_find_end(&reader, 0) == OK);
53 reader.js_buf = (char_u *)"f"; 53 reader.js_buf = (char_u *)"f";
54 assert(json_find_end(&reader) == MAYBE); 54 assert(json_find_end(&reader, 0) == MAYBE);
55 reader.js_buf = (char_u *)"fa"; 55 reader.js_buf = (char_u *)"fa";
56 assert(json_find_end(&reader) == MAYBE); 56 assert(json_find_end(&reader, 0) == MAYBE);
57 reader.js_buf = (char_u *)"fal"; 57 reader.js_buf = (char_u *)"fal";
58 assert(json_find_end(&reader) == MAYBE); 58 assert(json_find_end(&reader, 0) == MAYBE);
59 reader.js_buf = (char_u *)"fals"; 59 reader.js_buf = (char_u *)"fals";
60 assert(json_find_end(&reader) == MAYBE); 60 assert(json_find_end(&reader, 0) == MAYBE);
61 61
62 reader.js_buf = (char_u *)"true"; 62 reader.js_buf = (char_u *)"true";
63 assert(json_find_end(&reader) == OK); 63 assert(json_find_end(&reader, 0) == OK);
64 reader.js_buf = (char_u *)"t"; 64 reader.js_buf = (char_u *)"t";
65 assert(json_find_end(&reader) == MAYBE); 65 assert(json_find_end(&reader, 0) == MAYBE);
66 reader.js_buf = (char_u *)"tr"; 66 reader.js_buf = (char_u *)"tr";
67 assert(json_find_end(&reader) == MAYBE); 67 assert(json_find_end(&reader, 0) == MAYBE);
68 reader.js_buf = (char_u *)"tru"; 68 reader.js_buf = (char_u *)"tru";
69 assert(json_find_end(&reader) == MAYBE); 69 assert(json_find_end(&reader, 0) == MAYBE);
70 70
71 reader.js_buf = (char_u *)"null"; 71 reader.js_buf = (char_u *)"null";
72 assert(json_find_end(&reader) == OK); 72 assert(json_find_end(&reader, 0) == OK);
73 reader.js_buf = (char_u *)"n"; 73 reader.js_buf = (char_u *)"n";
74 assert(json_find_end(&reader) == MAYBE); 74 assert(json_find_end(&reader, 0) == MAYBE);
75 reader.js_buf = (char_u *)"nu"; 75 reader.js_buf = (char_u *)"nu";
76 assert(json_find_end(&reader) == MAYBE); 76 assert(json_find_end(&reader, 0) == MAYBE);
77 reader.js_buf = (char_u *)"nul"; 77 reader.js_buf = (char_u *)"nul";
78 assert(json_find_end(&reader) == MAYBE); 78 assert(json_find_end(&reader, 0) == MAYBE);
79 79
80 /* object without white space */ 80 /* object without white space */
81 reader.js_buf = (char_u *)"{\"a\":123}"; 81 reader.js_buf = (char_u *)"{\"a\":123}";
82 assert(json_find_end(&reader) == OK); 82 assert(json_find_end(&reader, 0) == OK);
83 reader.js_buf = (char_u *)"{\"a\":123"; 83 reader.js_buf = (char_u *)"{\"a\":123";
84 assert(json_find_end(&reader) == MAYBE); 84 assert(json_find_end(&reader, 0) == MAYBE);
85 reader.js_buf = (char_u *)"{\"a\":"; 85 reader.js_buf = (char_u *)"{\"a\":";
86 assert(json_find_end(&reader) == MAYBE); 86 assert(json_find_end(&reader, 0) == MAYBE);
87 reader.js_buf = (char_u *)"{\"a\""; 87 reader.js_buf = (char_u *)"{\"a\"";
88 assert(json_find_end(&reader) == MAYBE); 88 assert(json_find_end(&reader, 0) == MAYBE);
89 reader.js_buf = (char_u *)"{\"a"; 89 reader.js_buf = (char_u *)"{\"a";
90 assert(json_find_end(&reader) == MAYBE); 90 assert(json_find_end(&reader, 0) == MAYBE);
91 reader.js_buf = (char_u *)"{\""; 91 reader.js_buf = (char_u *)"{\"";
92 assert(json_find_end(&reader) == MAYBE); 92 assert(json_find_end(&reader, 0) == MAYBE);
93 reader.js_buf = (char_u *)"{"; 93 reader.js_buf = (char_u *)"{";
94 assert(json_find_end(&reader) == MAYBE); 94 assert(json_find_end(&reader, 0) == MAYBE);
95 95
96 /* object with white space */ 96 /* object with white space */
97 reader.js_buf = (char_u *)" { \"a\" : 123 } "; 97 reader.js_buf = (char_u *)" { \"a\" : 123 } ";
98 assert(json_find_end(&reader) == OK); 98 assert(json_find_end(&reader, 0) == OK);
99 reader.js_buf = (char_u *)" { \"a\" : 123 "; 99 reader.js_buf = (char_u *)" { \"a\" : 123 ";
100 assert(json_find_end(&reader) == MAYBE); 100 assert(json_find_end(&reader, 0) == MAYBE);
101 reader.js_buf = (char_u *)" { \"a\" : "; 101 reader.js_buf = (char_u *)" { \"a\" : ";
102 assert(json_find_end(&reader) == MAYBE); 102 assert(json_find_end(&reader, 0) == MAYBE);
103 reader.js_buf = (char_u *)" { \"a\" "; 103 reader.js_buf = (char_u *)" { \"a\" ";
104 assert(json_find_end(&reader) == MAYBE); 104 assert(json_find_end(&reader, 0) == MAYBE);
105 reader.js_buf = (char_u *)" { \"a "; 105 reader.js_buf = (char_u *)" { \"a ";
106 assert(json_find_end(&reader) == MAYBE); 106 assert(json_find_end(&reader, 0) == MAYBE);
107 reader.js_buf = (char_u *)" { "; 107 reader.js_buf = (char_u *)" { ";
108 assert(json_find_end(&reader) == MAYBE); 108 assert(json_find_end(&reader, 0) == MAYBE);
109 109
110 /* array without white space */ 110 /* array without white space */
111 reader.js_buf = (char_u *)"[\"a\",123]"; 111 reader.js_buf = (char_u *)"[\"a\",123]";
112 assert(json_find_end(&reader) == OK); 112 assert(json_find_end(&reader, 0) == OK);
113 reader.js_buf = (char_u *)"[\"a\",123"; 113 reader.js_buf = (char_u *)"[\"a\",123";
114 assert(json_find_end(&reader) == MAYBE); 114 assert(json_find_end(&reader, 0) == MAYBE);
115 reader.js_buf = (char_u *)"[\"a\","; 115 reader.js_buf = (char_u *)"[\"a\",";
116 assert(json_find_end(&reader) == MAYBE); 116 assert(json_find_end(&reader, 0) == MAYBE);
117 reader.js_buf = (char_u *)"[\"a\""; 117 reader.js_buf = (char_u *)"[\"a\"";
118 assert(json_find_end(&reader) == MAYBE); 118 assert(json_find_end(&reader, 0) == MAYBE);
119 reader.js_buf = (char_u *)"[\"a"; 119 reader.js_buf = (char_u *)"[\"a";
120 assert(json_find_end(&reader) == MAYBE); 120 assert(json_find_end(&reader, 0) == MAYBE);
121 reader.js_buf = (char_u *)"[\""; 121 reader.js_buf = (char_u *)"[\"";
122 assert(json_find_end(&reader) == MAYBE); 122 assert(json_find_end(&reader, 0) == MAYBE);
123 reader.js_buf = (char_u *)"["; 123 reader.js_buf = (char_u *)"[";
124 assert(json_find_end(&reader) == MAYBE); 124 assert(json_find_end(&reader, 0) == MAYBE);
125 125
126 /* array with white space */ 126 /* array with white space */
127 reader.js_buf = (char_u *)" [ \"a\" , 123 ] "; 127 reader.js_buf = (char_u *)" [ \"a\" , 123 ] ";
128 assert(json_find_end(&reader) == OK); 128 assert(json_find_end(&reader, 0) == OK);
129 reader.js_buf = (char_u *)" [ \"a\" , 123 "; 129 reader.js_buf = (char_u *)" [ \"a\" , 123 ";
130 assert(json_find_end(&reader) == MAYBE); 130 assert(json_find_end(&reader, 0) == MAYBE);
131 reader.js_buf = (char_u *)" [ \"a\" , "; 131 reader.js_buf = (char_u *)" [ \"a\" , ";
132 assert(json_find_end(&reader) == MAYBE); 132 assert(json_find_end(&reader, 0) == MAYBE);
133 reader.js_buf = (char_u *)" [ \"a\" "; 133 reader.js_buf = (char_u *)" [ \"a\" ";
134 assert(json_find_end(&reader) == MAYBE); 134 assert(json_find_end(&reader, 0) == MAYBE);
135 reader.js_buf = (char_u *)" [ \"a "; 135 reader.js_buf = (char_u *)" [ \"a ";
136 assert(json_find_end(&reader) == MAYBE); 136 assert(json_find_end(&reader, 0) == MAYBE);
137 reader.js_buf = (char_u *)" [ "; 137 reader.js_buf = (char_u *)" [ ";
138 assert(json_find_end(&reader) == MAYBE); 138 assert(json_find_end(&reader, 0) == MAYBE);
139 } 139 }
140 140
141 static int 141 static int
142 fill_from_cookie(js_read_T *reader) 142 fill_from_cookie(js_read_T *reader)
143 { 143 {
155 155
156 reader.js_fill = fill_from_cookie; 156 reader.js_fill = fill_from_cookie;
157 reader.js_used = 0; 157 reader.js_used = 0;
158 reader.js_buf = (char_u *)" [ \"a\" , 123 "; 158 reader.js_buf = (char_u *)" [ \"a\" , 123 ";
159 reader.js_cookie = " [ \"a\" , 123 ] "; 159 reader.js_cookie = " [ \"a\" , 123 ] ";
160 assert(json_find_end(&reader) == OK); 160 assert(json_find_end(&reader, 0) == OK);
161 reader.js_buf = (char_u *)" [ \"a\" , "; 161 reader.js_buf = (char_u *)" [ \"a\" , ";
162 assert(json_find_end(&reader) == OK); 162 assert(json_find_end(&reader, 0) == OK);
163 reader.js_buf = (char_u *)" [ \"a\" "; 163 reader.js_buf = (char_u *)" [ \"a\" ";
164 assert(json_find_end(&reader) == OK); 164 assert(json_find_end(&reader, 0) == OK);
165 reader.js_buf = (char_u *)" [ \"a"; 165 reader.js_buf = (char_u *)" [ \"a";
166 assert(json_find_end(&reader) == OK); 166 assert(json_find_end(&reader, 0) == OK);
167 reader.js_buf = (char_u *)" [ "; 167 reader.js_buf = (char_u *)" [ ";
168 assert(json_find_end(&reader) == OK); 168 assert(json_find_end(&reader, 0) == OK);
169 } 169 }
170 170
171 /* 171 /*
172 * Test json_find_end with an incomplete string, calling the fill function. 172 * Test json_find_end with an incomplete string, calling the fill function.
173 */ 173 */