|
| 1 | +import {AttributePath, PathElement} from "./AttributePath"; |
| 2 | + |
| 3 | +describe('AttributePath', () => { |
| 4 | + it('should convert a string path to a list of elements', () => { |
| 5 | + expect( |
| 6 | + new AttributePath('foo.bar.baz[3][4][2].fizz[0].buzz[1]').elements |
| 7 | + ).toEqual([ |
| 8 | + {type: 'AttributeName', name: 'foo'}, |
| 9 | + {type: 'AttributeName', name: 'bar'}, |
| 10 | + {type: 'AttributeName', name: 'baz'}, |
| 11 | + {type: 'ListIndex', index: 3}, |
| 12 | + {type: 'ListIndex', index: 4}, |
| 13 | + {type: 'ListIndex', index: 2}, |
| 14 | + {type: 'AttributeName', name: 'fizz'}, |
| 15 | + {type: 'ListIndex', index: 0}, |
| 16 | + {type: 'AttributeName', name: 'buzz'}, |
| 17 | + {type: 'ListIndex', index: 1}, |
| 18 | + ]); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should clone an iterable of elements passed to the constructor', () => { |
| 22 | + const elements: Array<PathElement> = [ |
| 23 | + {type: 'AttributeName', name: 'foo'}, |
| 24 | + {type: 'AttributeName', name: 'bar'}, |
| 25 | + {type: 'AttributeName', name: 'baz'}, |
| 26 | + {type: 'ListIndex', index: 3}, |
| 27 | + {type: 'ListIndex', index: 4}, |
| 28 | + {type: 'ListIndex', index: 2}, |
| 29 | + {type: 'AttributeName', name: 'fizz'}, |
| 30 | + {type: 'ListIndex', index: 0}, |
| 31 | + {type: 'AttributeName', name: 'buzz'}, |
| 32 | + {type: 'ListIndex', index: 1}, |
| 33 | + ]; |
| 34 | + const path = new AttributePath(elements); |
| 35 | + |
| 36 | + expect(path.elements).toEqual(elements); |
| 37 | + expect(path.elements).not.toBe(elements); |
| 38 | + |
| 39 | + elements.shift(); |
| 40 | + expect(path.elements).not.toEqual(elements); |
| 41 | + expect(path.elements.slice(1)).toEqual(elements); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should allow attribute names with embedded control characters', () => { |
| 45 | + expect(new AttributePath('_bracket_\\[_period_\\._backslash_\\\\_unescaped_backslash_\\_.foo').elements).toEqual([ |
| 46 | + {type: 'AttributeName', name: '_bracket_[_period_._backslash_\\_unescaped_backslash_\\_'}, |
| 47 | + {type: 'AttributeName', name: 'foo'}, |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('path correctness checking', () => { |
| 52 | + it( |
| 53 | + 'should throw an error when a path begins with a control character', |
| 54 | + () => { |
| 55 | + expect(() => new AttributePath('[1]')) |
| 56 | + .toThrowError(/Invalid control character/); |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + it( |
| 61 | + 'should throw an error when a list index access contains invalid characters', |
| 62 | + () => { |
| 63 | + expect(() => new AttributePath('foo[a]')) |
| 64 | + .toThrowError(/Invalid array index character/); |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + it( |
| 69 | + 'should throw an error when a list index access contains no characters', |
| 70 | + () => { |
| 71 | + expect(() => new AttributePath('foo[]')) |
| 72 | + .toThrowError(/Invalid array index/); |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + it( |
| 77 | + 'should throw an error when an identifier immediately follows a list index access', |
| 78 | + () => { |
| 79 | + expect(() => new AttributePath('foo[1]a')) |
| 80 | + .toThrowError(/Bare identifier encountered/); |
| 81 | + } |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('::isAttributePath', () => { |
| 86 | + const ctor = AttributePath; |
| 87 | + |
| 88 | + afterEach(() => { |
| 89 | + (AttributePath as any) = ctor; |
| 90 | + }); |
| 91 | + |
| 92 | + it('should return true for AttributePath objects', () => { |
| 93 | + expect( |
| 94 | + AttributePath.isAttributePath(new AttributePath('foo')) |
| 95 | + ).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return false for scalar values', () => { |
| 99 | + for (let scalar of ['string', 123.234, true, null, void 0]) { |
| 100 | + expect(AttributePath.isAttributePath(scalar)).toBe(false); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it( |
| 105 | + 'should return true for AttributePaths created with a different instance of the AttributePath constructor', |
| 106 | + () => { |
| 107 | + const {isAttributePath} = AttributePath; |
| 108 | + const path = new AttributePath('foo.bar'); |
| 109 | + (AttributePath as any) = () => path; |
| 110 | + |
| 111 | + expect(path).not.toBeInstanceOf(AttributePath); |
| 112 | + expect(isAttributePath(path)).toBe(true); |
| 113 | + } |
| 114 | + ); |
| 115 | + }); |
| 116 | +}); |
0 commit comments