Copy all parser tests.
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
fb25a86b91
commit
b68bbf01d2
1 changed files with 300 additions and 140 deletions
|
|
@ -12,7 +12,7 @@ func TestLetStatements(t *testing.T) {
|
|||
tests := []struct {
|
||||
input string
|
||||
expectedIdentifier string
|
||||
expectedValue any
|
||||
expectedValue interface{}
|
||||
}{
|
||||
{"let x = 5;", "x", 5},
|
||||
{"let y = true;", "y", true},
|
||||
|
|
@ -42,32 +42,6 @@ func TestLetStatements(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
|
||||
if s.TokenLiteral() != "let" {
|
||||
t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
letStmt, ok := s.(*ast.LetStatement)
|
||||
if !ok {
|
||||
t.Errorf("s not *ast.LetStatement. got=%T", s)
|
||||
return false
|
||||
}
|
||||
|
||||
if letStmt.Name.Value != name {
|
||||
t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
|
||||
return false
|
||||
}
|
||||
|
||||
if letStmt.Name.TokenLiteral() != name {
|
||||
t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s",
|
||||
name, letStmt.Name.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func TestReturnStatements(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
|
|
@ -104,12 +78,14 @@ func TestReturnStatements(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIdentifierExpressions(t *testing.T) {
|
||||
func TestIdentifierExpression(t *testing.T) {
|
||||
input := "foobar;"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
if len(program.Statements) != 1 {
|
||||
t.Fatalf("program has not enough statements. got=%d",
|
||||
len(program.Statements))
|
||||
|
|
@ -168,10 +144,12 @@ func TestParsingPrefixExpressions(t *testing.T) {
|
|||
prefixTests := []struct {
|
||||
input string
|
||||
operator string
|
||||
value any
|
||||
value interface{}
|
||||
}{
|
||||
{"!5;", "!", 5},
|
||||
{"-15;", "-", 15},
|
||||
{"!foobar;", "!", "foobar"},
|
||||
{"-foobar;", "-", "foobar"},
|
||||
{"!true;", "!", true},
|
||||
{"!false;", "!", false},
|
||||
}
|
||||
|
|
@ -207,14 +185,12 @@ func TestParsingPrefixExpressions(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// parser/parser_test.go
|
||||
|
||||
func TestParsingInfixExpressions(t *testing.T) {
|
||||
infixTests := []struct {
|
||||
input string
|
||||
leftValue any
|
||||
leftValue interface{}
|
||||
operator string
|
||||
rightValue any
|
||||
rightValue interface{}
|
||||
}{
|
||||
{"5 + 5;", 5, "+", 5},
|
||||
{"5 - 5;", 5, "-", 5},
|
||||
|
|
@ -224,6 +200,14 @@ func TestParsingInfixExpressions(t *testing.T) {
|
|||
{"5 < 5;", 5, "<", 5},
|
||||
{"5 == 5;", 5, "==", 5},
|
||||
{"5 != 5;", 5, "!=", 5},
|
||||
{"foobar + barfoo;", "foobar", "+", "barfoo"},
|
||||
{"foobar - barfoo;", "foobar", "-", "barfoo"},
|
||||
{"foobar * barfoo;", "foobar", "*", "barfoo"},
|
||||
{"foobar / barfoo;", "foobar", "/", "barfoo"},
|
||||
{"foobar > barfoo;", "foobar", ">", "barfoo"},
|
||||
{"foobar < barfoo;", "foobar", "<", "barfoo"},
|
||||
{"foobar == barfoo;", "foobar", "==", "barfoo"},
|
||||
{"foobar != barfoo;", "foobar", "!=", "barfoo"},
|
||||
{"true == true", true, "==", true},
|
||||
{"true != false", true, "!=", false},
|
||||
{"false == false", false, "==", false},
|
||||
|
|
@ -246,7 +230,8 @@ func TestParsingInfixExpressions(t *testing.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
|
||||
}
|
||||
}
|
||||
|
|
@ -333,6 +318,10 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
|
|||
"2 / (5 + 5)",
|
||||
"(2 / (5 + 5))",
|
||||
},
|
||||
{
|
||||
"(5 + 5) * 2 * (5 + 5)",
|
||||
"(((5 + 5) * 2) * (5 + 5))",
|
||||
},
|
||||
{
|
||||
"-(5 + 5)",
|
||||
"(-(5 + 5))",
|
||||
|
|
@ -483,8 +472,7 @@ func TestIfElseExpression(t *testing.T) {
|
|||
|
||||
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") {
|
||||
|
|
@ -498,7 +486,7 @@ func TestIfElseExpression(t *testing.T) {
|
|||
|
||||
consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
|
||||
if !ok {
|
||||
t.Fatalf("Statements[0] of consequence is not ast.ExpressionStatement. got=%T",
|
||||
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
|
||||
exp.Consequence.Statements[0])
|
||||
}
|
||||
|
||||
|
|
@ -507,15 +495,16 @@ func TestIfElseExpression(t *testing.T) {
|
|||
}
|
||||
|
||||
if len(exp.Alternative.Statements) != 1 {
|
||||
t.Errorf("alternative is not 1 statement. got=%d\n",
|
||||
t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d\n",
|
||||
len(exp.Alternative.Statements))
|
||||
}
|
||||
|
||||
alternative, ok := exp.Alternative.Statements[0].(*ast.ExpressionStatement)
|
||||
if !ok {
|
||||
t.Fatalf("Statements[0] of alternative is not ast.ExpressionStatement. got=%T",
|
||||
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
|
||||
exp.Alternative.Statements[0])
|
||||
}
|
||||
|
||||
if !testIdentifier(t, alternative.Expression, "y") {
|
||||
return
|
||||
}
|
||||
|
|
@ -636,6 +625,60 @@ func TestCallExpressionParsing(t *testing.T) {
|
|||
testInfixExpression(t, exp.Arguments[2], 4, "+", 5)
|
||||
}
|
||||
|
||||
func TestCallExpressionParameterParsing(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expectedIdent string
|
||||
expectedArgs []string
|
||||
}{
|
||||
{
|
||||
input: "add();",
|
||||
expectedIdent: "add",
|
||||
expectedArgs: []string{},
|
||||
},
|
||||
{
|
||||
input: "add(1);",
|
||||
expectedIdent: "add",
|
||||
expectedArgs: []string{"1"},
|
||||
},
|
||||
{
|
||||
input: "add(1, 2 * 3, 4 + 5);",
|
||||
expectedIdent: "add",
|
||||
expectedArgs: []string{"1", "(2 * 3)", "(4 + 5)"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if !testIdentifier(t, exp.Function, tt.expectedIdent) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(exp.Arguments) != len(tt.expectedArgs) {
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringLiteralExpression(t *testing.T) {
|
||||
input := `"hello world";`
|
||||
|
||||
|
|
@ -655,6 +698,25 @@ func TestStringLiteralExpression(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestParsingEmptyArrayLiterals(t *testing.T) {
|
||||
input := "[]"
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
||||
array, ok := stmt.Expression.(*ast.ArrayLiteral)
|
||||
if !ok {
|
||||
t.Fatalf("exp not ast.ArrayLiteral. got=%T", stmt.Expression)
|
||||
}
|
||||
|
||||
if len(array.Elements) != 0 {
|
||||
t.Errorf("len(array.Elements) not 0. got=%d", len(array.Elements))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsingArrayLiterals(t *testing.T) {
|
||||
input := "[1, 2 * 2, 3 + 3]"
|
||||
|
||||
|
|
@ -701,42 +763,6 @@ func TestParsingIndexExpressions(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestParsingHashLiteralsStringKeys(t *testing.T) {
|
||||
input := `{"one": 1, "two": 2, "three": 3}`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
||||
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
||||
if !ok {
|
||||
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
||||
}
|
||||
|
||||
if len(hash.Pairs) != 3 {
|
||||
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
||||
}
|
||||
|
||||
expected := map[string]int64{
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3,
|
||||
}
|
||||
|
||||
for key, value := range hash.Pairs {
|
||||
literal, ok := key.(*ast.StringLiteral)
|
||||
if !ok {
|
||||
t.Errorf("key is not ast.StringLiteral. got=%T", key)
|
||||
}
|
||||
|
||||
expectedValue := expected[literal.String()]
|
||||
|
||||
testIntegerLiteral(t, value, expectedValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsingEmptyHashLiteral(t *testing.T) {
|
||||
input := "{}"
|
||||
|
||||
|
|
@ -756,6 +782,114 @@ func TestParsingEmptyHashLiteral(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestParsingHashLiteralsStringKeys(t *testing.T) {
|
||||
input := `{"one": 1, "two": 2, "three": 3}`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
||||
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
||||
if !ok {
|
||||
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
||||
}
|
||||
|
||||
expected := map[string]int64{
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3,
|
||||
}
|
||||
|
||||
if len(hash.Pairs) != len(expected) {
|
||||
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
||||
}
|
||||
|
||||
for key, value := range hash.Pairs {
|
||||
literal, ok := key.(*ast.StringLiteral)
|
||||
if !ok {
|
||||
t.Errorf("key is not ast.StringLiteral. got=%T", key)
|
||||
continue
|
||||
}
|
||||
|
||||
expectedValue := expected[literal.String()]
|
||||
testIntegerLiteral(t, value, expectedValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsingHashLiteralsBooleanKeys(t *testing.T) {
|
||||
input := `{true: 1, false: 2}`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
||||
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
||||
if !ok {
|
||||
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
||||
}
|
||||
|
||||
expected := map[string]int64{
|
||||
"true": 1,
|
||||
"false": 2,
|
||||
}
|
||||
|
||||
if len(hash.Pairs) != len(expected) {
|
||||
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
||||
}
|
||||
|
||||
for key, value := range hash.Pairs {
|
||||
boolean, ok := key.(*ast.Boolean)
|
||||
if !ok {
|
||||
t.Errorf("key is not ast.BooleanLiteral. got=%T", key)
|
||||
continue
|
||||
}
|
||||
|
||||
expectedValue := expected[boolean.String()]
|
||||
testIntegerLiteral(t, value, expectedValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsingHashLiteralsIntegerKeys(t *testing.T) {
|
||||
input := `{1: 1, 2: 2, 3: 3}`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
stmt := program.Statements[0].(*ast.ExpressionStatement)
|
||||
hash, ok := stmt.Expression.(*ast.HashLiteral)
|
||||
if !ok {
|
||||
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
|
||||
}
|
||||
|
||||
expected := map[string]int64{
|
||||
"1": 1,
|
||||
"2": 2,
|
||||
"3": 3,
|
||||
}
|
||||
|
||||
if len(hash.Pairs) != len(expected) {
|
||||
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
|
||||
}
|
||||
|
||||
for key, value := range hash.Pairs {
|
||||
integer, ok := key.(*ast.IntegerLiteral)
|
||||
if !ok {
|
||||
t.Errorf("key is not ast.IntegerLiteral. got=%T", key)
|
||||
continue
|
||||
}
|
||||
|
||||
expectedValue := expected[integer.String()]
|
||||
|
||||
testIntegerLiteral(t, value, expectedValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsingHashLiteralsWithExpressions(t *testing.T) {
|
||||
input := `{"one": 0 + 1, "two": 10 - 8, "three": 15 / 5}`
|
||||
|
||||
|
|
@ -803,6 +937,97 @@ func TestParsingHashLiteralsWithExpressions(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
|
||||
if s.TokenLiteral() != "let" {
|
||||
t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
letStmt, ok := s.(*ast.LetStatement)
|
||||
if !ok {
|
||||
t.Errorf("s not *ast.LetStatement. got=%T", s)
|
||||
return false
|
||||
}
|
||||
|
||||
if letStmt.Name.Value != name {
|
||||
t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
|
||||
return false
|
||||
}
|
||||
|
||||
if letStmt.Name.TokenLiteral() != name {
|
||||
t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s",
|
||||
name, letStmt.Name.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func testInfixExpression(t *testing.T, exp ast.Expression, left interface{},
|
||||
operator string, right interface{}) bool {
|
||||
|
||||
opExp, ok := exp.(*ast.InfixExpression)
|
||||
if !ok {
|
||||
t.Errorf("exp is not ast.InfixExpression. got=%T(%s)", exp, exp)
|
||||
return false
|
||||
}
|
||||
|
||||
if !testLiteralExpression(t, opExp.Left, left) {
|
||||
return false
|
||||
}
|
||||
|
||||
if opExp.Operator != operator {
|
||||
t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator)
|
||||
return false
|
||||
}
|
||||
|
||||
if !testLiteralExpression(t, opExp.Right, right) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func testLiteralExpression(
|
||||
t *testing.T,
|
||||
exp ast.Expression,
|
||||
expected interface{},
|
||||
) bool {
|
||||
switch v := expected.(type) {
|
||||
case int:
|
||||
return testIntegerLiteral(t, exp, int64(v))
|
||||
case int64:
|
||||
return testIntegerLiteral(t, exp, v)
|
||||
case string:
|
||||
return testIdentifier(t, exp, v)
|
||||
case bool:
|
||||
return testBooleanLiteral(t, exp, v)
|
||||
}
|
||||
t.Errorf("type of exp not handled. got=%T", exp)
|
||||
return false
|
||||
}
|
||||
|
||||
func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
|
||||
integ, ok := il.(*ast.IntegerLiteral)
|
||||
if !ok {
|
||||
t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
|
||||
return false
|
||||
}
|
||||
|
||||
if integ.Value != value {
|
||||
t.Errorf("integ.Value not %d. got=%d", value, integ.Value)
|
||||
return false
|
||||
}
|
||||
|
||||
if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
|
||||
t.Errorf("integ.TokenLiteral not %d. got=%s", value,
|
||||
integ.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
|
||||
ident, ok := exp.(*ast.Identifier)
|
||||
if !ok {
|
||||
|
|
@ -845,71 +1070,6 @@ func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func testLiteralExpression(
|
||||
t *testing.T,
|
||||
exp ast.Expression,
|
||||
expected any,
|
||||
) bool {
|
||||
switch v := expected.(type) {
|
||||
case int:
|
||||
return testIntegerLiteral(t, exp, int64(v))
|
||||
case int64:
|
||||
return testIntegerLiteral(t, exp, v)
|
||||
case string:
|
||||
return testIdentifier(t, exp, v)
|
||||
case bool:
|
||||
return testBooleanLiteral(t, exp, v)
|
||||
}
|
||||
t.Errorf("type of exp not handled. got=%T", exp)
|
||||
return false
|
||||
}
|
||||
|
||||
func testInfixExpression(t *testing.T, exp ast.Expression, left any,
|
||||
operator string, right any) bool {
|
||||
|
||||
opExp, ok := exp.(*ast.InfixExpression)
|
||||
if !ok {
|
||||
t.Errorf("exp is not ast.InfixExpression. got=%T(%s)", exp, exp)
|
||||
return false
|
||||
}
|
||||
|
||||
if !testLiteralExpression(t, opExp.Left, left) {
|
||||
return false
|
||||
}
|
||||
|
||||
if opExp.Operator != operator {
|
||||
t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator)
|
||||
return false
|
||||
}
|
||||
|
||||
if !testLiteralExpression(t, opExp.Right, right) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
|
||||
integ, ok := il.(*ast.IntegerLiteral)
|
||||
if !ok {
|
||||
t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
|
||||
return false
|
||||
}
|
||||
|
||||
if integ.Value != value {
|
||||
t.Errorf("integ.Value not %d. got=%d", value, integ.Value)
|
||||
return false
|
||||
}
|
||||
|
||||
if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
|
||||
t.Errorf("integ.TokenLiteral not %d. got=%s", value,
|
||||
integ.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func checkParserErrors(t *testing.T, p *Parser) {
|
||||
errors := p.Errors()
|
||||
if len(errors) == 0 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue