|
| 1 | +import { CommandInfo } from '../command'; |
| 2 | +import { combinations, ExpandMatrices } from './expand-matrices'; |
| 3 | + |
| 4 | +const createCommandInfo = (command: string): CommandInfo => ({ |
| 5 | + command, |
| 6 | + name: '', |
| 7 | +}); |
| 8 | + |
| 9 | +describe('ExpandMatrices', () => { |
| 10 | + it('should replace placeholders with matrix values', () => { |
| 11 | + const matrices = [ |
| 12 | + ['a', 'b'], |
| 13 | + ['1', '2'], |
| 14 | + ]; |
| 15 | + const expandMatrices = new ExpandMatrices(matrices); |
| 16 | + const commandInfo = createCommandInfo('echo {1} and {2}'); |
| 17 | + |
| 18 | + const result = expandMatrices.parse(commandInfo); |
| 19 | + |
| 20 | + expect(result).toEqual([ |
| 21 | + { command: 'echo a and 1', name: '' }, |
| 22 | + { command: 'echo a and 2', name: '' }, |
| 23 | + { command: 'echo b and 1', name: '' }, |
| 24 | + { command: 'echo b and 2', name: '' }, |
| 25 | + ]); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should handle escaped placeholders', () => { |
| 29 | + const matrices = [['a', 'b']]; |
| 30 | + const expandMatrices = new ExpandMatrices(matrices); |
| 31 | + const commandInfo = createCommandInfo('echo \\{1} and {1}'); |
| 32 | + |
| 33 | + const result = expandMatrices.parse(commandInfo); |
| 34 | + |
| 35 | + expect(result).toEqual([ |
| 36 | + { command: 'echo {1} and a', name: '' }, |
| 37 | + { command: 'echo {1} and b', name: '' }, |
| 38 | + ]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should replace placeholders with empty string if index is out of bounds', () => { |
| 42 | + const matrices = [['a']]; |
| 43 | + const expandMatrices = new ExpandMatrices(matrices); |
| 44 | + const commandInfo = createCommandInfo('echo {2}'); |
| 45 | + |
| 46 | + const result = expandMatrices.parse(commandInfo); |
| 47 | + |
| 48 | + expect(result).toEqual([{ command: 'echo ', name: '' }]); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('combinations', () => { |
| 53 | + it('should return all possible combinations of the given dimensions', () => { |
| 54 | + const dimensions = [ |
| 55 | + ['a', 'b'], |
| 56 | + ['1', '2'], |
| 57 | + ]; |
| 58 | + |
| 59 | + const result = combinations(dimensions); |
| 60 | + |
| 61 | + expect(result).toEqual([ |
| 62 | + ['a', '1'], |
| 63 | + ['a', '2'], |
| 64 | + ['b', '1'], |
| 65 | + ['b', '2'], |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should handle single dimension', () => { |
| 70 | + const dimensions = [['a', 'b']]; |
| 71 | + |
| 72 | + const result = combinations(dimensions); |
| 73 | + |
| 74 | + expect(result).toEqual([['a'], ['b']]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle empty dimensions', () => { |
| 78 | + const dimensions: string[][] = []; |
| 79 | + |
| 80 | + const result = combinations(dimensions); |
| 81 | + |
| 82 | + expect(result).toEqual([[]]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should handle dimensions with empty arrays', () => { |
| 86 | + const dimensions = [['a', 'b'], []]; |
| 87 | + |
| 88 | + const result = combinations(dimensions); |
| 89 | + |
| 90 | + expect(result).toEqual([]); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should handle dimensions with multiple empty arrays', () => { |
| 94 | + const dimensions = [[], []]; |
| 95 | + |
| 96 | + const result = combinations(dimensions); |
| 97 | + |
| 98 | + expect(result).toEqual([]); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle dimensions with some empty arrays', () => { |
| 102 | + const dimensions = [['a', 'b'], [], ['x', 'y']]; |
| 103 | + |
| 104 | + const result = combinations(dimensions); |
| 105 | + |
| 106 | + expect(result).toEqual([]); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should handle dimensions with all empty arrays', () => { |
| 110 | + const dimensions = [[], [], []]; |
| 111 | + |
| 112 | + const result = combinations(dimensions); |
| 113 | + |
| 114 | + expect(result).toEqual([]); |
| 115 | + }); |
| 116 | +}); |
0 commit comments