|
| 1 | +import { UnscopedValidationError } from '../../lib'; |
| 2 | +import { TemplateString } from '../../lib/helpers-internal'; |
| 3 | + |
| 4 | +describe('new TemplateString', () => { |
| 5 | + describe('parse', () => { |
| 6 | + it('parses template with single variable correctly', () => { |
| 7 | + const result = new TemplateString('Hello, ${name}!').parse('Hello, John!'); |
| 8 | + expect(result).toEqual({ name: 'John' }); |
| 9 | + }); |
| 10 | + |
| 11 | + it('parses template with multiple variables correctly', () => { |
| 12 | + const result = new TemplateString('My name is ${firstName} ${lastName}.').parse('My name is Jane Doe.'); |
| 13 | + expect(result).toEqual({ firstName: 'Jane', lastName: 'Doe' }); |
| 14 | + }); |
| 15 | + |
| 16 | + it('throws error when input does not match template', () => { |
| 17 | + expect(() => { |
| 18 | + new TemplateString('Hello, ${name}!').parse('Hi, John!'); |
| 19 | + }).toThrow(UnscopedValidationError); |
| 20 | + }); |
| 21 | + |
| 22 | + it('parses template with no variables correctly', () => { |
| 23 | + const result = new TemplateString('Hello, world!').parse('Hello, world!'); |
| 24 | + expect(result).toEqual({}); |
| 25 | + }); |
| 26 | + |
| 27 | + it('parses template with trailing variable correctly', () => { |
| 28 | + const result = new TemplateString('Path: ${path}').parse('Path: /home/user'); |
| 29 | + expect(result).toEqual({ path: '/home/user' }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('throws error when input has extra characters', () => { |
| 33 | + expect(() => { |
| 34 | + new TemplateString('Hello, ${name}!').parse('Hello, John!!'); |
| 35 | + }).toThrow(UnscopedValidationError); |
| 36 | + }); |
| 37 | + |
| 38 | + it('parses template with adjacent variables correctly', () => { |
| 39 | + const result = new TemplateString('${greeting}, ${name}!').parse('Hi, John!'); |
| 40 | + expect(result).toEqual({ greeting: 'Hi', name: 'John' }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('throws error when input is shorter than template', () => { |
| 44 | + expect(() => { |
| 45 | + new TemplateString('Hello, ${name}!').parse('Hello, '); |
| 46 | + }).toThrow(UnscopedValidationError); |
| 47 | + }); |
| 48 | + |
| 49 | + it('parses template with empty variable value correctly', () => { |
| 50 | + const result = new TemplateString('Hello, ${name}!').parse('Hello, !'); |
| 51 | + expect(result).toEqual({ name: '' }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('parses template with variable at the start correctly', () => { |
| 55 | + const result = new TemplateString('${greeting}, world!').parse('Hi, world!'); |
| 56 | + expect(result).toEqual({ greeting: 'Hi' }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('parses complex template correctly', () => { |
| 60 | + const result = new TemplateString('arn:${Partition}:dynamodb:${Region}:${Account}:table/${TableName}') |
| 61 | + .parse('arn:aws:dynamodb:us-east-1:12345:table/MyTable'); |
| 62 | + expect(result).toEqual({ |
| 63 | + Partition: 'aws', |
| 64 | + Region: 'us-east-1', |
| 65 | + Account: '12345', |
| 66 | + TableName: 'MyTable', |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('interpolate', () => { |
| 72 | + it('interpolates template with single variable correctly', () => { |
| 73 | + const result = new TemplateString('Hello, ${name}!').interpolate({ name: 'John' }); |
| 74 | + expect(result).toBe('Hello, John!'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('interpolates template with multiple variables correctly', () => { |
| 78 | + const result = new TemplateString('My name is ${firstName} ${lastName}.').interpolate({ |
| 79 | + firstName: 'Jane', |
| 80 | + lastName: 'Doe', |
| 81 | + }); |
| 82 | + expect(result).toBe('My name is Jane Doe.'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('throws error when variable is missing in interpolation', () => { |
| 86 | + expect(() => { |
| 87 | + new TemplateString('Hello, ${name}!').interpolate({}); |
| 88 | + }).toThrow(UnscopedValidationError); |
| 89 | + }); |
| 90 | + |
| 91 | + it('interpolates template with no variables correctly', () => { |
| 92 | + const result = new TemplateString('Hello, world!').interpolate({}); |
| 93 | + expect(result).toBe('Hello, world!'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('throws error when template contains undefined variable', () => { |
| 97 | + expect(() => { |
| 98 | + new TemplateString('Hello, ${name}!').interpolate({ greeting: 'Hi' }); |
| 99 | + }).toThrow(UnscopedValidationError); |
| 100 | + }); |
| 101 | + |
| 102 | + it('interpolates template with adjacent variables correctly', () => { |
| 103 | + const result = new TemplateString('${greeting}, ${name}!').interpolate({ greeting: 'Hi', name: 'John' }); |
| 104 | + expect(result).toBe('Hi, John!'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('interpolates template with empty variable value correctly', () => { |
| 108 | + const result = new TemplateString('Hello, ${name}!').interpolate({ name: '' }); |
| 109 | + expect(result).toBe('Hello, !'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('interpolates template with variable at the start correctly', () => { |
| 113 | + const result = new TemplateString('${greeting}, world!').interpolate({ greeting: 'Hi' }); |
| 114 | + expect(result).toBe('Hi, world!'); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments