Created
February 22, 2024 18:48
-
-
Save robertleeplummerjr/7bfde41f06681c123fce6430c944bbed to your computer and use it in GitHub Desktop.
Revisions
-
robertleeplummerjr created this gist
Feb 22, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ export interface ITestCaseSubject { subject: string; } export interface IEachTestCase extends ITestCaseSubject { each: Array<() => any>; } export type ArrayMoreThanOne<T> = [T, T, ...T[]]; export function testPermutations(subjectCases: ArrayMoreThanOne<IEachTestCase>) { function traverseAll( fn: (subject: string, permutation: () => string, subjectIndex: number, permutationIndex: number) => void, ): void { for (let subjectIndex = 0; subjectIndex < subjectCases.length; subjectIndex++) { const subjectCase = subjectCases[subjectIndex]; const permutations = subjectCase.each; for (let permutationIndex = 0; permutationIndex < permutations.length; permutationIndex++) { fn(subjectCase.subject, permutations[permutationIndex], subjectIndex, permutationIndex); } } } function traverseAllBut( skipSubjectIndex: number, skipPermutationIndex: number, fn: (subject: string, permutation: () => string, subjectIndex: number, permutationIndex: number) => void, ): void { for (let subjectIndex = 0; subjectIndex < subjectCases.length; subjectIndex++) { if (subjectIndex === skipSubjectIndex) continue; const subjectCase = subjectCases[subjectIndex]; const permutations = subjectCase.each; for (let permutationIndex = 0; permutationIndex < permutations.length; permutationIndex++) { if (permutationIndex === skipPermutationIndex) continue; fn(subjectCase.subject, permutations[permutationIndex], subjectIndex, permutationIndex); } } } return (runTest: () => void | Promise<void>) => { let whenDescription: string[]; beforeEach(() => { whenDescription = []; traverseAll((subject, permutation, subjectIndex, permutationIndex) => { traverseAllBut(subjectIndex, permutationIndex, (relatedSubject, relatedPermutation) => { whenDescription.push(`${relatedSubject} = ${JSON.stringify(relatedPermutation())}`); }); }); }); // describe(`when ${whenDescription.join(", ")} and ${subject} = ${JSON.stringify(permutation())}`, () => { // beforeEach(permutation); // runTest(); // // it("returns true", async () => { // // expect(await runTest()).toBe(true); // // }); // }); }; }