package parser import ( "fmt" "github/runnignwater/monkey/ast" "github/runnignwater/monkey/lexer" "log" "testing" ) /** * @Author: simon * @Author: ynwdlxm@163.com * @Date: 2022/10/2 下午10:40 * @Desc: LetStatement test case */ func TestLetStatements(t *testing.T) { input := ` let x = 5; let y = 10; let foo = 838383; ` l := lexer.New(input) p := New(l) program := p.ParseProgram() checkParseErrors(t, p) if program == nil { t.Fatalf("ParseProgram() return nil") } if len(program.Statements) != 3 { t.Fatalf("Program.Statements does not contain 3 statements. got=%d", len(program.Statements)) } tests := []struct { expectedIdentifies string }{ {"x"}, {"y"}, {"foo"}, } for i, tt := range tests { stmt := program.Statements[i] if !testLetStatement(t, stmt, tt.expectedIdentifies) { return } } } func TestReturnStatements(t *testing.T) { input := ` return 5; return 10; return add(a,10); ` l := lexer.New(input) p := New(l) program := p.ParseProgram() checkParseErrors(t, p) if len(program.Statements) != 3 { t.Fatalf("Program.Statements does not contain 3 statements. got=%d", len(program.Statements)) } for _, stmt := range program.Statements { returnStmt, ok := stmt.(*ast.ReturnStatement) if !ok { t.Errorf("stmt not *ast.ReturnStatement. got=%T", stmt) continue } if returnStmt.TokenLiteral() != "return" { t.Errorf("returnStmt.TokenLiteral not 'return', got %q", returnStmt.TokenLiteral()) } } } func checkParseErrors(t *testing.T, p *Parser) { errors := p.errors if len(errors) == 0 { return } t.Errorf("parse has %d errors.", len(errors)) for _, msg := range errors { t.Errorf("parse error: %q", msg) } t.FailNow() } 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 is 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("s.name not '%s. got=%s", name, letStmt.Name) return false } return true } func TestIdentifier(t *testing.T) { input := "foobar;" l := lexer.New(input) p := New(l) program := p.ParseProgram() checkParseErrors(t, p) if len(program.Statements) != 1 { 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]) } ident, ok := stmt.Expression.(*ast.Identifier) if !ok { t.Fatalf("exp not *ast.Identifier. got=%T", stmt.Expression) } if ident.Value != "foobar" { t.Fatalf("ident.Value not %s. got=%s", "foobar", ident.Value) } if ident.TokenLiteral() != "foobar" { t.Errorf("ident.TokenLiteral() not %s. got=%s", "foobar", ident.TokenLiteral()) } } func TestIntegerLiteralExpression(t *testing.T) { input := "5;" l := lexer.New(input) p := New(l) program := p.ParseProgram() checkParseErrors(t, p) if len(program.Statements) != 1 { 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]) } literal, ok := stmt.Expression.(*ast.IntegerLiteral) if !ok { t.Fatalf("exp not *ast.IntegerLiteral. got=%T", stmt.Expression) } if literal.Value != 5 { 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()) } } /** * Prefix Operators or "prefix expressions" * ; * -5; * !foobar; * 5 + -10; */ func TestParsingPrefixExpressions(t *testing.T) { prefixTests := []struct { input string operator string integerValue int64 }{ {"!5;", "!", 5}, {"-15;", "-", 15}, } for _, tt := range prefixTests { l := lexer.New(tt.input) p := New(l) program := p.ParseProgram() checkParseErrors(t, p) if len(program.Statements) != 1 { t.Fatalf("program.Statement does not contain %d statements. got=%d\n", 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]) } exp, ok := stmt.Expression.(*ast.PrefixExpression) if !ok { log.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) } if !testIntegerLiteral(t, exp.Right, tt.integerValue) { return } } } 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. get=%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 TestParsingInfixExpressions(t *testing.T) { infixTests := []struct { input string leftValue int64 operator string rightValue int64 }{ {"5 + 5", 5, "+", 5}, {"5 - 5", 5, "-", 5}, {"5 * 5", 5, "*", 5}, {"5 / 5", 5, "/", 5}, {"5 > 5", 5, ">", 5}, {"5 < 5", 5, "<", 5}, {"5 == 5", 5, "==", 5}, {"5 != 5", 5, "!=", 5}, } for _, tt := range infixTests { l := lexer.New(tt.input) p := New(l) program := p.ParseProgram() checkParseErrors(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("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0]) } exp, ok := stmt.Expression.(*ast.InfixExpression) if !ok { t.Fatalf("exp is not ast.InfixExpression. got =%T", stmt.Expression) } if !testIntegerLiteral(t, exp.Left, tt.leftValue) { return } if exp.Operator != tt.operator { t.Fatalf("exp.Operator is not '%s'. got=%s", tt.operator, exp.Operator) } if !testIntegerLiteral(t, exp.Right, tt.rightValue) { return } } } func TestOperatorPrecedenceParsing(t *testing.T) { tests := []struct { input string expected string }{ { "-a * b", "((-a) * b)", }, { "!-a", "(!(-a))", }, { "a + b + c", "((a + b) + c)", }, { "a + b - c", "((a + b) - c)", }, { "a * b * c", "((a * b) * c)", }, { "a * b / c", "((a * b) / c)", }, { "a + b / c", "(a + (b / c))", }, { "a + b * c + d / e - f", "(((a + (b * c)) + (d / e)) - f)", }, { "3 + 4; -5 *5", "(3 + 4)((-5) * 5)", }, { "5 > 4 == 3 < 4", "((5 > 4) == (3 < 4))", }, { "5 < 4 != 3 > 4", "((5 < 4) != (3 > 4))", }, { "3 + 4 * 5 == 3 * 1 + 4 * 5", "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))", }, { "3 + 4 * 5 == 3 * 1 + 4 * 5", "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))", }, } for _, tt := range tests { fmt.Printf("=====================================parse %s, begin============================\n", tt.input) l := lexer.New(tt.input) p := New(l) program := p.ParseProgram() checkParseErrors(t, p) actual := program.String() if actual != tt.expected { t.Errorf("exptected=%q, got=%q", tt.expected, actual) } fmt.Printf("=====================================parse %s, end============================\n", tt.input) } }