| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859 |
- 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"
- * <prefix operator><expression>;
- * -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 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 hand. got=%T", exp)
- return false
- }
- 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 false
- }
- return true
- }
- func testIdentifier(t *testing.T, exp ast.Expression, v string) bool {
- ident, ok := exp.(*ast.Identifier)
- if !ok {
- t.Errorf("exp not *ast.Identifier. got=%T", exp)
- return false
- }
- if ident.Value != v {
- t.Errorf("ident.Value not %s. got=%s", v, ident.Value)
- return false
- }
- if ident.TokenLiteral() != v {
- t.Errorf("ident.TokenLiteral not %s. got=%s", v,
- ident.TokenLiteral())
- return false
- }
- return true
- }
- // <expression> <infix operator> <expression>
- 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)))",
- },
- {
- "3 > 5 == false",
- "((3 > 5) == false)",
- },
- {
- "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))",
- },
- {
- "a * [1, 2, 3, 4][b * c] *d",
- "((a * ([1, 2, 3, 4][(b * c)])) * d)",
- },
- {
- "add(a * b[2], b[1], 2 * [1, 2][1])",
- "add((a * (b[2])), (b[1]), (2 * ([1, 2][1])))",
- },
- }
- for _, tt := range tests {
- 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,\n 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()
- 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])
- }
- 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 TestIfExpression(t *testing.T) {
- input := `if (x < y) { x }`
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(t, p)
- if len(program.Statements) != 1 {
- t.Fatalf("program.Body 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.IfExpression)
- if !ok {
- t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T", stmt.Expression)
- }
- if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
- return
- }
- if len(exp.Consequence.Statements) != 1 {
- t.Errorf("consequence is not 1 statements. got=%d\n",
- 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])
- }
- if !testIdentifier(t, consequence.Expression, "x") {
- return
- }
- if exp.Alternative != nil {
- t.Errorf("exp.Alternative.Statements was not nil. got=%+v", exp.Alternative)
- }
- }
- func TestIfElseExpression(t *testing.T) {
- input := `if (x < y) { x } else { y }`
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(t, p)
- if len(program.Statements) != 1 {
- t.Fatalf("program.Body 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.IfExpression)
- if !ok {
- t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T", stmt.Expression)
- }
- if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
- return
- }
- if len(exp.Consequence.Statements) != 1 {
- t.Errorf("consequence is not 1 statements. got=%d\n",
- 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])
- }
- if !testIdentifier(t, consequence.Expression, "x") {
- return
- }
- if len(exp.Alternative.Statements) != 1 {
- 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] is not ast.ExpressionStatement. got=%T",
- exp.Alternative.Statements[0])
- }
- if !testIdentifier(t, alternative.Expression, "y") {
- return
- }
- }
- 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.OperatorExpression. 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 TestFunctionLiteral(t *testing.T) {
- input := `fn(x,y) {x+y}`
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(t, p)
- if len(program.Statements) != 1 {
- t.Fatalf("Program.Body does not contain %d statements. got=%d\n", 1, len(program.Statements))
- }
- stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
- if !ok {
- t.Fatalf("program.Statement[0] is not ast.ExpressionStatemtn. 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)
- }
- if len(function.Parameters) != 2 {
- t.Fatalf("function literal parameters wrong. want 2, got =%d\n", 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))
- }
- 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])
- }
- testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
- }
- func TestFunctionParameterParsing(t *testing.T) {
- tests := []struct {
- input string
- expectedParams []string
- }{
- {input: "fn() {};", expectedParams: []string{}},
- {input: "fn(x){};", expectedParams: []string{"x"}},
- {input: "fn(x,y,z){};", expectedParams: []string{"x", "y", "z"}},
- }
- for _, tt := range tests {
- l := lexer.New(tt.input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(t, p)
- stmt := program.Statements[0].(*ast.ExpressionStatement)
- 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))
- }
- for i, ident := range tt.expectedParams {
- testLiteralExpression(t, function.Parameters[i], ident)
- }
- }
- }
- func TestCallExpressionParsing(t *testing.T) {
- input := `add(1, 2 * 3, 4+5);`
- l := lexer.New(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("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)
- }
- if !testIdentifier(t, exp.Function, "add") {
- return
- }
- if len(exp.Arguments) != 3 {
- t.Fatalf("wrong length arguments. got=%d", len(exp.Arguments))
- }
- testLiteralExpression(t, exp.Arguments[0], 1)
- testInfixExpression(t, exp.Arguments[1], 2, "*", 3)
- testInfixExpression(t, exp.Arguments[2], 4, "+", 5)
- }
- func TestStringLiteralExpression(t *testing.T) {
- input := `"hello world";`
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(t, p)
- stmt := program.Statements[0].(*ast.ExpressionStatement)
- literal, ok := stmt.Expression.(*ast.StringLiteral)
- if !ok {
- t.Fatalf("exp not *ast.StringLiteral. got=%T", stmt.Expression)
- }
- if literal.Value != "hello world" {
- t.Fatalf("literal.Value not %q. got=%q", "hello world", literal.Value)
- }
- }
- func TestArrayLiteralsParsing(t *testing.T) {
- input := "[1, 2 * 2, 3 + 3]"
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(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.Element) != 3 {
- t.Fatalf("len(array.Element) not 3. got=%d", len(array.Element))
- }
- testIntegerLiteral(t, array.Element[0], 1)
- testInfixExpression(t, array.Element[1], 2, "*", 2)
- testInfixExpression(t, array.Element[2], 3, "+", 3)
- }
- func TestIndexExpressionParsing(t *testing.T) {
- input := "myArray[1+1]"
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(t, p)
- stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
- indexExp, ok := stmt.Expression.(*ast.IndexExpression)
- if !ok {
- t.Fatalf("exp not *ast.IndexExpression. got=%T", stmt.Expression)
- }
- if !testIdentifier(t, indexExp.Left, "myArray") {
- return
- }
- if !testInfixExpression(t, indexExp.Index, 1, "+", 1) {
- return
- }
- }
- func TestHashLiteralParsing(t *testing.T) {
- input := `{"one": 1, "two":2, "three": 3}`
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(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 k, v := range hash.Pairs {
- literal, ok := k.(*ast.StringLiteral)
- if !ok {
- t.Errorf("key is not ast.StringLiteral. got =%T", k)
- }
- expectedVal := expected[literal.String()]
- testIntegerLiteral(t, v, expectedVal)
- }
- }
- func TestEmptyHashLiteralParsing(t *testing.T) {
- input := `{}`
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(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) != 0 {
- t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
- }
- }
- func TestParsingHashLiteralsWithExpressions(t *testing.T) {
- input := `{"one": 0 + 1, "two": 10 - 8, "three": 15 / 5}`
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParseErrors(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))
- }
- tests := map[string]func(ast.Expression){
- "one": func(e ast.Expression) {
- testInfixExpression(t, e, 0, "+", 1)
- },
- "two": func(e ast.Expression) {
- testInfixExpression(t, e, 10, "-", 8)
- },
- "three": func(e ast.Expression) {
- testInfixExpression(t, e, 15, "/", 5)
- },
- }
- for key, value := range hash.Pairs {
- literal, ok := key.(*ast.StringLiteral)
- if !ok {
- t.Errorf("key is not ast.StringLiteral. got=%T", key)
- continue
- }
- testFunc, ok := tests[literal.String()]
- if !ok {
- t.Errorf("No test function for key %q found.", literal.String())
- continue
- }
- testFunc(value)
- }
- }
|