| 116 | } |
| 117 | |
| 118 | int main(int argc, char *argv[]) |
| 119 | { |
| 120 | const jsmntok_t *toks, *t; |
| 121 | char *buf; |
| 122 | char *s; |
| 123 | u32 u32val; |
| 124 | u8 *hex; |
| 125 | const char *err; |
| 126 | |
| 127 | common_setup(argv[0]); |
| 128 | |
| 129 | buf = tal_strdup(tmpctx, "{\"1\":\"one\", \"2\":\"two\", \"3\":{\"three\": {\"deeper\": 17}}, \"arr\": [{\"1\": \"arrone\"}, 2, [3, 4]]}"); |
| 130 | toks = json_parse_simple(tmpctx, buf, strlen(buf)); |
| 131 | assert(toks); |
| 132 | assert(toks->size == 4); |
| 133 | |
| 134 | /* These are direct matches, and they work. */ |
| 135 | assert(!json_scan(tmpctx, buf, toks, "{1:one}")); |
| 136 | assert(!json_scan(tmpctx, buf, toks, "{1:one,2:two}")); |
| 137 | assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one}")); |
| 138 | assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}}}")); |
| 139 | assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}},arr:[0:{1:arrone}]}")); |
| 140 | assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}},arr:[1:2]}")); |
| 141 | assert(!json_scan(tmpctx, buf, toks, "{2:two,1:one,3:{three:{deeper:17}},arr:[1:2,2:[0:3,1:4]]}")); |
| 142 | |
| 143 | /* These do not match */ |
| 144 | err = json_scan(tmpctx, buf, toks, "{2:one}"); |
| 145 | assert(streq(err, "Parsing '{2:one': \"two\" does not match expected one")); |
| 146 | err = json_scan(tmpctx, buf, toks, "{1:one,2:tw}"); |
| 147 | assert(streq(err, "Parsing '{1:one,2:tw': \"two\" does not match expected tw")); |
| 148 | err = json_scan(tmpctx, buf, toks, "{1:one,2:twoo}"); |
| 149 | assert(streq(err, "Parsing '{1:one,2:twoo': \"two\" does not match expected twoo")); |
| 150 | err = json_scan(tmpctx, buf, toks, "{4:one}"); |
| 151 | assert(streq(err, "Parsing '{4:': object does not have member 4")); |
| 152 | err = json_scan(tmpctx, buf, toks, "{2:two,1:one,3:three}"); |
| 153 | assert(streq(err, "Parsing '{2:two,1:one,3:three': {\"three\": {\"deeper\": 17}} does not match expected three")); |
| 154 | err = json_scan(tmpctx, buf, toks, "{3:{three:deeper}}"); |
| 155 | assert(streq(err, "Parsing '{3:{three:deeper': {\"deeper\": 17} does not match expected deeper")); |
| 156 | err = json_scan(tmpctx, buf, toks, "{3:{three:{deeper:{}}}}"); |
| 157 | assert(streq(err, "Parsing '{3:{three:{deeper:{': token is not an object: 17")); |
| 158 | err = json_scan(tmpctx, buf, toks, "{arr:{}}"); |
| 159 | assert(streq(err, "Parsing '{arr:{': token is not an object: [{\"1\": \"arrone\"}, 2, [3, 4]]")); |
| 160 | err = json_scan(tmpctx, buf, toks, "{arr:[0:1]}"); |
| 161 | assert(streq(err, "Parsing '{arr:[0:1': {\"1\": \"arrone\"} does not match expected 1")); |
| 162 | err = json_scan(tmpctx, buf, toks, "{arr:[4:0]}"); |
| 163 | assert(streq(err, "Parsing '{arr:[4:': token has no index 4: [{\"1\": \"arrone\"}, 2, [3, 4]]")); |
| 164 | err = json_scan(tmpctx, buf, toks, "{arr:[0:{1:arrtwo}]}"); |
| 165 | assert(streq(err, "Parsing '{arr:[0:{1:arrtwo': \"arrone\" does not match expected arrtwo")); |
| 166 | err = json_scan(tmpctx, buf, toks, "{arr:[1:3]}"); |
| 167 | assert(streq(err, "Parsing '{arr:[1:3': 2 does not match expected 3")); |
| 168 | err = json_scan(tmpctx, buf, toks, "{arr:[2:[0:1]]}"); |
| 169 | assert(streq(err, "Parsing '{arr:[2:[0:1': 3 does not match expected 1")); |
| 170 | |
| 171 | /* These capture simple values. */ |
| 172 | assert(!json_scan(tmpctx, buf, toks, "{3:{three:{deeper:%}}}", |
| 173 | JSON_SCAN(json_to_number, &u32val))); |
| 174 | assert(u32val == 17); |
| 175 | err = json_scan(tmpctx, buf, toks, "{1:%}", |
nothing calls this directly
no test coverage detected