From 94ffbdfe591aaf4970d08483a6604e520f62cd2b Mon Sep 17 00:00:00 2001 From: z7zmey Date: Sat, 31 Mar 2018 14:08:56 +0300 Subject: [PATCH] pretty printer: print Echo, ElseIf, Else --- printer/printer.go | 53 ++++++++++++++++++ printer/printer_test.go | 119 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) diff --git a/printer/printer.go b/printer/printer.go index 50d36d9..bbd7077 100644 --- a/printer/printer.go +++ b/printer/printer.go @@ -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) diff --git a/printer/printer_test.go b/printer/printer_test.go index 13949a9..ef9f798 100644 --- a/printer/printer_test.go +++ b/printer/printer_test.go @@ -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("")