|
|
@@ -239,6 +239,56 @@ func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
+func testLiteralExpression(t *testing.T, exp ast.Expression, expected interface{}) bool {
|
|
|
+ switch v := expected.(type) {
|
|
|
+ case int:
|
|
|
+ return testIntegerLiteral(t, exp, int64(v))
|
|
|
+ case int64:
|
|
|
+ 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 hand. got=%T", exp)
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
+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 false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+func testIdentifier(t *testing.T, exp ast.Expression, v string) bool {
|
|
|
+ ident, ok := exp.(*ast.Identifier)
|
|
|
+ if !ok {
|
|
|
+ t.Errorf("exp not *ast.Identifier. got=%T", exp)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if ident.Value != v {
|
|
|
+ t.Errorf("ident.Value not %s. got=%s", v, ident.Value)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if ident.TokenLiteral() != v {
|
|
|
+ t.Errorf("ident.TokenLiteral not %s. got=%s", v,
|
|
|
+ ident.TokenLiteral())
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
// <expression> <infix operator> <expression>
|
|
|
func TestParsingInfixExpressions(t *testing.T) {
|
|
|
infixTests := []struct {
|
|
|
@@ -345,6 +395,10 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
|
|
|
"3 + 4 * 5 == 3 * 1 + 4 * 5",
|
|
|
"((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
|
|
|
},
|
|
|
+ {
|
|
|
+ "3 > 5 == false",
|
|
|
+ "((3 > 5) == false)",
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
@@ -361,3 +415,40 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
|
|
|
fmt.Printf("=====================================parse %s, end============================\n", tt.input)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+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()
|
|
|
+ checkParseErrors(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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|