|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { interpolateString } from './interpolateString'; |
| 3 | + |
| 4 | +describe('interpolateString', () => { |
| 5 | + it('should replace single variable with provided value', () => { |
| 6 | + const result = interpolateString('Hello {name}', { name: 'World' }); |
| 7 | + expect(result).toBe('Hello World'); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should replace multiple variables with provided values', () => { |
| 11 | + const result = interpolateString( |
| 12 | + 'Server {serverId} at {rconAddress}:{hostPort}', |
| 13 | + { serverId: 'srv-123', rconAddress: '192.168.1.1', hostPort: 27015 } |
| 14 | + ); |
| 15 | + expect(result).toBe('Server srv-123 at 192.168.1.1:27015'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should handle repeated variables', () => { |
| 19 | + const result = interpolateString( |
| 20 | + '{value} and {value}', |
| 21 | + { value: 'test' } |
| 22 | + ); |
| 23 | + expect(result).toBe('test and test'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should leave unmapped variables unchanged', () => { |
| 27 | + const result = interpolateString( |
| 28 | + 'Value: {exists}, Missing: {notExists}', |
| 29 | + { exists: 'found' } |
| 30 | + ); |
| 31 | + expect(result).toBe('Value: found, Missing: {notExists}'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should handle empty template', () => { |
| 35 | + const result = interpolateString('', { key: 'value' }); |
| 36 | + expect(result).toBe(''); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should handle template with no variables', () => { |
| 40 | + const result = interpolateString('No variables here', { key: 'value' }); |
| 41 | + expect(result).toBe('No variables here'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should handle empty data object', () => { |
| 45 | + const result = interpolateString('Hello {name}', {}); |
| 46 | + expect(result).toBe('Hello {name}'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should convert non-string values to strings', () => { |
| 50 | + const result = interpolateString( |
| 51 | + 'Player count: {count}, Status: {ready}', |
| 52 | + { count: 24, ready: true } |
| 53 | + ); |
| 54 | + expect(result).toBe('Player count: 24, Status: true'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle multiline templates with variables', () => { |
| 58 | + const result = interpolateString( |
| 59 | + 'Server: {serverId}\nAddress: {rconAddress}:{hostPort}\nPassword: {rconPassword}', |
| 60 | + { |
| 61 | + serverId: 'srv-001', |
| 62 | + rconAddress: '10.0.0.1', |
| 63 | + hostPort: 27015, |
| 64 | + rconPassword: 'secret123' |
| 65 | + } |
| 66 | + ); |
| 67 | + expect(result).toBe( |
| 68 | + 'Server: srv-001\nAddress: 10.0.0.1:27015\nPassword: secret123' |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should handle nested braces correctly', () => { |
| 73 | + const result = interpolateString( |
| 74 | + 'Data: {key}', |
| 75 | + { key: '{nested}' } |
| 76 | + ); |
| 77 | + expect(result).toBe('Data: {nested}'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should handle variables with underscores and numbers', () => { |
| 81 | + const result = interpolateString( |
| 82 | + 'Values: {var_1}, {var2_name}', |
| 83 | + { var_1: 'first', var2_name: 'second' } |
| 84 | + ); |
| 85 | + expect(result).toBe('Values: first, second'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments