diff options
| author | Bobby <[email protected]> | 2023-11-03 16:17:15 +0000 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-11-03 16:17:15 +0000 |
| commit | e8a6c163c9a58b69f6f96c373118ac6996637e25 (patch) | |
| tree | d16ab60698adbd9491e5904809c67a4ecc41cd0e /parser/parser_test.go | |
| parent | 15f9a15757322221d8bf9a3ecd5d8ce8490ebda6 (diff) | |
| download | mana-e8a6c163c9a58b69f6f96c373118ac6996637e25.tar.xz mana-e8a6c163c9a58b69f6f96c373118ac6996637e25.zip | |
parser:init pratt parser. parser:add prefix, infix and `parseIntegerLiteral`
Diffstat (limited to 'parser/parser_test.go')
| -rw-r--r-- | parser/parser_test.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/parser/parser_test.go b/parser/parser_test.go index 8e06ecd..cd1d506 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -122,3 +122,73 @@ func TestReturnStatements(t *testing.T) { } } } + +// Identifier expression tests. +func TestIdentifierExpression(t *testing.T) { + var input string = "foobar;" + + var l *lexer.Lexer = lexer.New(input) + var p *Parser = New(l) + var program *ast.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]) + } + + ident, ok := stmt.Expression.(*ast.Identifier) + + if !ok { + t.Fatalf("exp not *ast.Identifier. got=%T", stmt.Expression) + } + + if ident.Value != "foobar" { + t.Errorf("ident.Value not %s. got=%s", "foobar", ident.Value) + } + + if ident.TokenLiteral() != "foobar" { + t.Errorf("ident.TokenLiteral not %s. got=%s", "foobar", ident.TokenLiteral()) + } +} + +// Integer literal expression tests. +func TestIntegerLiteralExpression(t *testing.T) { + var input string = "5;" + + var l *lexer.Lexer = lexer.New(input) + var p *Parser = New(l) + var program *ast.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]) + } + + 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()) + } +} + |
