pretty printer: print Class

This commit is contained in:
z7zmey 2018-03-20 20:06:56 +02:00
parent 7c5d20a1b9
commit cbc46cb612
2 changed files with 116 additions and 0 deletions

View File

@ -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)

View File

@ -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("")