-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathexpand-matrix.spec.ts
More file actions
130 lines (99 loc) · 3.82 KB
/
expand-matrix.spec.ts
File metadata and controls
130 lines (99 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { describe, expect, it } from 'vitest';
import { CommandInfo } from '../command';
import { combinations, ExpandMatrix } from './expand-matrix';
const createCommandInfo = (command: string): CommandInfo => ({
command,
name: '',
});
describe('ExpandMatrix', () => {
it('should replace placeholders with matrix values', () => {
const matrix = {
X: ['a', 'b'],
Y: ['1', '2'],
};
const expandMatrix = new ExpandMatrix(matrix);
const commandInfo = createCommandInfo('echo {M:X} and {M:Y}');
const result = expandMatrix.parse(commandInfo);
expect(result).toEqual([
{ command: 'echo a and 1', name: '' },
{ command: 'echo a and 2', name: '' },
{ command: 'echo b and 1', name: '' },
{ command: 'echo b and 2', name: '' },
]);
});
it('should handle escaped placeholders', () => {
const matrix = { X: ['a', 'b'] };
const expandMatrix = new ExpandMatrix(matrix);
const commandInfo = createCommandInfo('echo \\{M:X} and {M:X}');
const result = expandMatrix.parse(commandInfo);
expect(result).toEqual([
{ command: 'echo {M:X} and a', name: '' },
{ command: 'echo {M:X} and b', name: '' },
]);
});
it('throws SyntaxError if matrix name is invalid', () => {
const matrix = { X: ['a'] };
const expandMatrix = new ExpandMatrix(matrix);
const commandInfo = createCommandInfo('echo {M:INVALID}');
expect(() => expandMatrix.parse(commandInfo)).toThrowError(
"[concurrently] Matrix placeholder '{M:INVALID}' does not match any defined matrix.",
);
});
});
describe('combinations', () => {
it('should return all possible combinations of the given dimensions', () => {
const dimensions = {
X: ['a', 'b'],
Y: ['1', '2'],
};
const result = Array.from(combinations(dimensions));
expect(result).toEqual([
{ X: 'a', Y: '1' },
{ X: 'a', Y: '2' },
{ X: 'b', Y: '1' },
{ X: 'b', Y: '2' },
]);
});
it('should handle single dimension', () => {
const dimensions = { X: ['a', 'b'] };
const result = Array.from(combinations(dimensions));
const expected = [{ X: 'a' }, { X: 'b' }] as Record<string, string>[];
expect(result).toEqual(expected);
});
it('should handle empty dimensions', () => {
const dimensions: Record<string, string[]> = {};
const result = Array.from(combinations(dimensions));
expect(result).toEqual([]);
});
it('should handle dimensions with empty arrays', () => {
const dimensions = { X: ['a', 'b'], Y: [] };
const result = Array.from(combinations(dimensions));
expect(result).toEqual([]);
});
it('should handle dimensions with multiple empty arrays', () => {
const dimensions = { X: [], Y: [] };
const result = Array.from(combinations(dimensions));
expect(result).toEqual([]);
});
it('should handle dimensions with all empty arrays', () => {
const dimensions = { X: [], Y: [], Z: [] };
const result = Array.from(combinations(dimensions));
expect(result).toEqual([]);
});
it('should handle uneven dimensions', () => {
const dimensions = {
A: ['x'],
B: ['1', '2', '3'],
C: ['foo', 'bar'],
};
const result = Array.from(combinations(dimensions));
expect(result).toEqual([
{ A: 'x', B: '1', C: 'foo' },
{ A: 'x', B: '1', C: 'bar' },
{ A: 'x', B: '2', C: 'foo' },
{ A: 'x', B: '2', C: 'bar' },
{ A: 'x', B: '3', C: 'foo' },
{ A: 'x', B: '3', C: 'bar' },
]);
});
});