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.go266
1 files changed, 266 insertions, 0 deletions
diff --git a/parser/parser_test.go b/parser/parser_test.go
index fa080aa..e187c9f 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -345,6 +345,14 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
"add(a + b + c * d / f + g)",
"add((((a + b) + ((c * d) / f)) + g))",
},
+ {
+ "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 {
@@ -674,6 +682,264 @@ func TestCallExpressionParameterParsing(t *testing.T) {
}
}
+func TestStringLiteralExpression(t *testing.T) {
+ input := `"hello world";`
+
+ l := lexer.New(input)
+ p := New(l)
+ program := p.ParseProgram()
+ checkParserErrors(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.Errorf("literal.Value not %q. got=%q", "hello world", literal.Value)
+ }
+}
+
+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]"
+
+ 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) != 3 {
+ t.Fatalf("len(array.Elements) not 3. got=%d", len(array.Elements))
+ }
+
+ testIntegerLiteral(t, array.Elements[0], 1)
+ testInfixExpression(t, array.Elements[1], 2, "*", 2)
+ testInfixExpression(t, array.Elements[2], 3, "+", 3)
+}
+
+func TestParsingIndexExpressions(t *testing.T) {
+ input := "myArray[1 + 1]"
+
+ l := lexer.New(input)
+ p := New(l)
+ program := p.ParseProgram()
+ checkParserErrors(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 TestParsingEmptyHashLiteral(t *testing.T) {
+ input := "{}"
+
+ 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) != 0 {
+ t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
+ }
+}
+
+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}`
+
+ 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))
+ }
+
+ 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)
+ }
+}
+
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())