Skip to content

Commit

Permalink
add a few test about functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tengs Penkwe committed Feb 21, 2024
1 parent e0dfcc5 commit dfe5889
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
63 changes: 63 additions & 0 deletions test/ast/Func.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,67 @@ describe("transProgram", () => {
const result = program.eval(context);
expect(result.value).to.equal(6);
});

it("Should be able to define a function with no arguments", () => {
const text = `
def foo() -> Num {
return 1;
}
foo();
`;
const tree = parseProgram(text, false);
const program = transProgram(tree, false);
const context = new Context();
const result = program.eval(context);
expect(result.value).to.equal(1);
});

it("Should be able to define a function with multiple arguments", () => {
const text = `
def foo(x: Num, y: Num) -> Num {
return x + y;
}
foo(1, 2);
`;
const tree = parseProgram(text, false);
const program = transProgram(tree, false);
const context = new Context();
const result = program.eval(context);
expect(result.value).to.equal(3);
});

it("Should be able to define a function with multiple arguments and use them in a nested call", () => {
const text = `
def foo(x: Num, y: Num) -> Num {
return x + y;
}
def bar(x: Num, y: Num) -> Num {
return foo(x, y);
}
bar(1, 2);
`;
const tree = parseProgram(text, false);
const program = transProgram(tree, false);
const context = new Context();
const result = program.eval(context);
expect(result.value).to.equal(3);
});

it("Should be able to define a function with multiple arguments and use them in a nested call with a literal", () => {
const text = `
def foo(x: Num, y: Num) -> Num {
return x + y;
}
def bar(x: Num) -> Num {
return foo(x, 2);
}
bar(1);
`;
const tree = parseProgram(text, false);
const program = transProgram(tree, false);
const context = new Context();
const result = program.eval(context);
expect(result.value).to.equal(3);
});

});
4 changes: 2 additions & 2 deletions test/e2e/mainCLI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ describe("Farm DSL CLI", function () {
it("provides verbose output when requested", async () => {
const filePath = path.join(__dirname, "..", "..", "examples", "ex4.frm");
const result = await runCLI(`--file "${filePath}" --verbose`);
console.log(result);
expect(result).to
.include(`(prog (stmt (decl_stmt (type Farm) farm = (pairs [ (pair Name : (expr "myFarm")) , (pair Area : (expr 1200)) , (pair GridLength : (expr 10)) , (pair Polyculture : (expr true)) , (pair MaxWaterUsage : (expr 1500)) , (pair Season : (expr "Summer")) ]) ;)))
Program { stmts: [ Statement { stmt: [DeclStatment] } ] }`);
.include(`(decl_stmt (type Farm) farm = (pairs [ (pair Name : (expr "myFarm")) , (pair Height : (expr 10)) , (pair Width : (expr 10)) , (pair Polyculture : (expr true)) , (pair MaxWaterUsage : (expr 1500)) , (pair Season : (expr "Summer")) ]) ;)`);
});
});

0 comments on commit dfe5889

Please sign in to comment.