From dfe588926c6cd758e4ce4be8aa48c23a3768d016 Mon Sep 17 00:00:00 2001 From: Tengs Penkwe Date: Tue, 20 Feb 2024 20:11:50 -0800 Subject: [PATCH] add a few test about functions --- test/ast/Func.spec.ts | 63 ++++++++++++++++++++++++++++++++++++++++ test/e2e/mainCLI.spec.ts | 4 +-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/test/ast/Func.spec.ts b/test/ast/Func.spec.ts index 3407f30..60646df 100644 --- a/test/ast/Func.spec.ts +++ b/test/ast/Func.spec.ts @@ -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); + }); + }); diff --git a/test/e2e/mainCLI.spec.ts b/test/e2e/mainCLI.spec.ts index 578892a..96fc8c5 100644 --- a/test/e2e/mainCLI.spec.ts +++ b/test/e2e/mainCLI.spec.ts @@ -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")) ]) ;)`); }); });