|
| 1 | +import { search } from '../src'; |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | + |
| 4 | +describe('JMESPath Ternary Operations', () => { |
| 5 | + const data = { |
| 6 | + true: true, |
| 7 | + false: false, |
| 8 | + foo: 'foo', |
| 9 | + bar: 'bar', |
| 10 | + baz: 'baz', |
| 11 | + qux: 'qux', |
| 12 | + quux: 'quux', |
| 13 | + two: 2, |
| 14 | + fourty: 40, |
| 15 | + }; |
| 16 | + |
| 17 | + describe('Basic Ternary Operations', () => { |
| 18 | + it('should return foo when condition is true', () => { |
| 19 | + expect(search(data, 'true ? foo : bar')).toBe('foo'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return bar when condition is false', () => { |
| 23 | + expect(search(data, 'false ? foo : bar')).toBe('bar'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return bar when condition is null', () => { |
| 27 | + expect(search(data, '`null` ? foo : bar')).toBe('bar'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return bar when condition is empty array', () => { |
| 31 | + expect(search(data, '`[]` ? foo : bar')).toBe('bar'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return bar when condition is not an empty array', () => { |
| 35 | + expect(search(data, '`[1]` ? foo : bar')).toBe('foo'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return bar when condition is empty object', () => { |
| 39 | + expect(search(data, '`{}` ? foo : bar')).toBe('bar'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return bar when condition is empty string', () => { |
| 43 | + expect(search(data, "'' ? foo : bar")).toBe('bar'); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('Chained Ternary Operations', () => { |
| 48 | + it('should handle chained ternary operations', () => { |
| 49 | + expect(search(data, 'foo ? bar ? baz : qux : quux')).toBe('baz'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('Ternary Operations with Precedence', () => { |
| 54 | + it('should handle precedence with pipes', () => { |
| 55 | + expect(search(data, 'false ? foo | bar | @ : baz')).toBe('baz'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should handle precedence with arithmetic', () => { |
| 59 | + expect(search(data, 'foo ? fourty + two : `false`')).toBe(42); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('Nested Ternary Operations', () => { |
| 64 | + it('should handle left-nested ternary operations', () => { |
| 65 | + expect(search(data, 'true ? (true ? foo : bar) : baz')).toBe('foo'); |
| 66 | + expect(search(data, 'true ? (false ? foo : bar) : baz')).toBe('bar'); |
| 67 | + expect(search(data, 'false ? (true ? foo : bar) : baz')).toBe('baz'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle right-nested ternary operations', () => { |
| 71 | + expect(search(data, 'true ? foo : (true ? bar : baz)')).toBe('foo'); |
| 72 | + expect(search(data, 'false ? foo : (true ? bar : baz)')).toBe('bar'); |
| 73 | + expect(search(data, 'false ? foo : (false ? bar : baz)')).toBe('baz'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle multiple nested ternary operations', () => { |
| 77 | + expect(search(data, 'true ? (true ? (true ? foo : bar) : baz) : quux')).toBe('foo'); |
| 78 | + expect(search(data, 'true ? (true ? (false ? foo : bar) : baz) : quux')).toBe('bar'); |
| 79 | + expect(search(data, 'true ? (false ? (true ? foo : bar) : baz) : quux')).toBe('baz'); |
| 80 | + expect(search(data, 'false ? (true ? (true ? foo : bar) : baz) : quux')).toBe('quux'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle mixed nested ternary operations with literals', () => { |
| 84 | + expect(search(data, 'true ? (`null` ? foo : bar) : baz')).toBe('bar'); |
| 85 | + expect(search(data, 'true ? (`[]` ? foo : bar) : baz')).toBe('bar'); |
| 86 | + expect(search(data, 'true ? (`{}` ? foo : bar) : baz')).toBe('bar'); |
| 87 | + expect(search(data, "true ? ('' ? foo : bar) : baz")).toBe('bar'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should handle nested ternary operations with arithmetic', () => { |
| 91 | + const testData = { |
| 92 | + ...data, |
| 93 | + threshold: 40, |
| 94 | + }; |
| 95 | + expect(search(testData, 'true ? (fourty + two > threshold ? foo : bar) : baz')).toBe('foo'); |
| 96 | + expect(search(testData, 'true ? (fourty + two < threshold ? foo : bar) : baz')).toBe('bar'); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments