From cbc46cb6127ffe224733a38526aaadb3666b04da Mon Sep 17 00:00:00 2001 From: z7zmey Date: Tue, 20 Mar 2018 20:06:56 +0200 Subject: [PATCH] pretty printer: print Class --- printer/printer.go | 37 +++++++++++++++++++ printer/printer_test.go | 79 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/printer/printer.go b/printer/printer.go index 5417ce3..522e7b7 100644 --- a/printer/printer.go +++ b/printer/printer.go @@ -294,6 +294,8 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) { return printStmtClassConstList case *stmt.ClassMethod: return printStmtClassMethod + case *stmt.Class: + return printStmtClass case *stmt.Constant: return printStmtConstant @@ -1366,6 +1368,41 @@ func printStmtClassMethod(o io.Writer, n node.Node) { io.WriteString(o, "}\n") // TODO: handle indentation } +func printStmtClass(o io.Writer, n node.Node) { + nn := n.(*stmt.Class) + + if nn.Modifiers != nil { + joinPrint(" ", o, nn.Modifiers) + io.WriteString(o, " ") + } + io.WriteString(o, "class") + + if nn.ClassName != nil { + io.WriteString(o, " ") + Print(o, nn.ClassName) + } + + if nn.Args != nil { + io.WriteString(o, "(") + joinPrint(", ", o, nn.Args) + io.WriteString(o, ")") + } + + if nn.Extends != nil { + io.WriteString(o, " extends ") + Print(o, nn.Extends) + } + + if nn.Implements != nil { + io.WriteString(o, " implements ") + joinPrint(", ", o, nn.Implements) + } + + io.WriteString(o, "\n{\n") // TODO: handle indentation + printNodes(o, nn.Stmts) + io.WriteString(o, "}\n") // TODO: handle indentation +} + func printStmtConstant(o io.Writer, n node.Node) { nn := n.(*stmt.Constant) diff --git a/printer/printer_test.go b/printer/printer_test.go index 231a49f..b67dbff 100644 --- a/printer/printer_test.go +++ b/printer/printer_test.go @@ -2149,6 +2149,85 @@ $a; } } +func TestPrintStmtClass(t *testing.T) { + o := bytes.NewBufferString("") + + printer.Print(o, &stmt.Class{ + Modifiers: []node.Node{&node.Identifier{Value: "abstract"}}, + ClassName: &node.Identifier{Value: "Foo"}, + Extends: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}}, + Implements: []node.Node{ + &name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}}, + &name.Name{Parts: []node.Node{&name.NamePart{Value: "Quuz"}}}, + }, + Stmts: []node.Node{ + &stmt.ClassConstList{ + Modifiers: []node.Node{&node.Identifier{Value: "public"}}, + Consts: []node.Node{ + &stmt.Constant{ + ConstantName: &node.Identifier{Value: "FOO"}, + Expr: &scalar.String{Value: "bar"}, + }, + }, + }, + }, + }) + + expected := `abstract class Foo extends Bar implements Baz, Quuz +{ +public const FOO = 'bar'; +} +` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + +func TestPrintStmtAnonymousClass(t *testing.T) { + o := bytes.NewBufferString("") + + printer.Print(o, &stmt.Class{ + Modifiers: []node.Node{&node.Identifier{Value: "abstract"}}, + Args: []node.Node{ + &node.Argument{ + Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, + }, + &node.Argument{ + Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}, + }, + }, + Extends: &name.Name{Parts: []node.Node{&name.NamePart{Value: "Bar"}}}, + Implements: []node.Node{ + &name.Name{Parts: []node.Node{&name.NamePart{Value: "Baz"}}}, + &name.Name{Parts: []node.Node{&name.NamePart{Value: "Quuz"}}}, + }, + Stmts: []node.Node{ + &stmt.ClassConstList{ + Modifiers: []node.Node{&node.Identifier{Value: "public"}}, + Consts: []node.Node{ + &stmt.Constant{ + ConstantName: &node.Identifier{Value: "FOO"}, + Expr: &scalar.String{Value: "bar"}, + }, + }, + }, + }, + }) + + expected := `abstract class($a, $b) extends Bar implements Baz, Quuz +{ +public const FOO = 'bar'; +} +` + actual := o.String() + + if expected != actual { + t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) + } +} + func TestPrintStmtConstant(t *testing.T) { o := bytes.NewBufferString("")