|
|
|
|
@ -503,50 +503,58 @@ func TestIfElseExpression(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFunctionLiteralParsing(t *testing.T) {
|
|
|
|
|
input := `fn(x, y) { x + y; }`
|
|
|
|
|
tests := []struct {
|
|
|
|
|
input string
|
|
|
|
|
}{
|
|
|
|
|
{"fn(x, y) { x + y; }"},
|
|
|
|
|
{"fn(x, y) -> x + y;"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l := lexer.New(input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Body does not contain %d statements. got=%d\n",
|
|
|
|
|
1, len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
l := lexer.New(tt.input)
|
|
|
|
|
p := New(l)
|
|
|
|
|
program := p.ParseProgram()
|
|
|
|
|
checkParserErrors(t, p)
|
|
|
|
|
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
if len(program.Statements) != 1 {
|
|
|
|
|
t.Fatalf("program.Body does not contain %d statements. got=%d\n",
|
|
|
|
|
1, len(program.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function, ok := stmt.Expression.(*ast.FunctionLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("stmt.Expression is not ast.FunctionLiteral. got=%T",
|
|
|
|
|
stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
program.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(function.Parameters) != 2 {
|
|
|
|
|
t.Fatalf("function literal parameters wrong. want 2, got=%d\n",
|
|
|
|
|
len(function.Parameters))
|
|
|
|
|
}
|
|
|
|
|
function, ok := stmt.Expression.(*ast.FunctionLiteral)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("stmt.Expression is not ast.FunctionLiteral. got=%T",
|
|
|
|
|
stmt.Expression)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testLiteralExpression(t, function.Parameters[0], "x")
|
|
|
|
|
testLiteralExpression(t, function.Parameters[1], "y")
|
|
|
|
|
if len(function.Parameters) != 2 {
|
|
|
|
|
t.Fatalf("function literal parameters wrong. want 2, got=%d\n",
|
|
|
|
|
len(function.Parameters))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(function.Body.Statements) != 1 {
|
|
|
|
|
t.Fatalf("function.Body.Statements has not 1 statements. got=%d\n",
|
|
|
|
|
len(function.Body.Statements))
|
|
|
|
|
}
|
|
|
|
|
testLiteralExpression(t, function.Parameters[0], "x")
|
|
|
|
|
testLiteralExpression(t, function.Parameters[1], "y")
|
|
|
|
|
|
|
|
|
|
bodyStmt, ok := function.Body.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("function body stmt is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
function.Body.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
if len(function.Body.Statements) != 1 {
|
|
|
|
|
t.Fatalf("function.Body.Statements has not 1 statements. got=%d\n",
|
|
|
|
|
len(function.Body.Statements))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
|
|
|
|
|
bodyStmt, ok := function.Body.Statements[0].(*ast.ExpressionStatement)
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("function body stmt is not ast.ExpressionStatement. got=%T",
|
|
|
|
|
function.Body.Statements[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFunctionParameterParsing(t *testing.T) {
|
|
|
|
|
|