Bram Moolenaar | 56ead34 | 2016-02-02 18:20:08 +0100 | [diff] [blame] | 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 | /* |
| 25 | * Test json_find_end() with imcomplete items. |
| 26 | */ |
| 27 | static void |
| 28 | test_decode_find_end(void) |
| 29 | { |
| 30 | js_read_T reader; |
| 31 | |
| 32 | reader.js_fill = NULL; |
| 33 | reader.js_used = 0; |
| 34 | |
| 35 | /* string and incomplete string */ |
| 36 | reader.js_buf = (char_u *)"\"hello\""; |
| 37 | assert(json_find_end(&reader) == OK); |
| 38 | reader.js_buf = (char_u *)" \"hello\" "; |
| 39 | assert(json_find_end(&reader) == OK); |
| 40 | reader.js_buf = (char_u *)"\"hello"; |
| 41 | assert(json_find_end(&reader) == MAYBE); |
| 42 | |
| 43 | /* number and dash (incomplete number) */ |
| 44 | reader.js_buf = (char_u *)"123"; |
| 45 | assert(json_find_end(&reader) == OK); |
| 46 | reader.js_buf = (char_u *)"-"; |
| 47 | assert(json_find_end(&reader) == MAYBE); |
| 48 | |
| 49 | /* false, true and null, also incomplete */ |
| 50 | reader.js_buf = (char_u *)"false"; |
| 51 | assert(json_find_end(&reader) == OK); |
| 52 | reader.js_buf = (char_u *)"f"; |
| 53 | assert(json_find_end(&reader) == MAYBE); |
| 54 | reader.js_buf = (char_u *)"fa"; |
| 55 | assert(json_find_end(&reader) == MAYBE); |
| 56 | reader.js_buf = (char_u *)"fal"; |
| 57 | assert(json_find_end(&reader) == MAYBE); |
| 58 | reader.js_buf = (char_u *)"fals"; |
| 59 | assert(json_find_end(&reader) == MAYBE); |
| 60 | |
| 61 | reader.js_buf = (char_u *)"true"; |
| 62 | assert(json_find_end(&reader) == OK); |
| 63 | reader.js_buf = (char_u *)"t"; |
| 64 | assert(json_find_end(&reader) == MAYBE); |
| 65 | reader.js_buf = (char_u *)"tr"; |
| 66 | assert(json_find_end(&reader) == MAYBE); |
| 67 | reader.js_buf = (char_u *)"tru"; |
| 68 | assert(json_find_end(&reader) == MAYBE); |
| 69 | |
| 70 | reader.js_buf = (char_u *)"null"; |
| 71 | assert(json_find_end(&reader) == OK); |
| 72 | reader.js_buf = (char_u *)"n"; |
| 73 | assert(json_find_end(&reader) == MAYBE); |
| 74 | reader.js_buf = (char_u *)"nu"; |
| 75 | assert(json_find_end(&reader) == MAYBE); |
| 76 | reader.js_buf = (char_u *)"nul"; |
| 77 | assert(json_find_end(&reader) == MAYBE); |
| 78 | |
| 79 | /* object without white space */ |
| 80 | reader.js_buf = (char_u *)"{\"a\":123}"; |
| 81 | assert(json_find_end(&reader) == OK); |
| 82 | reader.js_buf = (char_u *)"{\"a\":123"; |
| 83 | assert(json_find_end(&reader) == MAYBE); |
| 84 | reader.js_buf = (char_u *)"{\"a\":"; |
| 85 | assert(json_find_end(&reader) == MAYBE); |
| 86 | reader.js_buf = (char_u *)"{\"a\""; |
| 87 | assert(json_find_end(&reader) == MAYBE); |
| 88 | reader.js_buf = (char_u *)"{\"a"; |
| 89 | assert(json_find_end(&reader) == MAYBE); |
| 90 | reader.js_buf = (char_u *)"{\""; |
| 91 | assert(json_find_end(&reader) == MAYBE); |
| 92 | reader.js_buf = (char_u *)"{"; |
| 93 | assert(json_find_end(&reader) == MAYBE); |
| 94 | |
| 95 | /* object with white space */ |
| 96 | reader.js_buf = (char_u *)" { \"a\" : 123 } "; |
| 97 | assert(json_find_end(&reader) == OK); |
| 98 | reader.js_buf = (char_u *)" { \"a\" : 123 "; |
| 99 | assert(json_find_end(&reader) == MAYBE); |
| 100 | reader.js_buf = (char_u *)" { \"a\" : "; |
| 101 | assert(json_find_end(&reader) == MAYBE); |
| 102 | reader.js_buf = (char_u *)" { \"a\" "; |
| 103 | assert(json_find_end(&reader) == MAYBE); |
| 104 | reader.js_buf = (char_u *)" { \"a "; |
| 105 | assert(json_find_end(&reader) == MAYBE); |
| 106 | reader.js_buf = (char_u *)" { "; |
| 107 | assert(json_find_end(&reader) == MAYBE); |
| 108 | |
| 109 | /* array without white space */ |
| 110 | reader.js_buf = (char_u *)"[\"a\",123]"; |
| 111 | assert(json_find_end(&reader) == OK); |
| 112 | reader.js_buf = (char_u *)"[\"a\",123"; |
| 113 | assert(json_find_end(&reader) == MAYBE); |
| 114 | reader.js_buf = (char_u *)"[\"a\","; |
| 115 | assert(json_find_end(&reader) == MAYBE); |
| 116 | reader.js_buf = (char_u *)"[\"a\""; |
| 117 | assert(json_find_end(&reader) == MAYBE); |
| 118 | reader.js_buf = (char_u *)"[\"a"; |
| 119 | assert(json_find_end(&reader) == MAYBE); |
| 120 | reader.js_buf = (char_u *)"[\""; |
| 121 | assert(json_find_end(&reader) == MAYBE); |
| 122 | reader.js_buf = (char_u *)"["; |
| 123 | assert(json_find_end(&reader) == MAYBE); |
| 124 | |
| 125 | /* array with white space */ |
| 126 | reader.js_buf = (char_u *)" [ \"a\" , 123 ] "; |
| 127 | assert(json_find_end(&reader) == OK); |
| 128 | reader.js_buf = (char_u *)" [ \"a\" , 123 "; |
| 129 | assert(json_find_end(&reader) == MAYBE); |
| 130 | reader.js_buf = (char_u *)" [ \"a\" , "; |
| 131 | assert(json_find_end(&reader) == MAYBE); |
| 132 | reader.js_buf = (char_u *)" [ \"a\" "; |
| 133 | assert(json_find_end(&reader) == MAYBE); |
| 134 | reader.js_buf = (char_u *)" [ \"a "; |
| 135 | assert(json_find_end(&reader) == MAYBE); |
| 136 | reader.js_buf = (char_u *)" [ "; |
| 137 | assert(json_find_end(&reader) == MAYBE); |
| 138 | } |
| 139 | |
| 140 | static int |
| 141 | fill_from_cookie(js_read_T *reader) |
| 142 | { |
| 143 | reader->js_buf = reader->js_cookie; |
| 144 | return TRUE; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Test json_find_end with an incomplete array, calling the fill function. |
| 149 | */ |
| 150 | static void |
| 151 | test_fill_called_on_find_end(void) |
| 152 | { |
| 153 | js_read_T reader; |
| 154 | |
| 155 | reader.js_fill = fill_from_cookie; |
| 156 | reader.js_used = 0; |
| 157 | reader.js_buf = (char_u *)" [ \"a\" , 123 "; |
| 158 | reader.js_cookie = " [ \"a\" , 123 ] "; |
| 159 | assert(json_find_end(&reader) == OK); |
| 160 | reader.js_buf = (char_u *)" [ \"a\" , "; |
| 161 | assert(json_find_end(&reader) == OK); |
| 162 | reader.js_buf = (char_u *)" [ \"a\" "; |
| 163 | assert(json_find_end(&reader) == OK); |
| 164 | reader.js_buf = (char_u *)" [ \"a"; |
| 165 | assert(json_find_end(&reader) == OK); |
| 166 | reader.js_buf = (char_u *)" [ "; |
| 167 | assert(json_find_end(&reader) == OK); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Test json_find_end with an incomplete string, calling the fill function. |
| 172 | */ |
| 173 | static void |
| 174 | test_fill_called_on_string(void) |
| 175 | { |
| 176 | js_read_T reader; |
| 177 | |
| 178 | reader.js_fill = fill_from_cookie; |
| 179 | reader.js_used = 0; |
| 180 | reader.js_buf = (char_u *)" \"foo"; |
| 181 | reader.js_end = reader.js_buf + STRLEN(reader.js_buf); |
| 182 | reader.js_cookie = " \"foobar\" "; |
| 183 | assert(json_decode_string(&reader, NULL) == OK); |
| 184 | } |
| 185 | |
| 186 | int |
| 187 | main(void) |
| 188 | { |
| 189 | test_decode_find_end(); |
| 190 | test_fill_called_on_find_end(); |
| 191 | test_fill_called_on_string(); |
| 192 | return 0; |
| 193 | } |