boolean and if/else
This commit is contained in:
@@ -356,6 +356,114 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIfExpression(t *testing.T) {
|
||||
input := `if (x < y) { x }`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
if len(program.Statements) != 1 {
|
||||
t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
|
||||
1, 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])
|
||||
}
|
||||
|
||||
exp, ok := stmt.Expression.(*ast.IfExpression)
|
||||
if !ok {
|
||||
t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T",
|
||||
stmt.Expression)
|
||||
}
|
||||
|
||||
if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
|
||||
return
|
||||
}
|
||||
|
||||
if len(exp.Consequence.Statements) != 1 {
|
||||
t.Errorf("consequence is not 1 statements. got=%d\n",
|
||||
len(exp.Consequence.Statements))
|
||||
}
|
||||
|
||||
consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
|
||||
if !ok {
|
||||
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
|
||||
exp.Consequence.Statements[0])
|
||||
}
|
||||
|
||||
if !testIdentifier(t, consequence.Expression, "x") {
|
||||
return
|
||||
}
|
||||
|
||||
if exp.Alternative != nil {
|
||||
t.Errorf("exp.Alternative.Statements was not nil. got=%+v", exp.Alternative)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIfElseExpression(t *testing.T) {
|
||||
input := `if (x < y) { x } else { y }`
|
||||
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
if len(program.Statements) != 1 {
|
||||
t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
|
||||
1, 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])
|
||||
}
|
||||
|
||||
exp, ok := stmt.Expression.(*ast.IfExpression)
|
||||
if !ok {
|
||||
t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T", stmt.Expression)
|
||||
}
|
||||
|
||||
if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
|
||||
return
|
||||
}
|
||||
|
||||
if len(exp.Consequence.Statements) != 1 {
|
||||
t.Errorf("consequence is not 1 statements. got=%d\n",
|
||||
len(exp.Consequence.Statements))
|
||||
}
|
||||
|
||||
consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
|
||||
if !ok {
|
||||
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
|
||||
exp.Consequence.Statements[0])
|
||||
}
|
||||
|
||||
if !testIdentifier(t, consequence.Expression, "x") {
|
||||
return
|
||||
}
|
||||
|
||||
if len(exp.Alternative.Statements) != 1 {
|
||||
t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d\n",
|
||||
len(exp.Alternative.Statements))
|
||||
}
|
||||
|
||||
alternative, ok := exp.Alternative.Statements[0].(*ast.ExpressionStatement)
|
||||
if !ok {
|
||||
t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
|
||||
exp.Alternative.Statements[0])
|
||||
}
|
||||
|
||||
if !testIdentifier(t, alternative.Expression, "y") {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
@@ -419,11 +527,34 @@ 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
|
||||
}
|
||||
|
||||
func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
|
||||
bo, ok := exp.(*ast.Boolean)
|
||||
if !ok {
|
||||
t.Errorf("exp *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("intboeg.TokenLiteral not %t. got=%s", value,
|
||||
bo.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
|
||||
integ, ok := il.(*ast.IntegerLiteral)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user