1 /* vi:set ts=8 sts=4 sw=4 noet: 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 /* JS object with white space */ 111 reader.js_buf = (char_u *)" { a : 123 } "; 112 assert(json_find_end(&reader, JSON_JS) == OK); 113 reader.js_buf = (char_u *)" { a : "; 114 assert(json_find_end(&reader, JSON_JS) == MAYBE); 115 116 /* array without white space */ 117 reader.js_buf = (char_u *)"[\"a\",123]"; 118 assert(json_find_end(&reader, 0) == OK); 119 reader.js_buf = (char_u *)"[\"a\",123"; 120 assert(json_find_end(&reader, 0) == MAYBE); 121 reader.js_buf = (char_u *)"[\"a\","; 122 assert(json_find_end(&reader, 0) == MAYBE); 123 reader.js_buf = (char_u *)"[\"a\""; 124 assert(json_find_end(&reader, 0) == MAYBE); 125 reader.js_buf = (char_u *)"[\"a"; 126 assert(json_find_end(&reader, 0) == MAYBE); 127 reader.js_buf = (char_u *)"[\""; 128 assert(json_find_end(&reader, 0) == MAYBE); 129 reader.js_buf = (char_u *)"["; 130 assert(json_find_end(&reader, 0) == MAYBE); 131 132 /* array with white space */ 133 reader.js_buf = (char_u *)" [ \"a\" , 123 ] "; 134 assert(json_find_end(&reader, 0) == OK); 135 reader.js_buf = (char_u *)" [ \"a\" , 123 "; 136 assert(json_find_end(&reader, 0) == MAYBE); 137 reader.js_buf = (char_u *)" [ \"a\" , "; 138 assert(json_find_end(&reader, 0) == MAYBE); 139 reader.js_buf = (char_u *)" [ \"a\" "; 140 assert(json_find_end(&reader, 0) == MAYBE); 141 reader.js_buf = (char_u *)" [ \"a "; 142 assert(json_find_end(&reader, 0) == MAYBE); 143 reader.js_buf = (char_u *)" [ "; 144 assert(json_find_end(&reader, 0) == MAYBE); 145 } 146 147 static int 148 fill_from_cookie(js_read_T *reader) 149 { 150 reader->js_buf = reader->js_cookie; 151 return TRUE; 152 } 153 154 /* 155 * Test json_find_end with an incomplete array, calling the fill function. 156 */ 157 static void 158 test_fill_called_on_find_end(void) 159 { 160 js_read_T reader; 161 162 reader.js_fill = fill_from_cookie; 163 reader.js_used = 0; 164 reader.js_buf = (char_u *)" [ \"a\" , 123 "; 165 reader.js_cookie = " [ \"a\" , 123 ] "; 166 assert(json_find_end(&reader, 0) == OK); 167 reader.js_buf = (char_u *)" [ \"a\" , "; 168 assert(json_find_end(&reader, 0) == OK); 169 reader.js_buf = (char_u *)" [ \"a\" "; 170 assert(json_find_end(&reader, 0) == OK); 171 reader.js_buf = (char_u *)" [ \"a"; 172 assert(json_find_end(&reader, 0) == OK); 173 reader.js_buf = (char_u *)" [ "; 174 assert(json_find_end(&reader, 0) == OK); 175 } 176 177 /* 178 * Test json_find_end with an incomplete string, calling the fill function. 179 */ 180 static void 181 test_fill_called_on_string(void) 182 { 183 js_read_T reader; 184 185 reader.js_fill = fill_from_cookie; 186 reader.js_used = 0; 187 reader.js_buf = (char_u *)" \"foo"; 188 reader.js_end = reader.js_buf + STRLEN(reader.js_buf); 189 reader.js_cookie = " \"foobar\" "; 190 assert(json_decode_string(&reader, NULL, '"') == OK); 191 } 192 #endif 193 194 int 195 main(void) 196 { 197 #if defined(FEAT_EVAL) 198 test_decode_find_end(); 199 test_fill_called_on_find_end(); 200 test_fill_called_on_string(); 201 #endif 202 return 0; 203 } 204