123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- package parser
- import (
- "testing"
- "fmt"
- "code.osinet.fr/fgm/waiig15/ast"
- "code.osinet.fr/fgm/waiig15/lexer"
- )
- func TestLetStatements(t *testing.T) {
- // Try removing the ident, the =, or both, to get human-readable errors.
- input := `
- let x = 5;
- let y = 10;
- let foobar = 838383;
- `
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParserErrors(t, p)
- if program == nil {
- t.Fatalf("ParseProgram() returned nil.")
- }
- if len(program.Statements) != 3 {
- t.Fatalf("program.Statements does not contain 3 statements, got=%d",
- len(program.Statements))
- }
- tests := []struct {
- expectedIdentifier string
- }{
- {"x"},
- {"y"},
- {"foobar"},
- }
- for i, tt := range tests {
- stmt := program.Statements[i]
- if !testLetStatement(t, stmt, tt.expectedIdentifier) {
- return
- }
- }
- }
- func TestReturnStatements(t *testing.T) {
- // Try removing the ident, the =, or both, to get human-readable errors.
- input := `
- return 5;
- return 10;
- return 993322;
- `
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParserErrors(t, p)
- if program == nil {
- t.Fatalf("ParseProgram() returned nil.")
- }
- if len(program.Statements) != 3 {
- t.Fatalf("program.Statements does not contain 3 statements, got=%d",
- len(program.Statements))
- }
- for _, stmt := range program.Statements {
- // Statement is an interface, we need a concrete type for the value, and
- // our test input only contains Let statements.
- returnStmt, ok := stmt.(*ast.ReturnStatement)
- if !ok {
- t.Errorf("s not *ast.ReturnStatement{}, got=%T", stmt)
- continue
- }
- if returnStmt.TokenLiteral() != "return" {
- t.Errorf("s.TokenLiteral not 'return', got=%q",
- stmt.TokenLiteral())
- }
- }
- }
- func TestIdentifierExpression(t *testing.T) {
- const 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))
- }
- stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
- if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. Got=%T",
- program.Statements)
- }
- ident, ok := stmt.Expression.(*ast.Identifier)
- if !ok {
- t.Fatalf("exp not *ast.Identifier. got=%T", stmt.Expression)
- }
- // Why not use input instead of inline strings ?
- if ident.Value != input {
- t.Errorf("ident.Value not %s. got=%s", input,
- ident.Value)
- }
- if ident.TokenLiteral() != input {
- t.Errorf("ident.TokenLiteral not %s. got=%s", input,
- ident.TokenLiteral())
- }
- }
- func TestIntegerLiteralExpression(t *testing.T) {
- input := "5;"
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParserErrors(t, p)
- if len(program.Statements) != 1 {
- t.Fatalf("program does not have 1 statement. 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())
- }
- }
- func TestParsingPrefixExpressions(t *testing.T) {
- prefixTests := []struct {
- input string
- operator string
- value interface{}
- }{
- {"!5", "!", 5},
- {"-15", "-", 15},
- {"!true", "!", true},
- {"!false", "!", false},
- }
- for _, tt := range prefixTests {
- l := lexer.New(tt.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("program.STatements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
- }
- exp, ok := stmt.Expression.(*ast.PrefixExpression)
- if !ok {
- t.Fatalf("stms 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 !testLiteralExpression(t, exp.Right, tt.value) {
- // No error message, they have already been generated in testLiteralExpression.
- return
- }
- }
- }
- func TestParsingInfixExpressions(t *testing.T) {
- infixTests := []struct {
- input string
- leftValue interface{}
- operator string
- rightValue interface{}
- }{
- {"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},
- {"true == true", true, "==", true},
- {"true != false", true, "!=", false},
- {"false == false", false, "==", false},
- }
- for _, tt := range infixTests {
- l := lexer.New(tt.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",
- 1, len(program.Statements))
- }
- stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
- if !ok {
- t.Fatalf("program.Statements[0] is not expressionStatement. got=%T",
- program.Statements[0])
- }
- exp, ok := stmt.Expression.(*ast.InfixExpression)
- if !ok {
- t.Fatalf("exp is not infixExpression. got=%T", stmt.Expression)
- }
- if !testInfixExpression(t, exp, tt.leftValue, tt.operator, tt.rightValue) {
- // No error message, they have already been generated in testInfixExpression.
- 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)))",
- },
- {
- "true",
- "true",
- },
- {
- "false",
- "false",
- },
- {
- "3 > 5 == false",
- "((3 > 5) == false)",
- },
- {
- "3 < 5 == true",
- "((3 < 5) == true)",
- },
- {
- "1 + (2 + 3) + 4",
- "((1 + (2 + 3)) + 4)",
- },
- {
- "(5 + 5) * 2",
- "((5 + 5) * 2)",
- },
- {
- "2 / (5 + 5)",
- "(2 / (5 + 5))",
- },
- {
- "-(5 + 5)",
- "(-(5 + 5))",
- },
- {
- "!(true == true)",
- "(!(true == true))",
- },
- }
- for _, tt := range tests {
- l := lexer.New(tt.input)
- p := New(l)
- program := p.ParseProgram()
- checkParserErrors(t, p)
- actual := program.String()
- if actual != tt.expected {
- t.Errorf("expected=%q, got=%q", tt.expected, actual)
- }
- }
- }
- func TestBooleanExpression(t *testing.T) {
- tests := []struct {
- input string
- expectedBoolean bool
- }{
- {"true", true},
- {"false", false},
- }
- for _, tt := range tests {
- l := lexer.New(tt.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))
- }
- stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
- if !ok {
- t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
- program.Statements[0])
- }
- boolean, ok := stmt.Expression.(*ast.Boolean)
- if !ok {
- 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)
- }
- }
- }
- 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
- }
- // Statement is an interface, we need a concrete type for the value, and we
- // just determined this looked like a LetStatement.
- 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 {
- t.Errorf("exp not *ast.Identifier. got=%T", exp)
- return false
- }
- if ident.Value != value {
- t.Errorf("ident.Value not %s. got=%s", value, ident.Value)
- return false
- }
- if ident.TokenLiteral() != value {
- t.Errorf("ident.TokenLiteral not %s. got=%s", value,
- ident.TokenLiteral())
- return false
- }
- return true
- }
- func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
- bo, ok := exp.(*ast.Boolean)
- if !ok {
- t.Errorf("exp not *ast.Boolean. got=%T", exp)
- return false
- }
- if bo.Value != value {
- t.Errorf("bo.Value not %t. got=%t", value, bo.Value)
- return false
- }
- if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
- t.Errorf("bo.TokenLiteral not %t. got=%s",
- value, bo.TokenLiteral())
- }
- return true
- }
- func checkParserErrors(t *testing.T, p *Parser) {
- errors := p.Errors()
- if len(errors) == 0 {
- return
- }
- t.Errorf("parser has %d errors", len(errors))
- for _, msg := range errors {
- t.Errorf("parser error: %q", msg)
- }
- t.FailNow()
- }
|