Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export type {
TwoWayRule,
When,
} from "./crosswalk";
export { ruleSides } from "./sides";
export type { RuleSides } from "./sides";
export { CrosswalkStructureError, CrosswalkSyntaxError, RuleApplicationError, RoundTripMismatchError, CrossmapError } from "./errors";
export type { CrosswalkIssue, SyntaxIssue } from "./errors";
export { compileCrosswalk, decode, encode } from "./executor";
Expand Down
32 changes: 32 additions & 0 deletions src/sides.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { parseCrosswalk, ruleSides } from "./index";

/**
* Parses one two-way rule inside a crosswalk, so the rule under test is what the schema accepts.
* @param rule - The rule's document.
* @returns The rule as parsed.
*/
function parsed(rule: unknown) {
const [first] = parseCrosswalk({ source: "native", target: "shared", rules: [rule], oneWay: [] }).rules;
if (first === undefined) {
return expect.unreachable("the crosswalk has the one rule");
}

return first;
}

describe("ruleSides", () => {
it("gives the one path on each side of a rule that has one", () => {
expect(ruleSides(parsed({ op: "move", from: "meta", to: "info" }))).toEqual({ from: ["meta"], to: ["info"] });
expect(ruleSides(parsed({ op: "scale", from: "a", to: "b", factor: 2 }))).toEqual({ from: ["a"], to: ["b"] });
expect(ruleSides(parsed({ op: "split", from: "a", to: "b", separator: "," }))).toEqual({ from: ["a"], to: ["b"] });
});

it("gives a template's fields in its record's key order and a copy's every target", () => {
expect(ruleSides(parsed({ op: "template", from: { family: "people[].family", given: "people[].given" }, to: "names[]", template: "{family}, {given}" }))).toEqual({
from: ["people[].family", "people[].given"],
to: ["names[]"],
});
expect(ruleSides(parsed({ op: "copy", from: "items[].id", to: ["nodes[].id", "labels[].text"] }))).toEqual({ from: ["items[].id"], to: ["nodes[].id", "labels[].text"] });
});
});