pretty printer: print Heredoc
This commit is contained in:
		
							parent
							
								
									9a799fc3be
								
							
						
					
					
						commit
						79277ea3cc
					
				| @ -2,6 +2,7 @@ package printer | ||||
| 
 | ||||
| import ( | ||||
| 	"io" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"github.com/z7zmey/php-parser/node/stmt" | ||||
| 
 | ||||
| @ -99,6 +100,8 @@ func (p *Printer) printNode(n node.Node) { | ||||
| 		p.printScalarEncapsedStringPart(n) | ||||
| 	case *scalar.Encapsed: | ||||
| 		p.printScalarEncapsed(n) | ||||
| 	case *scalar.Heredoc: | ||||
| 		p.printScalarHeredoc(n) | ||||
| 	case *scalar.MagicConstant: | ||||
| 		p.printScalarMagicConstant(n) | ||||
| 
 | ||||
| @ -530,6 +533,20 @@ func (p *Printer) printScalarEncapsed(n node.Node) { | ||||
| 	io.WriteString(p.w, "\"") | ||||
| } | ||||
| 
 | ||||
| func (p *Printer) printScalarHeredoc(n node.Node) { | ||||
| 	nn := n.(*scalar.Heredoc) | ||||
| 
 | ||||
| 	io.WriteString(p.w, "<<<") | ||||
| 	io.WriteString(p.w, nn.Label) | ||||
| 	io.WriteString(p.w, "\n") | ||||
| 
 | ||||
| 	for _, nn := range nn.Parts { | ||||
| 		p.Print(nn) | ||||
| 	} | ||||
| 
 | ||||
| 	io.WriteString(p.w, strings.Trim(nn.Label, "\"'")) | ||||
| } | ||||
| 
 | ||||
| func (p *Printer) printScalarMagicConstant(n node.Node) { | ||||
| 	v := n.(*scalar.MagicConstant).Value | ||||
| 	io.WriteString(p.w, v) | ||||
|  | ||||
| @ -258,6 +258,50 @@ func TestPrintScalarEncapsed(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintScalarHeredoc(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
| 	p := printer.NewPrinter(o, "    ") | ||||
| 	p.Print(&scalar.Heredoc{ | ||||
| 		Label: "LBL", | ||||
| 		Parts: []node.Node{ | ||||
| 			&scalar.EncapsedStringPart{Value: "hello "}, | ||||
| 			&expr.Variable{VarName: &node.Identifier{Value: "var"}}, | ||||
| 			&scalar.EncapsedStringPart{Value: " world\n"}, | ||||
| 		}, | ||||
| 	}) | ||||
| 
 | ||||
| 	expected := `<<<LBL | ||||
| hello $var world | ||||
| LBL` | ||||
| 	actual := o.String() | ||||
| 
 | ||||
| 	if expected != actual { | ||||
| 		t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintScalarNowdoc(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
| 	p := printer.NewPrinter(o, "    ") | ||||
| 	p.Print(&scalar.Heredoc{ | ||||
| 		Label: "'LBL'", | ||||
| 		Parts: []node.Node{ | ||||
| 			&scalar.EncapsedStringPart{Value: "hello world\n"}, | ||||
| 		}, | ||||
| 	}) | ||||
| 
 | ||||
| 	expected := `<<<'LBL' | ||||
| hello world | ||||
| LBL` | ||||
| 	actual := o.String() | ||||
| 
 | ||||
| 	if expected != actual { | ||||
| 		t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPrintScalarMagicConstant(t *testing.T) { | ||||
| 	o := bytes.NewBufferString("") | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user