pretty printer: print Echo, ElseIf, Else
This commit is contained in:
parent
6197aa5afb
commit
94ffbdfe59
@ -306,6 +306,12 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) {
|
||||
return printStmtDefault
|
||||
case *stmt.Do:
|
||||
return printStmtDo
|
||||
case *stmt.Echo:
|
||||
return printStmtEcho
|
||||
case *stmt.ElseIf:
|
||||
return printStmtElseif
|
||||
case *stmt.Else:
|
||||
return printStmtElse
|
||||
|
||||
case *stmt.StmtList:
|
||||
return printStmtStmtList
|
||||
@ -1484,6 +1490,53 @@ func printStmtDo(o io.Writer, n node.Node) {
|
||||
io.WriteString(o, ");\n")
|
||||
}
|
||||
|
||||
func printStmtEcho(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.Echo)
|
||||
io.WriteString(o, "echo ")
|
||||
joinPrint(", ", o, nn.Exprs)
|
||||
io.WriteString(o, ";\n")
|
||||
}
|
||||
|
||||
func printStmtElseif(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.ElseIf)
|
||||
|
||||
io.WriteString(o, "elseif (")
|
||||
Print(o, nn.Cond)
|
||||
io.WriteString(o, ")")
|
||||
|
||||
switch s := nn.Stmt.(type) {
|
||||
case *stmt.Nop:
|
||||
Print(o, s)
|
||||
break
|
||||
case *stmt.StmtList:
|
||||
io.WriteString(o, " {\n")
|
||||
printNodes(o, s.Stmts)
|
||||
io.WriteString(o, "}\n")
|
||||
default:
|
||||
io.WriteString(o, "\n")
|
||||
Print(o, s)
|
||||
}
|
||||
}
|
||||
|
||||
func printStmtElse(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.Else)
|
||||
|
||||
io.WriteString(o, "else")
|
||||
|
||||
switch s := nn.Stmt.(type) {
|
||||
case *stmt.Nop:
|
||||
Print(o, s)
|
||||
break
|
||||
case *stmt.StmtList:
|
||||
io.WriteString(o, " {\n")
|
||||
printNodes(o, s.Stmts)
|
||||
io.WriteString(o, "}\n")
|
||||
default:
|
||||
io.WriteString(o, "\n")
|
||||
Print(o, s)
|
||||
}
|
||||
}
|
||||
|
||||
func printStmtStmtList(o io.Writer, n node.Node) {
|
||||
nn := n.(*stmt.StmtList)
|
||||
|
||||
|
@ -2381,6 +2381,125 @@ func TestPrintStmtDo_StmtList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtEcho(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.Echo{
|
||||
Exprs: []node.Node{
|
||||
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
||||
&expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
||||
},
|
||||
})
|
||||
|
||||
expected := "echo $a, $b;\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtElseIfStmts(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.ElseIf{
|
||||
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
||||
Stmt: &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Nop{},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expected := "elseif ($a) {\n;\n}\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtElseIfExpr(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.ElseIf{
|
||||
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
||||
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "bar"}},
|
||||
})
|
||||
|
||||
expected := "elseif ($a)\n'bar';\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtElseIfNop(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.ElseIf{
|
||||
Cond: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
||||
Stmt: &stmt.Nop{},
|
||||
})
|
||||
|
||||
expected := "elseif ($a);\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtElseStmts(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.Else{
|
||||
Stmt: &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Nop{},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expected := "else {\n;\n}\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtElseExpr(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.Else{
|
||||
Stmt: &stmt.Expression{Expr: &scalar.String{Value: "bar"}},
|
||||
})
|
||||
|
||||
expected := "else\n'bar';\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtElseNop(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
printer.Print(o, &stmt.Else{
|
||||
Stmt: &stmt.Nop{},
|
||||
})
|
||||
|
||||
expected := "else;\n"
|
||||
actual := o.String()
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintStmtList(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user