Skip to content

Commit

Permalink
test: adds support for function evaluation
Browse files Browse the repository at this point in the history
Signed-off-by: maxwellgithinji <[email protected]>
  • Loading branch information
maxwellgithinji committed Feb 2, 2024
1 parent cc93dc4 commit d108ead
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,34 @@ func TestFunctionObject(t *testing.T) {
t.Fatalf("function.Body is not %q, got: %q", expectedBody, function.Body.String())
}
}

func TestFunctionApplication(t *testing.T) {
tests := []struct {
input string
expected int64
}{
{"let identity = fn(x) {x}; identity(5);", 5},
{"let identity = fn(x) {return x}; identity(5);", 5},
{"let double = fn(x) {return x * 2}; double(5);", 10},
{"let add = fn(x, y) {return x + y}; add(5, 5);", 10},
{"let add = fn(x, y) {return x + y}; add(5 + 5, add(5, 5));", 20},
{"fn(x) { x; }(5)", 5},
}

for _, tt := range tests {
testIntegerObject(t, testEval(tt.input), tt.expected)
}
}

func TestClosures(t *testing.T) {
input := `
let newAdder = fn(x) {
fn(y) {x + y };
};
let addTwo = newAdder(2);
addTwo(2);
`

testIntegerObject(t, testEval(input), 4)
}

0 comments on commit d108ead

Please sign in to comment.