Skip to content

Commit

Permalink
test: add test of spread
Browse files Browse the repository at this point in the history
  • Loading branch information
iyxan23 committed Sep 20, 2024
1 parent e009de1 commit befd978
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions src/sheet/expression/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,45 @@ describe("expression evaluation", () => {
expect(consoleWarnMock).toHaveBeenCalledOnce();
});

it("spreads arrays correctly", (test) => {
const consoleWarnMock = mockWarn();
test.onTestFinished(() => consoleWarnMock.mockRestore());

// [call ...[:array]]
const expr: Expression = {
type: "call",
identifier: "call",
args: [
{
type: "spread",
expr: {
type: "variableAccess",
identifier: "array",
args: [],
},
},
],
};

const result = evaluateExpression(
expr,
{ col: 0, row: 0, callTree: ["root"] },
(fName) =>
fName === "call"
? (_funcName, ...args) => success("called: " + JSON.stringify(args))
: undefined,
(vName) => (vName === "array" ? ["hello", "world"] : undefined),
);

if (result.status === "failed") {
throw new Error(JSON.stringify(result));
}

expect(result.issues).toHaveLength(0);
expect(result.result).toEqual('called: ["hello","world"]');
expect(consoleWarnMock).toHaveBeenCalledTimes(0);
});

it("returns a function call when calling a lambda", (test) => {
const consoleWarnMock = mockWarn();
test.onTestFinished(() => consoleWarnMock.mockRestore());
Expand Down Expand Up @@ -247,14 +286,14 @@ describe("expression evaluation", () => {
(fName) =>
fName === "call"
? createTemplaterFunction(z.tuple([z.function()]), (s) => {
return s((vName: string) =>
vName === "world" ? "people!" : undefined,
);
})
return s((vName: string) =>
vName === "world" ? "people!" : undefined,
);
})
: fName === "ret"
? createTemplaterFunction(z.tuple([z.string()]), (s) => {
return success(`ret ${s}`);
})
return success(`ret ${s}`);
})
: undefined,
(_vName) => undefined,
);
Expand Down Expand Up @@ -407,19 +446,19 @@ describe("expression evaluation", () => {
(vName) =>
vName === "students"
? [
{
fullName: "John Doe",
},
{
fullName: "Jane Doe",
},
{
fullName: "Mark Doe",
},
{
fullName: "Mary Doe",
},
]
{
fullName: "John Doe",
},
{
fullName: "Jane Doe",
},
{
fullName: "Mark Doe",
},
{
fullName: "Mary Doe",
},
]
: undefined,
);

Expand Down

0 comments on commit befd978

Please sign in to comment.