- Added helper functions to test BooleanLiterals
- Formatting

git-svn-id: https://svn.tlawal.org/svn/monkey@33 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
This commit is contained in:
Tijani Lawal 2022-07-14 12:12:56 +00:00
parent 628a604701
commit 388700441e
3 changed files with 52 additions and 10 deletions

View File

@ -191,4 +191,4 @@ func (ie *InfixExpression) String() string {
// Booleans
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}

View File

@ -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{

View File

@ -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
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
}