-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathexamples.spec.ts
100 lines (84 loc) · 2.68 KB
/
examples.spec.ts
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { strict as assert } from "assert";
import { OptionsMatrix, generatePairwiseOptions } from "../index.js";
describe("generatePairwiseOptions.examples", () => {
it("Testing a function", () => {
const myFunction = (param1: boolean, param2: "string1" | "string2", param3?: number) => {};
const options = generatePairwiseOptions<{
p1: boolean;
p2: "string1" | "string2";
p3?: number;
}>({
p1: [true, false],
p2: ["string1", "string2"],
p3: [undefined, 0, 10, 100],
});
for (const option of options) {
myFunction(option.p1, option.p2, option.p3);
}
});
it("Testing an object", () => {
interface MyObject {
prop1: boolean;
prop2?: number;
prop3?: string;
}
const myObjectMatrix: OptionsMatrix<MyObject> = {
prop1: [true, false],
prop2: [undefined, 37, 242],
prop3: [undefined],
};
const myObjects = generatePairwiseOptions<MyObject>(myObjectMatrix);
// use the options to drive a scenario
const runScenario = (instance: MyObject) => {};
for (const instance of myObjects) {
runScenario(instance);
}
});
it("Generate a fixed length Array", () => {
const arrayMatrix: OptionsMatrix<ArrayLike<number>> = {
0: [3, 6, 9, 12, 15],
1: [7, 14, 28],
length: [2],
};
const myArrayLikes = generatePairwiseOptions<ArrayLike<number>>(arrayMatrix);
// use the array to drive a scenario
const runScenario = (numbers: number[]) => {
assert.strictEqual(numbers.length, 2);
assert(numbers[0] % 3 === 0);
assert(numbers[1] % 7 === 0);
};
for (const arrayLike of myArrayLikes) {
runScenario(Array.from(arrayLike));
}
});
it("Generate an Complex object using nested options matrices", () => {
const arrayMatrix: OptionsMatrix<ArrayLike<number>> = {
0: [3, 6, 9, 12, 15],
1: [7, 14, 28],
length: [2],
};
interface MyComplexObject {
numbers?: number[];
subObject: { str: string };
}
// in this example we generate pairwise options for keys on the main object to
// create the values which will then be pairwise matched with eachother
const complexObjectMatrix: OptionsMatrix<MyComplexObject> = {
numbers: [
undefined,
...generatePairwiseOptions<ArrayLike<number>>(arrayMatrix).map((a) => Array.from(a)),
],
subObject: generatePairwiseOptions<{ str: string }>({ str: ["a", "b", "c"] }),
};
const complexObjects = generatePairwiseOptions<MyComplexObject>(complexObjectMatrix);
// use the array to drive a scenario
const runScenario = (complexObject: MyComplexObject) => {};
for (const complexObject of complexObjects) {
runScenario(complexObject);
}
});
});