|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { parse } from './jsonpath' |
| 4 | + |
| 5 | +describe('utils/jsonpath', () => { |
| 6 | + describe('parse', () => { |
| 7 | + it('should accept dot-notation child access', () => { |
| 8 | + expect(() => parse('$.store.book')).not.toThrow() |
| 9 | + }) |
| 10 | + |
| 11 | + it('should accept wildcard subscript', () => { |
| 12 | + expect(() => parse('$.store.book[*].author')).not.toThrow() |
| 13 | + }) |
| 14 | + |
| 15 | + it('should accept recursive descent', () => { |
| 16 | + expect(() => parse('$..author')).not.toThrow() |
| 17 | + }) |
| 18 | + |
| 19 | + it('should accept wildcard member', () => { |
| 20 | + expect(() => parse('$.store.*')).not.toThrow() |
| 21 | + }) |
| 22 | + |
| 23 | + it('should accept recursive descent with member', () => { |
| 24 | + expect(() => parse('$.store..price')).not.toThrow() |
| 25 | + }) |
| 26 | + |
| 27 | + it('should accept numeric array index', () => { |
| 28 | + expect(() => parse('$..book[2]')).not.toThrow() |
| 29 | + }) |
| 30 | + |
| 31 | + it('should accept script subscript expression', () => { |
| 32 | + expect(() => parse('$..book[(@.length-1)]')).not.toThrow() |
| 33 | + }) |
| 34 | + |
| 35 | + it('should accept negative slice', () => { |
| 36 | + expect(() => parse('$..book[-1:]')).not.toThrow() |
| 37 | + }) |
| 38 | + |
| 39 | + it('should accept union of indices', () => { |
| 40 | + expect(() => parse('$..book[0,1]')).not.toThrow() |
| 41 | + }) |
| 42 | + |
| 43 | + it('should accept array slice', () => { |
| 44 | + expect(() => parse('$..book[:2]')).not.toThrow() |
| 45 | + }) |
| 46 | + |
| 47 | + it('should accept filter expression with existence check', () => { |
| 48 | + expect(() => parse('$..book[?(@.isbn)]')).not.toThrow() |
| 49 | + }) |
| 50 | + |
| 51 | + it('should accept filter expression with comparison', () => { |
| 52 | + expect(() => parse('$..book[?(@.price<10)]')).not.toThrow() |
| 53 | + }) |
| 54 | + |
| 55 | + it('should accept recursive wildcard', () => { |
| 56 | + expect(() => parse('$..*')).not.toThrow() |
| 57 | + }) |
| 58 | + |
| 59 | + it('should reject plain text', () => { |
| 60 | + expect(() => parse('not a path')).toThrow() |
| 61 | + }) |
| 62 | + |
| 63 | + it('should reject empty string', () => { |
| 64 | + expect(() => parse('')).toThrow() |
| 65 | + }) |
| 66 | + |
| 67 | + // Goessner spec does not allow `}` in expressions, but jsonpath-plus |
| 68 | + // silently accepts malformed bracket contents via toPathArray. |
| 69 | + it('should not reject malformed brackets (spec deviation)', () => { |
| 70 | + expect(() => parse('$[}}}')).not.toThrow() |
| 71 | + }) |
| 72 | + |
| 73 | + it('should reject expression missing root', () => { |
| 74 | + expect(() => parse('[invalid')).toThrow() |
| 75 | + }) |
| 76 | + }) |
| 77 | +}) |
0 commit comments