Skip to content

Commit

Permalink
Added unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrannell1 committed Jun 30, 2023
1 parent 0a3763d commit b6b2ce6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/prisms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { AbstractPrism, Prism } from "./types.ts";

/**
* Static methods for prisms
*
*/
export class Prisms {

/**
* Construct a prism that unions a series of prisms.
*
* - view: returns the first non-null view of the prism series
* - set: sets the value of the first prism that has a non-null view
*
*/
static union<Whole, Part>(...prisms: Prism<Whole, Part>[]) {
return new class extends AbstractPrism<Whole, Part> {
Expand Down
49 changes: 49 additions & 0 deletions src/traversals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as Peach from "../../peach.ts/src/mod.ts";
import * as SubEdit from "./mod.ts";
import { assertEquals } from "https://deno.land/std@0.171.0/testing/asserts.ts";

import { Traversals } from "./traversals.ts";

const sampleText = Peach.String.from(
Peach.String.blocks.myanmarExtendedA(Peach.Number.uniform),
Peach.Number.uniform(0, 100),
);

Deno.test({
name: "Traversals.union.view: union() == []",
fn() {
const unioned = Traversals.union();

for (const tcase of Peach.Array.from(sampleText, 100)()) {
assertEquals(unioned.view(tcase), []);
}
},
});

Deno.test({
name: "Traversals.union.view: union(traversal) ~ traversal",
fn() {
const traversal = SubEdit.EachMatch(/.{3}/gd);
const unioned = Traversals.union(traversal);

for (const tcase of Peach.Array.from(sampleText, 100)()) {
assertEquals(unioned.view(tcase), traversal.view(tcase));
}
},
});

Deno.test({
name:
"Traversals.union.view: union(traversal, traversal) ~ traversal + traversal",
fn() {
const traversal = SubEdit.EachMatch(/.{3}/gd);
const unioned = Traversals.union(traversal, traversal);

for (const tcase of Peach.Array.from(sampleText, 100)()) {
assertEquals(
unioned.view(tcase),
traversal.view(tcase).concat(traversal.view(tcase)),
);
}
},
});
3 changes: 0 additions & 3 deletions src/traversals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { AbstractTraversal, Traversal } from "./types.ts";

/**
* Static methods for traversals
*
*/
export class Traversals {

/**
* Construct a traversal that joins the views of multiple traversals.
*
* - view: returns the concatenation of the views of each traversal
* - modify: applies the modifier to the view of each traversal
*
*/
static union<Whole, Part>(...traversals: Traversal<Whole, Part>[]) {
return new class extends AbstractTraversal<Whole, Part> {
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface Prism<Whole, Part> {

/*
* Partial implementation of the prism interface, leaving view and set abstract
*
*/
export abstract class AbstractPrism<Whole, Part> implements Prism<Whole, Part> {
abstract view(whole: Whole): Part | null;
Expand Down Expand Up @@ -84,7 +83,6 @@ export interface Traversal<Whole, Part> {

/*
* Partial implementation of the traversal interface, leaving view and modify abstract
*
*/
export abstract class AbstractTraversal<Whole, Part>
implements Traversal<Whole, Part> {
Expand Down

0 comments on commit b6b2ce6

Please sign in to comment.