Skip to content

Commit

Permalink
tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ArduCrow committed Sep 12, 2023
1 parent 3df09be commit cb69b13
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
32 changes: 16 additions & 16 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestErrorHandling(t *testing.T) {
"unknown operator: STRING - STRING",
},
{
`{"name": "Monkey"}[fn(x) { x }];`,
`{"name": "Monkey"}[function(x) { x }];`,
"unusable as hash key: FUNCTION",
},
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestLetStatements(t *testing.T) {
}

func TestFunctionObject(t *testing.T) {
input := "fn(x) { x + 2; };"
input := "function(x) { x + 2; };"

evaluated := testEval(input)
fn, ok := evaluated.(*object.Function)
Expand All @@ -251,12 +251,12 @@ func TestFunctionApplication(t *testing.T) {
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) { x * 2; }; double(5);", 10},
{"let add = fn(x, y) { x + y; }; add(5, 5);", 10},
{"let add = fn(x, y) { x + y; }; add(5 + 5, add(5, 5));", 20},
{"fn(x) { x; }(5)", 5},
{"let identity = function(x) { x; }; identity(5);", 5},
{"let identity = function(x) { return x; }; identity(5);", 5},
{"let double = function(x) { x * 2; }; double(5);", 10},
{"let add = function(x, y) { x + y; }; add(5, 5);", 10},
{"let add = function(x, y) { x + y; }; add(5 + 5, add(5, 5));", 20},
{"function(x) { x; }(5)", 5},
}

for _, tt := range tests {
Expand All @@ -266,8 +266,8 @@ func TestFunctionApplication(t *testing.T) {

func TestClosures(t *testing.T) {
input := `
let newAdder = fn(x) {
fn(y) { x + y };
let newAdder = function(x) {
function(y) { x + y };
};
let addTwo = newAdder(2);
Expand Down Expand Up @@ -371,14 +371,14 @@ func TestArrayIndexExpressions(t *testing.T) {
// {"[1, 2, 3][false]", nil},
// {"[1, 2, 3][null]", nil},
// {"[1, 2, 3][\"string\"]", nil},
// {"[1, 2, 3][fn(x) { x }]", nil},
// {"[1, 2, 3][function(x) { x }]", nil},
// {"[1, 2, 3][[1, 2, 3]]", nil},
// {"[1, 2, 3][{\"key\": \"value\"}]", nil},
// {"[1, 2, 3][fn(x) { x }()]", nil},
// {"[1, 2, 3][fn(x) { x }(1)]", nil},
// {"[1, 2, 3][fn(x) { x }(1, 2)]", nil},
// {"[1, 2, 3][fn(x) { x }(1, 2, 3)]", nil},
// {"[1, 2, 3][fn(x) { x }(1, 2, 3, 4)]", nil},
// {"[1, 2, 3][function(x) { x }()]", nil},
// {"[1, 2, 3][function(x) { x }(1)]", nil},
// {"[1, 2, 3][function(x) { x }(1, 2)]", nil},
// {"[1, 2, 3][function(x) { x }(1, 2, 3)]", nil},
// {"[1, 2, 3][function(x) { x }(1, 2, 3, 4)]", nil},
}

for _, tt := range tests {
Expand Down
6 changes: 3 additions & 3 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestNextToken(t *testing.T) {
input := `let five = 5;
let ten = 10;
let add = fn(x, y) {
let add = function(x, y) {
x + y;
};
let result = add(five, ten);
Expand All @@ -26,7 +26,7 @@ func TestNextToken(t *testing.T) {
"foobar"
"foo bar"
[1, 2];
{"foo": "bar}
{"foo": "bar"}
`

tests := []struct {
Expand All @@ -46,7 +46,7 @@ func TestNextToken(t *testing.T) {
{token.LET, "let"},
{token.IDENT, "add"},
{token.ASSIGN, "="},
{token.FUNCTION, "fn"},
{token.FUNCTION, "function"},
{token.LPAREN, "("},
{token.IDENT, "x"},
{token.COMMA, ","},
Expand Down
8 changes: 4 additions & 4 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func TestIfElseExpression(t *testing.T) {
}

func TestFunctionLiteralParsing(t *testing.T) {
input := `fn(x, y) { x + y }`
input := `function(x, y) { x + y }`

l := lexer.New(input)
p := New(l)
Expand Down Expand Up @@ -559,9 +559,9 @@ func TestFunctionParameterParsing(t *testing.T) {
input string
expectedParams []string
}{
{input: "fn() {};", expectedParams: []string{}},
{input: "fn(x) {};", expectedParams: []string{"x"}},
{input: "fn(x, y, z) {};", expectedParams: []string{"x", "y", "z"}},
{input: "function() {};", expectedParams: []string{}},
{input: "function(x) {};", expectedParams: []string{"x"}},
{input: "function(x, y, z) {};", expectedParams: []string{"x", "y", "z"}},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ REPL Session: main
### Example

```javascript
let tryThis = fn(foo, bar) {
let tryThis = function(foo, bar) {
let result = foo * bar + 10;
return result
}
Expand Down

0 comments on commit cb69b13

Please sign in to comment.