|
|
@@ -629,3 +629,33 @@ func TestFunctionLiteral(t *testing.T) {
|
|
|
}
|
|
|
testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
|
|
|
}
|
|
|
+
|
|
|
+func TestFunctionParameterParsing(t *testing.T) {
|
|
|
+ tests := []struct {
|
|
|
+ input string
|
|
|
+ expectedParams []string
|
|
|
+ }{
|
|
|
+ {input: "fn() {};", expectedParams: []string{}},
|
|
|
+ {input: "fn(x){};", expectedParams: []string{"x"}},
|
|
|
+ {input: "fn(x,y,z){};", expectedParams: []string{"x", "y", "z"}},
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, tt := range tests {
|
|
|
+ l := lexer.New(tt.input)
|
|
|
+ p := New(l)
|
|
|
+ program := p.ParseProgram()
|
|
|
+ checkParseErrors(t, p)
|
|
|
+
|
|
|
+ stmt := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
+ function := stmt.Expression.(*ast.FunctionLiteral)
|
|
|
+
|
|
|
+ if len(function.Parameters) != len(tt.expectedParams) {
|
|
|
+ t.Errorf("length parameters wrong. want %d, got=%d\n",
|
|
|
+ len(tt.expectedParams), len(function.Parameters))
|
|
|
+ }
|
|
|
+
|
|
|
+ for i, ident := range tt.expectedParams {
|
|
|
+ testLiteralExpression(t, function.Parameters[i], ident)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|