Skip to content

Commit

Permalink
added tests for prism
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrannell1 committed Jun 30, 2023
1 parent 628fa1f commit 3533fd3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/prism.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ Deno.test({
},
});

Deno.test({
name: "MaybeGroupMatch.view: view only shows the relevant focus",
fn() {
const firstFocusPrism = SubEdit.MaybeGroupMatch(/([0-9]+)-([0-9]+)/d, 1);
const secondFocusPrism = SubEdit.MaybeGroupMatch(/([0-9]+)-([0-9]+)/d, 2);

for (const [first, second] of Peach.Array.from(sampleNumberPairs, 100)()) {
const whole = `${first}-${second}`;

assertEquals(firstFocusPrism.view(whole), first);
assertEquals(secondFocusPrism.view(whole), second);
}
},
});



Deno.test({
name: "MaybeGroupMatch.set: if the pattern never matches, .set is idempotent",
Expand All @@ -141,3 +157,20 @@ Deno.test({
}
},
});

Deno.test({
name: "MaybeGroupMatch.set: set only updates the relevant focus",
fn() {
const firstFocusPrism = SubEdit.MaybeGroupMatch(/([0-9]+)-([0-9]+)/d, 1);
const secondFocusPrism = SubEdit.MaybeGroupMatch(/([0-9]+)-([0-9]+)/d, 2);

const samples = Peach.Array.concat(sampleNumbers, sampleNumbers, sampleNumbers, sampleNumbers)

for (const [first, second, firstPart, secondPart] of Peach.Array.from(samples, 100)()) {
const whole = `${first}-${second}`;

assertEquals(firstFocusPrism.set(firstPart, whole), `${firstPart}-${second}`);
assertEquals(secondFocusPrism.set(secondPart, whole), `${first}-${secondPart}`);
}
},
});
6 changes: 6 additions & 0 deletions src/prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export function MaybeMatch(pattern: RegExp) {
*
*/
export function MaybeGroupMatch(pattern: RegExp, index: number) {
if (pattern.flags.includes("g")) {
throw new Error(
"MaybeGroupMatch does not support the 'g' flag on the input pattern",
);
}

if (!pattern.flags.includes("d")) {
throw new Error(
"MaybeGroupMatch requires the 'd' flag to be set on the input pattern",
Expand Down

0 comments on commit 3533fd3

Please sign in to comment.