Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 2x 2x 2x 2x 2x 2x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import {arrayData, bookstore, complexData, data0, jsonpathDotComExample, testData} from './fixtures';
import type {JsonPathEval} from '../JsonPathEval';
export const testJsonPathExec = (run: typeof JsonPathEval.run) => {
describe('JSONPath specification', () => {
describe('root selector', () => {
test('select root from object', () => {
const expr = '$';
const data = {foo: 'bar'};
const result = run(expr, data);
expect(result.length).toBe(1);
expect(result[0].data).toEqual(data);
expect(result[0].pointer()).toBe('$');
});
test('mis-formatted root "$."', () => {
const expr = '$.';
const data = {foo: 'bar'};
expect(() => run(expr, data)).toThrow();
});
test('mis-formatted root ""', () => {
const expr = '';
const data = {foo: 'bar'};
expect(() => run(expr, data)).toThrow();
});
});
describe('recursive descent from root', () => {
test('$.. should be invalid (RFC 9535 Section 2.5.2.1)', () => {
const expr = '$..';
const data = {foo: 'bar'};
expect(() => run(expr, data)).toThrow();
});
test('$..*', () => {
const expr = '$..*';
const data = {
type: 'Program',
body: [],
sourceType: 'module',
range: [0, 1718],
};
const result = run(expr, data);
// $..*selects all descendants: all member values and array elements
// RFC 9535: "all member values and array elements contained in the input value"
expect(result.length).toBe(6);
const dataList = result.map((r) => r.data);
// Should contain all primitive and structured values in descendants
expect(dataList).toContain('Program'); // value of "type"
expect(dataList).toContain('module'); // value of "sourceType"
expect(dataList).toEqual(expect.arrayContaining(['Program', [], 'module', [0, 1718], 0, 1718]));
});
});
describe('named selector', () => {
test('basic object selection', () => {
const expr = '$.store.book[0].title';
const result = run(expr, data0);
expect(result).toMatchObject([
{
data: 'Harry Potter',
},
]);
expect(result[0].path()).toEqual(['$', 'store', 'book', 0, 'title']);
expect(result[0].pointer()).toBe("$['store']['book'][0]['title']");
});
test('nested object access', () => {
const expr = '$.store.bicycle.color';
const result = run(expr, data0);
expect(result.length).toBe(1);
expect(result[0].data).toBe('red');
});
test('nonexistent property', () => {
const expr = '$.store.nonexistent';
const result = run(expr, data0);
expect(result.length).toBe(0);
});
});
describe('index selector', () => {
test('positive array index', () => {
const expr = '$[1]';
const result = run(expr, arrayData);
expect(result.length).toBe(1);
expect(result[0].data).toBe('b');
});
test('negative array index', () => {
const expr = '$[-2]';
const result = run(expr, arrayData);
expect(result.length).toBe(1);
expect(result[0].data).toBe('f');
});
test('out of bounds index', () => {
const expr = '$[10]';
const result = run(expr, arrayData);
expect(result.length).toBe(0);
});
test('negative out of bounds index', () => {
const expr = '$[-10]';
const result = run(expr, arrayData);
expect(result.length).toBe(0);
});
});
describe('wildcard selector', () => {
test('wildcard on object', () => {
const expr = '$.store[*]';
const result = run(expr, data0);
expect(result.length).toBe(2);
// Results should include both book array and bicycle object
const values = result.map((r) => r.data);
expect(values).toContainEqual(data0.store.book);
expect(values).toContainEqual(data0.store.bicycle);
});
test('wildcard on array', () => {
const expr = '$[*]';
const result = run(expr, arrayData);
expect(result.length).toBe(arrayData.length);
// Array elements should be in order
for (let i = 0; i < arrayData.length; i++) {
expect(result[i].data).toBe(arrayData[i]);
}
});
test('wildcard on primitive', () => {
const expr = '$[*]';
const result = run(expr, 'hello');
expect(result.length).toBe(0);
});
});
describe('slice selector', () => {
test('basic slice [1:3]', () => {
const expr = '$[1:3]';
const result = run(expr, arrayData);
expect(result.length).toBe(2);
expect(result[0].data).toBe('b');
expect(result[1].data).toBe('c');
});
test('slice with no end [5:]', () => {
const expr = '$[5:]';
const result = run(expr, arrayData);
expect(result.length).toBe(2);
expect(result[0].data).toBe('f');
expect(result[1].data).toBe('g');
});
test('slice with step [1:5:2]', () => {
const expr = '$[1:5:2]';
const result = run(expr, arrayData);
expect(result.length).toBe(2);
expect(result[0].data).toBe('b');
expect(result[1].data).toBe('d');
});
test('slice with negative step [5:1:-2]', () => {
const expr = '$[5:1:-2]';
const result = run(expr, arrayData);
expect(result.length).toBe(2);
expect(result[0].data).toBe('f');
expect(result[1].data).toBe('d');
});
test('reverse slice [::-1]', () => {
const expr = '$[::-1]';
const result = run(expr, arrayData);
expect(result.length).toBe(arrayData.length);
// Should be in reverse order
for (let i = 0; i < arrayData.length; i++) {
expect(result[i].data).toBe(arrayData[arrayData.length - 1 - i]);
}
});
test('slice with zero step', () => {
const expr = '$[1:5:0]';
const result = run(expr, arrayData);
expect(result.length).toBe(0);
});
test('slice on non-array', () => {
const expr = '$[1:3]';
const result = run(expr, data0.store);
expect(result.length).toBe(0);
});
});
describe('filter selector', () => {
test('simple comparison filter', () => {
const expr = '$.store.book[?@.price < 10]';
const result = run(expr, data0);
expect(result.length).toBe(1);
expect((result[0].data as any).title).toBe('Harry Potter');
});
test('equality comparison filter', () => {
const expr = '$.a[?@.b == "k"]';
const result = run(expr, complexData);
expect(result.length).toBe(1);
expect((result[0].data as any).b).toBe('k');
});
test('existence filter', () => {
const expr = '$.a[?@.b]';
const result = run(expr, complexData);
expect(result.length).toBe(4); // All objects with property 'b'
});
test('array value comparison', () => {
const expr = '$.a[?@ > 3.5]';
const result = run(expr, complexData);
const values = result.map((r) => r.data);
expect(values).toEqual([5, 4, 6]);
});
});
describe('recursive descent selector', () => {
test('descendant by name', () => {
const expr = '$..price';
const result = run(expr, data0);
expect(result.length).toBe(3); // Two book prices + bicycle price
const values = result.map((r) => r.data);
expect(values).toContain(8.95);
expect(values).toContain(12.99);
expect(values).toContain(399);
});
test('descendant by index', () => {
const expr = '$..[0]';
const result = run(expr, data0);
expect(result.length).toBe(1); // Only book[0]
expect((result[0].data as any).title).toBe('Harry Potter');
});
test('descendant wildcard', () => {
const expr = '$..*';
const result = run(expr, {a: {b: 1}, c: [2, 3]});
// Should select all descendant values
expect(result.length).toBeGreaterThan(0);
const values = result.map((r) => r.data);
expect(values).toContain(1);
expect(values).toContain(2);
expect(values).toContain(3);
});
});
describe('combined selectors', () => {
test('multiple selectors in brackets', () => {
const expr = '$[0, 3]';
const result = run(expr, arrayData);
expect(result.length).toBe(2);
expect(result[0].data).toBe('a');
expect(result[1].data).toBe('d');
});
test('slice and index combined', () => {
const expr = '$[0:2, 5]';
const result = run(expr, arrayData);
expect(result.length).toBe(3);
expect(result[0].data).toBe('a');
expect(result[1].data).toBe('b');
expect(result[2].data).toBe('f');
});
test('duplicated entries', () => {
const expr = '$[0, 0]';
const result = run(expr, arrayData);
expect(result.length).toBe(2);
expect(result[0].data).toBe('a');
expect(result[1].data).toBe('a');
});
});
describe('edge cases', () => {
test('empty array', () => {
const expr = '$[*]';
const result = run(expr, []);
expect(result.length).toBe(0);
});
test('empty object', () => {
const expr = '$[*]';
const result = run(expr, {});
expect(result.length).toBe(0);
});
test('null values', () => {
const expr = '$.a';
const result = run(expr, {a: null});
expect(result.length).toBe(1);
expect(result[0].data).toBe(null);
});
test('deeply nested structure', () => {
const deep = {a: {b: {c: {d: {e: 'deep'}}}}};
const expr = '$.a.b.c.d.e';
const result = run(expr, deep);
expect(result.length).toBe(1);
expect(result[0].data).toBe('deep');
});
});
describe('real-world examples', () => {
describe('jsonpath.com front page examples', () => {
test('phone number type', () => {
const expr = '$.phoneNumbers[:1].type';
const result = run(expr, jsonpathDotComExample);
expect(result.length).toBe(1);
expect(result[0].data).toBe('iPhone');
expect(result[0].pointer()).toBe("$['phoneNumbers'][0]['type']");
});
});
describe('hevodata.com', () => {
test('example from docs', () => {
const expr = '$.event.data.name';
const data = {
event: {
agency: 'MI6',
data: {
name: 'James Bond',
id: '007',
},
},
};
const result = run(expr, data);
expect(result.length).toBe(1);
expect(result[0].data).toBe('James Bond');
expect(result[0].pointer()).toBe("$['event']['data']['name']");
});
});
});
describe('RFC 9535 compliance examples', () => {
test('$.store.book[*].author - the authors of all books in the store', () => {
const expr = '$.store.book[*].author';
const result = run(expr, bookstore);
expect(result.length).toBe(4);
const authors = result.map((r) => r.data);
expect(authors).toContain('Nigel Rees');
expect(authors).toContain('Evelyn Waugh');
expect(authors).toContain('Herman Melville');
expect(authors).toContain('J. R. R. Tolkien');
});
test('$..author - all authors', () => {
const expr = '$..author';
const result = run(expr, bookstore);
expect(result.length).toBe(4);
});
test('$.store.* - all things in the store', () => {
const expr = '$.store.*';
const result = run(expr, bookstore);
expect(result.length).toBe(2); // book array and bicycle object
});
test('$..book[2] - the third book', () => {
const expr = '$..book[2]';
const result = run(expr, bookstore);
expect(result.length).toBe(1);
expect((result[0].data as any).title).toBe('Moby Dick');
});
test('$..book[-1] - the last book', () => {
const expr = '$..book[-1]';
const result = run(expr, bookstore);
expect(result.length).toBe(1);
expect((result[0].data as any).title).toBe('The Lord of the Rings');
});
test('$..book[0,1] - the first two books', () => {
const expr = '$..book[0,1]';
const result = run(expr, bookstore);
expect(result.length).toBe(2);
expect((result[0].data as any).title).toBe('Sayings of the Century');
expect((result[1].data as any).title).toBe('Sword of Honour');
});
test('$..book[:2] - the first two books', () => {
const expr = '$..book[:2]';
const result = run(expr, bookstore);
expect(result.length).toBe(2);
expect((result[0].data as any).title).toBe('Sayings of the Century');
expect((result[1].data as any).title).toBe('Sword of Honour');
});
test('$..book[?@.isbn] - all books with an ISBN number', () => {
const expr = '$..book[?@.isbn]';
const result = run(expr, bookstore);
expect(result.length).toBe(2);
const titles = result.map((r) => (r.data as any).title);
expect(titles).toContain('Moby Dick');
expect(titles).toContain('The Lord of the Rings');
});
test('$..book[?@.price<10] - all books cheaper than 10', () => {
const expr = '$..book[?@.price < 10]';
const result = run(expr, bookstore);
expect(result.length).toBe(2);
const titles = result.map((r) => (r.data as any).title);
expect(titles).toContain('Sayings of the Century');
expect(titles).toContain('Moby Dick');
});
});
describe('JSONPath function extensions (RFC 9535)', () => {
describe('length() function', () => {
test('length of string', () => {
const expr = '$[?length(@.info.name) == 10]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('length of array', () => {
const expr = '$[?length(@.authors) == 3]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('length of object', () => {
const expr = '$[?length(@.info.contacts) == 2]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('length of array with books >= 4', () => {
const expr = '$[?length(@.store.book) >= 4]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('length with non-existent property returns nothing', () => {
const expr = '$[?length(@.nonexistent) == 0]';
const result = run(expr, testData);
expect(result.length).toBe(0);
});
test('length of primitive values returns nothing', () => {
const expr = '$[?length(@.store.bicycle.price) == 0]';
const result = run(expr, testData);
expect(result.length).toBe(0);
});
test('length function with Unicode characters', () => {
const unicodeData = {text: 'Hello 🌍 World'};
const expr = '$[?length(@.text) == 13]';
const result = run(expr, unicodeData);
expect(result.length).toBe(1);
});
});
describe('count() function', () => {
test('count all books', () => {
const expr = '$[?count(@.store.book[*]) == 4]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('count books with ISBN', () => {
const expr = '$[?count(@.store.book[?@.isbn]) == 2]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('count all descendants', () => {
const expr = '$[?count(@..*) > 20]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('count with empty result', () => {
const expr = '$[?count(@.nonexistent[*]) == 0]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('count specific property occurrences', () => {
const expr = '$[?count(@..price) == 5]'; // 4 books + 1 bicycle
const result = run(expr, testData);
expect(result.length).toBe(1);
});
});
describe('match() function', () => {
test('match exact string pattern', () => {
const expr = '$.store.book[?match(@.category, "fiction")]';
const result = run(expr, testData);
expect(result.length).toBe(3); // Three fiction books
});
test('match with character class', () => {
const expr = '$.store.book[?match(@.title, ".*Lord.*")]';
const result = run(expr, testData);
expect(result.length).toBe(1);
expect((result[0].data as any).title).toBe('The Lord of the Rings');
});
test('match ISBN pattern', () => {
const expr = '$.store.book[?match(@.isbn, "[0-9]+-[0-9]+-[0-9]+-[0-9]+")]';
const result = run(expr, testData);
expect(result.length).toBe(2);
});
test('match case sensitive', () => {
const expr = '$.store.book[?match(@.category, "Fiction")]'; // Capital F
const result = run(expr, testData);
expect(result.length).toBe(0);
});
test('match with anchors', () => {
const expr = '$.store.book[?match(@.author, ".*Tolkien")]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('match non-string returns false', () => {
const expr = '$.store.book[?match(@.price, "12\\.99")]';
const result = run(expr, testData);
expect(result.length).toBe(0); // price is number, not string
});
test('match invalid regex returns false', () => {
const expr = '$.store.book[?match(@.title, "[")]';
const result = run(expr, testData);
expect(result.length).toBe(0);
});
});
describe('search() function', () => {
test('search substring in title', () => {
const expr = '$.store.book[?search(@.title, "Lord")]';
const result = run(expr, testData);
expect(result.length).toBe(1);
expect((result[0].data as any).title).toBe('The Lord of the Rings');
});
test('search pattern in author names', () => {
const expr = '$.store.book[?search(@.author, "[JE].*")]';
const result = run(expr, testData);
expect(result.length).toBe(2); // J. R. R. Tolkien and Evelyn Waugh
});
test('search with character classes', () => {
const expr = '$.store.book[?search(@.title, "[Ss]word")]';
const result = run(expr, testData);
expect(result.length).toBe(1);
expect((result[0].data as any).title).toBe('Sword of Honour');
});
test('search digit pattern', () => {
const expr = '$.store.book[?search(@.isbn, "[0-9]+")]';
const result = run(expr, testData);
expect(result.length).toBe(2);
});
test('search case insensitive-like pattern', () => {
const expr = '$.store.book[?search(@.category, "[Ff]iction")]';
const result = run(expr, testData);
expect(result.length).toBe(3);
});
test('search non-string returns false', () => {
const expr = '$.store.book[?search(@.price, "99")]';
const result = run(expr, testData);
expect(result.length).toBe(0);
});
test('search with word boundaries', () => {
const expr = '$.authors[?search(@, "^Bob$")]';
const result = run(expr, testData);
expect(result.length).toBe(1);
expect(result[0].data).toBe('Bob');
});
});
describe('value() function', () => {
test('value from single node', () => {
const expr = '$[?value(@..color) == "red"]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('value from single price query', () => {
const expr = '$.store.book[?value(@.price) < 10]';
const result = run(expr, testData);
expect(result.length).toBe(2);
});
test('value from multiple nodes returns nothing', () => {
const expr = '$[?value(@..price) == 8.95]'; // Multiple prices exist
const result = run(expr, testData);
expect(result.length).toBe(0); // Should return nothing for multiple nodes
});
test('value from empty query returns nothing', () => {
const expr = '$[?value(@.nonexistent) == null]';
const result = run(expr, testData);
expect(result.length).toBe(0);
});
test('value with specific property', () => {
const expr = '$.store.book[?value(@.isbn) != null]';
const result = run(expr, testData);
expect(result.length).toBe(2);
});
});
describe('combined function usage', () => {
test('length and count together', () => {
const expr = '$[?length(@.authors) == count(@.authors[*])]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('match and search difference', () => {
const matchExpr = '$.store.book[?match(@.title, "Lord")]';
const searchExpr = '$.store.book[?search(@.title, "Lord")]';
const matchResult = run(matchExpr, testData);
const searchResult = run(searchExpr, testData);
expect(matchResult.length).toBe(0); // Exact match fails
expect(searchResult.length).toBe(1); // Substring search succeeds
});
test('nested function calls', () => {
const expr = '$[?count(@.store.book[?length(@.title) > 15]) == 2]';
const result = run(expr, testData);
expect(result.length).toBe(1);
});
test('value with length filter', () => {
const expr = '$.store.book[?length(value(@.title)) > 10]';
const result = run(expr, testData);
expect(result.length).toBe(3); // All but "Moby Dick"
});
test('functions in logical expressions', () => {
const expr = '$.store.book[?length(@.title) > 10 && search(@.category, "fiction")]';
const result = run(expr, testData);
expect(result.length).toBe(2); // Sword of Honour and The Lord of the Rings
});
});
describe('error cases and edge cases', () => {
test('functions with wrong argument count', () => {
const expr = '$[?length(@.name, @.other) == 5]'; // Wrong arg count
const result = run(expr, testData);
expect(result.length).toBe(0);
});
test('unknown function returns false', () => {
const expr = '$[?unknown(@.name) == true]';
const result = run(expr, testData);
expect(result.length).toBe(0);
});
test('function with null values', () => {
const nullData = {items: [null, '', 0, false]};
const expr = '$.items[?length(@) == 0]';
const result = run(expr, nullData);
expect(result.length).toBe(1); // Only empty string has length 0
expect(result[0].data).toBe('');
});
});
});
});
};
|