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.go173
1 files changed, 64 insertions, 109 deletions
diff --git a/parser/parser_test.go b/parser/parser_test.go
index df040ad..7b91214 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -12,7 +12,7 @@ func TestLetStatements(t *testing.T) {
tests := []struct {
input string
expectedIdentifier string
- expectedValue interface{}
+ expectedValue any
}{
{"let x = 5;", "x", 5},
{"let y = true;", "y", true},
@@ -26,8 +26,7 @@ func TestLetStatements(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain 1 statements. got=%d",
- len(program.Statements))
+ t.Fatalf("program.Statements does not contain 1 statements. got=%d", len(program.Statements))
}
stmt := program.Statements[0]
@@ -45,7 +44,7 @@ func TestLetStatements(t *testing.T) {
func TestReturnStatements(t *testing.T) {
tests := []struct {
input string
- expectedValue interface{}
+ expectedValue any
}{
{"return 5;", 5},
{"return true;", true},
@@ -59,8 +58,7 @@ func TestReturnStatements(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain 1 statements. got=%d",
- len(program.Statements))
+ t.Fatalf("program.Statements does not contain 1 statements. got=%d", len(program.Statements))
}
stmt := program.Statements[0]
@@ -69,8 +67,7 @@ func TestReturnStatements(t *testing.T) {
t.Fatalf("stmt not *ast.returnStatement. got=%T", stmt)
}
if returnStmt.TokenLiteral() != "return" {
- t.Fatalf("returnStmt.TokenLiteral not 'return', got %q",
- returnStmt.TokenLiteral())
+ t.Fatalf("returnStmt.TokenLiteral not 'return', got %q", returnStmt.TokenLiteral())
}
if testLiteralExpression(t, returnStmt.ReturnValue, tt.expectedValue) {
return
@@ -87,13 +84,11 @@ func TestIdentifierExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program has not enough statements. got=%d",
- len(program.Statements))
+ t.Fatalf("program has not enough statements. got=%d", len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
ident, ok := stmt.Expression.(*ast.Identifier)
@@ -104,8 +99,7 @@ func TestIdentifierExpression(t *testing.T) {
t.Errorf("ident.Value not %s. got=%s", "foobar", ident.Value)
}
if ident.TokenLiteral() != "foobar" {
- t.Errorf("ident.TokenLiteral not %s. got=%s", "foobar",
- ident.TokenLiteral())
+ t.Errorf("ident.TokenLiteral not %s. got=%s", "foobar", ident.TokenLiteral())
}
}
@@ -118,13 +112,11 @@ func TestIntegerLiteralExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program has not enough statements. got=%d",
- len(program.Statements))
+ t.Fatalf("program has not enough statements. got=%d", len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
literal, ok := stmt.Expression.(*ast.IntegerLiteral)
@@ -135,8 +127,7 @@ func TestIntegerLiteralExpression(t *testing.T) {
t.Errorf("literal.Value not %d. got=%d", 5, literal.Value)
}
if literal.TokenLiteral() != "5" {
- t.Errorf("literal.TokenLiteral not %s. got=%s", "5",
- literal.TokenLiteral())
+ t.Errorf("literal.TokenLiteral not %s. got=%s", "5", literal.TokenLiteral())
}
}
@@ -144,7 +135,7 @@ func TestParsingPrefixExpressions(t *testing.T) {
prefixTests := []struct {
input string
operator string
- value interface{}
+ value any
}{
{"!5;", "!", 5},
{"-15;", "-", 15},
@@ -161,14 +152,12 @@ func TestParsingPrefixExpressions(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Statements does not contain %d statements. got=%d", 1, len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
exp, ok := stmt.Expression.(*ast.PrefixExpression)
@@ -176,8 +165,7 @@ func TestParsingPrefixExpressions(t *testing.T) {
t.Fatalf("stmt is not ast.PrefixExpression. got=%T", stmt.Expression)
}
if exp.Operator != tt.operator {
- t.Fatalf("exp.Operator is not '%s'. got=%s",
- tt.operator, exp.Operator)
+ t.Fatalf("exp.Operator is not '%s'. got=%s", tt.operator, exp.Operator)
}
if !testLiteralExpression(t, exp.Right, tt.value) {
return
@@ -188,9 +176,9 @@ func TestParsingPrefixExpressions(t *testing.T) {
func TestParsingInfixExpressions(t *testing.T) {
infixTests := []struct {
input string
- leftValue interface{}
+ leftValue any
operator string
- rightValue interface{}
+ rightValue any
}{
{"5 + 5;", 5, "+", 5},
{"5 - 5;", 5, "-", 5},
@@ -220,18 +208,15 @@ func TestParsingInfixExpressions(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Statements does not contain %d statements. got=%d", 1, len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
- if !testInfixExpression(t, stmt.Expression, tt.leftValue,
- tt.operator, tt.rightValue) {
+ if !testInfixExpression(t, stmt.Expression, tt.leftValue, tt.operator, tt.rightValue) {
return
}
}
@@ -385,14 +370,12 @@ func TestBooleanExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program has not enough statements. got=%d",
- len(program.Statements))
+ t.Fatalf("program has not enough statements. got=%d", len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
boolean, ok := stmt.Expression.(*ast.Boolean)
@@ -400,8 +383,7 @@ func TestBooleanExpression(t *testing.T) {
t.Fatalf("exp not *ast.Boolean. got=%T", stmt.Expression)
}
if boolean.Value != tt.expectedBoolean {
- t.Errorf("boolean.Value not %t. got=%t", tt.expectedBoolean,
- boolean.Value)
+ t.Errorf("boolean.Value not %t. got=%t", tt.expectedBoolean, boolean.Value)
}
}
}
@@ -415,20 +397,17 @@ func TestIfExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Body does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Body does not contain %d statements. got=%d", 1, len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
exp, ok := stmt.Expression.(*ast.IfExpression)
if !ok {
- t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T",
- stmt.Expression)
+ t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T", stmt.Expression)
}
if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
@@ -436,14 +415,12 @@ func TestIfExpression(t *testing.T) {
}
if len(exp.Consequence.Statements) != 1 {
- t.Errorf("consequence is not 1 statements. got=%d\n",
- len(exp.Consequence.Statements))
+ t.Errorf("consequence is not 1 statements. got=%d", len(exp.Consequence.Statements))
}
consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
- exp.Consequence.Statements[0])
+ t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T", exp.Consequence.Statements[0])
}
if !testIdentifier(t, consequence.Expression, "x") {
@@ -464,14 +441,12 @@ func TestIfElseExpression(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Body does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Body does not contain %d statements. got=%d", 1, len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
exp, ok := stmt.Expression.(*ast.IfExpression)
@@ -484,14 +459,12 @@ func TestIfElseExpression(t *testing.T) {
}
if len(exp.Consequence.Statements) != 1 {
- t.Errorf("consequence is not 1 statements. got=%d\n",
- len(exp.Consequence.Statements))
+ t.Errorf("consequence is not 1 statements. got=%d", len(exp.Consequence.Statements))
}
consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
- exp.Consequence.Statements[0])
+ t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T", exp.Consequence.Statements[0])
}
if !testIdentifier(t, consequence.Expression, "x") {
@@ -499,14 +472,12 @@ func TestIfElseExpression(t *testing.T) {
}
if len(exp.Alternative.Statements) != 1 {
- t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d\n",
- len(exp.Alternative.Statements))
+ t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d", len(exp.Alternative.Statements))
}
alternative, ok := exp.Alternative.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
- exp.Alternative.Statements[0])
+ t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T", exp.Alternative.Statements[0])
}
if !testIdentifier(t, alternative.Expression, "y") {
@@ -523,39 +494,33 @@ func TestFunctionLiteralParsing(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Body does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Body does not contain %d statements. got=%d", 1, len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
function, ok := stmt.Expression.(*ast.FunctionLiteral)
if !ok {
- t.Fatalf("stmt.Expression is not ast.FunctionLiteral. got=%T",
- stmt.Expression)
+ t.Fatalf("stmt.Expression is not ast.FunctionLiteral. got=%T", stmt.Expression)
}
if len(function.Parameters) != 2 {
- t.Fatalf("function literal parameters wrong. want 2, got=%d\n",
- len(function.Parameters))
+ t.Fatalf("function literal parameters wrong. want 2, got=%d", len(function.Parameters))
}
testLiteralExpression(t, function.Parameters[0], "x")
testLiteralExpression(t, function.Parameters[1], "y")
if len(function.Body.Statements) != 1 {
- t.Fatalf("function.Body.Statements has not 1 statements. got=%d\n",
- len(function.Body.Statements))
+ t.Fatalf("function.Body.Statements has not 1 statements. got=%d", len(function.Body.Statements))
}
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])
+ t.Fatalf("function body stmt is not ast.ExpressionStatement. got=%T", function.Body.Statements[0])
}
testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
@@ -581,8 +546,7 @@ func TestFunctionParameterParsing(t *testing.T) {
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))
+ t.Errorf("length parameters wrong. want %d, got=%d", len(tt.expectedParams), len(function.Parameters))
}
for i, ident := range tt.expectedParams {
@@ -600,20 +564,17 @@ func TestCallExpressionParsing(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Statements does not contain %d statements. got=%d", 1, len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
- t.Fatalf("stmt is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
+ t.Fatalf("stmt is not ast.ExpressionStatement. got=%T", program.Statements[0])
}
exp, ok := stmt.Expression.(*ast.CallExpression)
if !ok {
- t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T",
- stmt.Expression)
+ t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T", stmt.Expression)
}
if !testIdentifier(t, exp.Function, "add") {
@@ -661,8 +622,7 @@ func TestCallExpressionParameterParsing(t *testing.T) {
stmt := program.Statements[0].(*ast.ExpressionStatement)
exp, ok := stmt.Expression.(*ast.CallExpression)
if !ok {
- t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T",
- stmt.Expression)
+ t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T", stmt.Expression)
}
if !testIdentifier(t, exp.Function, tt.expectedIdent) {
@@ -670,14 +630,12 @@ func TestCallExpressionParameterParsing(t *testing.T) {
}
if len(exp.Arguments) != len(tt.expectedArgs) {
- t.Fatalf("wrong number of arguments. want=%d, got=%d",
- len(tt.expectedArgs), len(exp.Arguments))
+ t.Fatalf("wrong number of arguments. want=%d, got=%d", len(tt.expectedArgs), len(exp.Arguments))
}
for i, arg := range tt.expectedArgs {
if exp.Arguments[i].String() != arg {
- t.Errorf("argument %d wrong. want=%q, got=%q", i,
- arg, exp.Arguments[i].String())
+ t.Errorf("argument %d wrong. want=%q, got=%q", i, arg, exp.Arguments[i].String())
}
}
}
@@ -951,6 +909,7 @@ func TestParsingHashLiteralsWithExpressions(t *testing.T) {
}
func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
+ t.Helper()
if s.TokenLiteral() != "let" {
t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
return false
@@ -975,7 +934,8 @@ func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
return true
}
-func testInfixExpression(t *testing.T, exp ast.Expression, left interface{}, operator string, right interface{}) bool {
+func testInfixExpression(t *testing.T, exp ast.Expression, left any, operator string, right any) bool {
+ t.Helper()
opExp, ok := exp.(*ast.InfixExpression)
if !ok {
t.Errorf("exp is not ast.OperatorExpression. got=%T(%s)", exp, exp)
@@ -998,7 +958,8 @@ func testInfixExpression(t *testing.T, exp ast.Expression, left interface{}, ope
return true
}
-func testLiteralExpression(t *testing.T, exp ast.Expression, expected interface{}) bool {
+func testLiteralExpression(t *testing.T, exp ast.Expression, expected any) bool {
+ t.Helper()
switch v := expected.(type) {
case int:
return testIntegerLiteral(t, exp, int64(v))
@@ -1014,6 +975,7 @@ func testLiteralExpression(t *testing.T, exp ast.Expression, expected interface{
}
func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
+ t.Helper()
integ, ok := il.(*ast.IntegerLiteral)
if !ok {
t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
@@ -1026,8 +988,7 @@ func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
}
if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
- t.Errorf("integ.TokenLiteral not %d. got=%s", value,
- integ.TokenLiteral())
+ t.Errorf("integ.TokenLiteral not %d. got=%s", value, integ.TokenLiteral())
return false
}
@@ -1035,6 +996,7 @@ func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
}
func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
+ t.Helper()
ident, ok := exp.(*ast.Identifier)
if !ok {
t.Errorf("exp not *ast.Identifier. got=%T", exp)
@@ -1047,8 +1009,7 @@ func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
}
if ident.TokenLiteral() != value {
- t.Errorf("ident.TokenLiteral not %s. got=%s", value,
- ident.TokenLiteral())
+ t.Errorf("ident.TokenLiteral not %s. got=%s", value, ident.TokenLiteral())
return false
}
@@ -1056,6 +1017,7 @@ func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
}
func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
+ t.Helper()
bo, ok := exp.(*ast.Boolean)
if !ok {
t.Errorf("exp not *ast.Boolean. got=%T", exp)
@@ -1068,8 +1030,7 @@ func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
}
if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
- t.Errorf("bo.TokenLiteral not %t. got=%s",
- value, bo.TokenLiteral())
+ t.Errorf("bo.TokenLiteral not %t. got=%s", value, bo.TokenLiteral())
return false
}
@@ -1098,39 +1059,33 @@ func TestMacroLiteralParsing(t *testing.T) {
checkParserErrors(t, p)
if len(program.Statements) != 1 {
- t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
- 1, len(program.Statements))
+ t.Fatalf("program.Statements does not contain %d statements. got=%d", 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])
+ 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)
+ 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))
+ t.Fatalf("macro literal parameters wrong. want 2, got=%d", 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))
+ t.Fatalf("macro.Body.Statements has not 1 statements. got=%d", 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])
+ t.Fatalf("macro body stmt is not ast.ExpressionStatement. got=%T", macro.Body.Statements[0])
}
testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")