From 388700441e91a92db39c239d1622f5cc72ae0ffe Mon Sep 17 00:00:00 2001 From: tijani Date: Thu, 14 Jul 2022 12:12:56 +0000 Subject: [PATCH] Changes: - Added helper functions to test BooleanLiterals - Formatting git-svn-id: https://svn.tlawal.org/svn/monkey@33 f6afcba9-9ef1-4bdd-9b72-7484f5705bac --- ast/ast.go | 4 +-- parser/parser.go | 1 - parser/parser_test.go | 57 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/ast/ast.go b/ast/ast.go index 2fedfc8..200c64b 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -189,6 +189,6 @@ func (ie *InfixExpression) String() string { } // Booleans -func (b *Boolean) expression_node() {} +func (b *Boolean) expression_node() {} func (b *Boolean) TokenLiteral() string { return b.Token.Literal } -func (b *Boolean) String() string { return b.Token.Literal } +func (b *Boolean) String() string { return b.Token.Literal} diff --git a/parser/parser.go b/parser/parser.go index ebab816..26530b8 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -271,7 +271,6 @@ func (l_parser *Parser) no_prefix_parse_function_error(l_token_type token.TokenT l_parser.errors = append(l_parser.errors, message) } - func (l_parser *Parser) parse_boolean() ast.Expression { defer untrace(trace("parse_boolean")) return &ast.Boolean{ diff --git a/parser/parser_test.go b/parser/parser_test.go index 2ca5a16..85ce650 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -139,14 +139,16 @@ func TestIntegerLiteralExpressions(l_test *testing.T) { } } -func TestParsingPrefixExpression(l_test *testing.T) { +func TestParsingPrefixExpressions(l_test *testing.T) { prefix_tests := []struct { - input string - operator string - integer_value int64 + input string + operator string + value interface{} }{ {"!5;", "!", 5}, {"-15", "-", 15}, + {"!true;", "!", true}, + {"!false;", "!", false}, } for _, tt := range prefix_tests { @@ -172,7 +174,7 @@ func TestParsingPrefixExpression(l_test *testing.T) { l_test.Fatalf("exp.Operator is not '%s', got %s", tt.operator, expression.Operator) } - if !testIntegerLiteral(l_test, expression.Right, tt.integer_value) { + if !testLiteralExpression(l_test, expression.Right, tt.value) { return } } @@ -181,9 +183,9 @@ func TestParsingPrefixExpression(l_test *testing.T) { func TestParsingInfixExpressions(l_test *testing.T) { infix_tests := []struct { input string - left_value int64 + left_value interface{} operator string - right_value int64 + right_value interface{} }{ {"5 + 5;", 5, "+", 5}, {"5 - 5;", 5, "-", 5}, @@ -193,6 +195,9 @@ func TestParsingInfixExpressions(l_test *testing.T) { {"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 infix_tests { @@ -269,6 +274,22 @@ func TestOperatorPrecedenceParsing(l_test *testing.T) { "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)", + }, } for _, tt := range tests { l_lexer := lexer.New(tt.input) @@ -406,6 +427,8 @@ func testLiteralExpression(l_test *testing.T, exp ast.Expression, expected inter return testIntegerLiteral(l_test, exp, v) case string: return testIdentifier(l_test, exp, v) + case bool: + return testBooleanLiteral(l_test, exp, v) } l_test.Errorf("type of exp not handled, got=%T", exp) @@ -433,3 +456,23 @@ func testInfixExpression(l_test *testing.T, exp ast.Expression, left interface{} } return true } + +func testBooleanLiteral(l_test *testing.T, exp ast.Expression, value bool) bool { + boolean, ok := exp.(*ast.Boolean) + if !ok { + l_test.Errorf("exp not *ast.Boolean, got=%T", exp) + return false + } + + if boolean.Value != value { + l_test.Errorf("boolean.Value is not %t, got=%t", value, boolean.Value) + return false + } + + if boolean.TokenLiteral() != fmt.Sprintf("%t", value) { + l_test.Errorf("boolean.TokenLiteral is not %t, got=%s", value, boolean.TokenLiteral()) + return false + } + + return true +}