aboutsummaryrefslogtreecommitdiff
path: root/parser/parser_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'parser/parser_test.go')
-rw-r--r--parser/parser_test.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 7fe5268..df040ad 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -1088,3 +1088,50 @@ func checkParserErrors(t *testing.T, p *Parser) {
}
t.FailNow()
}
+
+func TestMacroLiteralParsing(t *testing.T) {
+ input := `macro(x, y) { x + y; }`
+
+ l := lexer.New(input)
+ p := New(l)
+ program := p.ParseProgram()
+ checkParserErrors(t, p)
+
+ if len(program.Statements) != 1 {
+ t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
+ 1, len(program.Statements))
+ }
+
+ stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
+ if !ok {
+ t.Fatalf("statement is not ast.ExpressionStatement. got=%T",
+ program.Statements[0])
+ }
+
+ macro, ok := stmt.Expression.(*ast.MacroLiteral)
+ if !ok {
+ t.Fatalf("stmt.Expression is not ast.MacroLiteral. got=%T",
+ stmt.Expression)
+ }
+
+ if len(macro.Parameters) != 2 {
+ t.Fatalf("macro literal parameters wrong. want 2, got=%d\n",
+ len(macro.Parameters))
+ }
+
+ testLiteralExpression(t, macro.Parameters[0], "x")
+ testLiteralExpression(t, macro.Parameters[1], "y")
+
+ if len(macro.Body.Statements) != 1 {
+ t.Fatalf("macro.Body.Statements has not 1 statements. got=%d\n",
+ len(macro.Body.Statements))
+ }
+
+ bodyStmt, ok := macro.Body.Statements[0].(*ast.ExpressionStatement)
+ if !ok {
+ t.Fatalf("macro body stmt is not ast.ExpressionStatement. got=%T",
+ macro.Body.Statements[0])
+ }
+
+ testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
+}