|
@@ -150,12 +150,14 @@ func TestIntegerLiteralExpression(t *testing.T) {
|
|
|
|
|
|
func TestParsingPrefixExpressions(t *testing.T) {
|
|
|
prefixTests := []struct {
|
|
|
- input string
|
|
|
- operator string
|
|
|
- integerValue int64
|
|
|
+ input string
|
|
|
+ operator string
|
|
|
+ value interface{}
|
|
|
}{
|
|
|
{"!5", "!", 5},
|
|
|
{"-15", "-", 15},
|
|
|
+ {"!true", "!", true},
|
|
|
+ {"!false", "!", false},
|
|
|
}
|
|
|
|
|
|
for _, tt := range prefixTests {
|
|
@@ -186,7 +188,8 @@ func TestParsingPrefixExpressions(t *testing.T) {
|
|
|
tt.operator, exp.Operator)
|
|
|
}
|
|
|
|
|
|
- if !testIntegerLiteral(t, exp.Right, tt.integerValue) {
|
|
|
+ if !testLiteralExpression(t, exp.Right, tt.value) {
|
|
|
+ // No error message, they have already been generated in testLiteralExpression.
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -195,9 +198,9 @@ func TestParsingPrefixExpressions(t *testing.T) {
|
|
|
func TestParsingInfixExpressions(t *testing.T) {
|
|
|
infixTests := []struct {
|
|
|
input string
|
|
|
- leftValue int64
|
|
|
+ leftValue interface{}
|
|
|
operator string
|
|
|
- rightValue int64
|
|
|
+ rightValue interface{}
|
|
|
}{
|
|
|
{"5 + 5;", 5, "+", 5},
|
|
|
{"5 - 5;", 5, "-", 5},
|
|
@@ -207,6 +210,9 @@ func TestParsingInfixExpressions(t *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 infixTests {
|
|
@@ -231,18 +237,8 @@ func TestParsingInfixExpressions(t *testing.T) {
|
|
|
t.Fatalf("exp is not infixExpression. got=%T", stmt.Expression)
|
|
|
}
|
|
|
|
|
|
- // Why no error ?
|
|
|
- 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)
|
|
|
- }
|
|
|
-
|
|
|
- // Why no error ?
|
|
|
- if !testIntegerLiteral(t, exp.Right, tt.rightValue) {
|
|
|
+ if !testInfixExpression(t, exp, tt.leftValue, tt.operator, tt.rightValue) {
|
|
|
+ // No error message, they have already been generated in testInfixExpression.
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -301,6 +297,22 @@ func TestOperatorPrecedenceParsing(t *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 {
|
|
@@ -316,6 +328,43 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+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()
|
|
|
+ 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])
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 testLetStatement(t *testing.T, s ast.Statement, name string) bool {
|
|
|
if s.TokenLiteral() != "let" {
|
|
|
t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
|
|
@@ -385,6 +434,8 @@ func testLiteralExpression(
|
|
|
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 handled. got=%T", exp)
|
|
|
return false
|
|
@@ -432,6 +483,26 @@ func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
+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 true
|
|
|
+}
|
|
|
+
|
|
|
func checkParserErrors(t *testing.T, p *Parser) {
|
|
|
errors := p.Errors()
|
|
|
if len(errors) == 0 {
|