pretty printer: print StaticVar, Static, Switch
This commit is contained in:
		
							parent
							
								
									e87d6d372b
								
							
						
					
					
						commit
						a9b6e93480
					
				| @ -348,9 +348,15 @@ func getPrintFuncByNode(n node.Node) func(o io.Writer, n node.Node) { | ||||
| 		return printStmtProperty | ||||
| 	case *stmt.Return: | ||||
| 		return printStmtReturn | ||||
| 
 | ||||
| 	case *stmt.StaticVar: | ||||
| 		return printStmtStaticVar | ||||
| 	case *stmt.Static: | ||||
| 		return printStmtStatic | ||||
| 	case *stmt.StmtList: | ||||
| 		return printStmtStmtList | ||||
| 	case *stmt.Switch: | ||||
| 		return printStmtSwitch | ||||
| 
 | ||||
| 	case *stmt.Use: | ||||
| 		return printStmtUse | ||||
| 	} | ||||
| @ -1816,12 +1822,42 @@ func printStmtReturn(o io.Writer, n node.Node) { | ||||
| 	io.WriteString(o, ";\n") | ||||
| } | ||||
| 
 | ||||
| func printStmtStaticVar(o io.Writer, n node.Node) { | ||||
| 	nn := n.(*stmt.StaticVar) | ||||
| 	Print(o, nn.Variable) | ||||
| 
 | ||||
| 	if nn.Expr != nil { | ||||
| 		io.WriteString(o, " = ") | ||||
| 		Print(o, nn.Expr) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func printStmtStatic(o io.Writer, n node.Node) { | ||||
| 	nn := n.(*stmt.Static) | ||||
| 
 | ||||
| 	io.WriteString(o, "static ") | ||||
| 	joinPrint(", ", o, nn.Vars) | ||||
| 	io.WriteString(o, ";\n") | ||||
| } | ||||
| 
 | ||||
| func printStmtStmtList(o io.Writer, n node.Node) { | ||||
| 	nn := n.(*stmt.StmtList) | ||||
| 
 | ||||
| 	printNodes(o, nn.Stmts) | ||||
| } | ||||
| 
 | ||||
| func printStmtSwitch(o io.Writer, n node.Node) { | ||||
| 	nn := n.(*stmt.Switch) | ||||
| 
 | ||||
| 	io.WriteString(o, "switch (") | ||||
| 	Print(o, nn.Cond) | ||||
| 	io.WriteString(o, ")") | ||||
| 
 | ||||
| 	io.WriteString(o, " {\n") | ||||
| 	printNodes(o, nn.Cases) | ||||
| 	io.WriteString(o, "}\n") | ||||
| } | ||||
| 
 | ||||
| func printStmtUse(o io.Writer, n node.Node) { | ||||
| 	nn := n.(*stmt.Use) | ||||
| 
 | ||||
|  | ||||
| @ -2959,24 +2959,6 @@ $a; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintStmtList(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
| 	printer.Print(o, &stmt.StmtList{ | ||||
| 		Stmts: []node.Node{ | ||||
| 			&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, | ||||
| 			&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, | ||||
| 		}, | ||||
| 	}) | ||||
| 
 | ||||
| 	expected := "$a;\n$b;\n" | ||||
| 	actual := o.String() | ||||
| 
 | ||||
| 	if expected != actual { | ||||
| 		t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintNop(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
| @ -3047,6 +3029,97 @@ func TestPrintReturn(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintStaticVar(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
| 	printer.Print(o, &stmt.StaticVar{ | ||||
| 		Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, | ||||
| 		Expr:     &scalar.Lnumber{Value: "1"}, | ||||
| 	}) | ||||
| 
 | ||||
| 	expected := "$a = 1" | ||||
| 	actual := o.String() | ||||
| 
 | ||||
| 	if expected != actual { | ||||
| 		t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintStatic(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
| 	printer.Print(o, &stmt.Static{ | ||||
| 		Vars: []node.Node{ | ||||
| 			&stmt.StaticVar{ | ||||
| 				Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}}, | ||||
| 			}, | ||||
| 			&stmt.StaticVar{ | ||||
| 				Variable: &expr.Variable{VarName: &node.Identifier{Value: "b"}}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}) | ||||
| 
 | ||||
| 	expected := "static $a, $b;\n" | ||||
| 	actual := o.String() | ||||
| 
 | ||||
| 	if expected != actual { | ||||
| 		t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintStmtList(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
| 	printer.Print(o, &stmt.StmtList{ | ||||
| 		Stmts: []node.Node{ | ||||
| 			&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, | ||||
| 			&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, | ||||
| 		}, | ||||
| 	}) | ||||
| 
 | ||||
| 	expected := "$a;\n$b;\n" | ||||
| 	actual := o.String() | ||||
| 
 | ||||
| 	if expected != actual { | ||||
| 		t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintStmtSwitch(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
| 	printer.Print(o, &stmt.Switch{ | ||||
| 		Cond: &expr.Variable{VarName: &node.Identifier{Value: "var"}}, | ||||
| 		Cases: []node.Node{ | ||||
| 			&stmt.Case{ | ||||
| 				Cond: &scalar.String{Value: "a"}, | ||||
| 				Stmts: []node.Node{ | ||||
| 					&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&stmt.Case{ | ||||
| 				Cond: &scalar.String{Value: "b"}, | ||||
| 				Stmts: []node.Node{ | ||||
| 					&stmt.Expression{Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}) | ||||
| 
 | ||||
| 	expected := `switch ($var) { | ||||
| case 'a': | ||||
| $a; | ||||
| case 'b': | ||||
| $b; | ||||
| } | ||||
| ` | ||||
| 	actual := o.String() | ||||
| 
 | ||||
| 	if expected != actual { | ||||
| 		t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintUse(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user