run functions
Some checks failed
Build / build (push) Failing after 1m36s
Test / build (push) Successful in 1m37s

This commit is contained in:
Chuck Smith
2024-03-04 16:11:25 -05:00
parent e56fb40f83
commit 9d06c90e41
2 changed files with 184 additions and 21 deletions

View File

@@ -296,3 +296,93 @@ func TestIndexExpressions(t *testing.T) {
runVmTests(t, tests)
}
func TestCallingFunctionsWithoutArguments(t *testing.T) {
tests := []vmTestCase{
{
input: `
let fivePlusTen = fn() { 5 + 10; };
fivePlusTen();
`,
expected: 15,
},
{
input: `
let one = fn() { 1; };
let two = fn() { 2; };
one() + two()
`,
expected: 3,
},
{
input: `
let a = fn() { 1 };
let b = fn() { a() + 1 };
let c = fn() { b() + 1 };
c();
`,
expected: 3,
},
}
runVmTests(t, tests)
}
func TestFunctionsWithReturnStatements(t *testing.T) {
tests := []vmTestCase{
{
input: `
let earlyExit = fn() { return 99; 100; };
earlyExit();
`,
expected: 99,
},
{
input: `
let earlyExit = fn() { return 99; return 100; };
earlyExit();
`,
expected: 99,
},
}
runVmTests(t, tests)
}
func TestFunctionsWithoutReturnValue(t *testing.T) {
tests := []vmTestCase{
{
input: `
let noReturn = fn() { };
noReturn();
`,
expected: Null,
},
{
input: `
let noReturn = fn() { };
let noReturnTwo = fn() { noReturn(); };
noReturn();
noReturnTwo();
`,
expected: Null,
},
}
runVmTests(t, tests)
}
func TestFirstClassFunctions(t *testing.T) {
tests := []vmTestCase{
{
input: `
let returnsOne = fn() { 1; };
let returnsOneReturner = fn() { returnsOne; };
returnsOneReturner()();
`,
expected: 1,
},
}
runVmTests(t, tests)
}