[refactoring] update dumper
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,62 +1,76 @@
|
||||
package visitor_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"bytes"
|
||||
"github.com/z7zmey/php-parser/pkg/position"
|
||||
"github.com/z7zmey/php-parser/pkg/token"
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/ast/traverser"
|
||||
"github.com/z7zmey/php-parser/pkg/ast/visitor"
|
||||
"github.com/z7zmey/php-parser/pkg/token"
|
||||
)
|
||||
|
||||
func ExampleDump() {
|
||||
stxTree := &ast.Root{
|
||||
func TestDumper_root(t *testing.T) {
|
||||
o := bytes.NewBufferString("")
|
||||
|
||||
p := visitor.NewDump(o)
|
||||
n := &ast.Root{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 2,
|
||||
StartPos: 3,
|
||||
EndPos: 4,
|
||||
},
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.Identifier{},
|
||||
&ast.Parameter{
|
||||
Var: &ast.ExprVariable{},
|
||||
},
|
||||
&ast.StmtInlineHtml{
|
||||
Value: []byte("foo"),
|
||||
},
|
||||
&ast.StmtNop{},
|
||||
},
|
||||
EndTkn: &token.Token{
|
||||
ID: token.T_WHITESPACE,
|
||||
Value: []byte(" "),
|
||||
FreeFloating: []*token.Token{
|
||||
{
|
||||
ID: token.T_WHITESPACE,
|
||||
Value: []byte(" "),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 2,
|
||||
StartPos: 3,
|
||||
EndPos: 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
n.Accept(p)
|
||||
|
||||
traverser.NewDFS(visitor.NewDump(os.Stdout)).Traverse(stxTree)
|
||||
expected := `&ast.Root{
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 2,
|
||||
StartPos: 3,
|
||||
EndPos: 4,
|
||||
},
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtNop{
|
||||
},
|
||||
},
|
||||
EndTkn: &token.Token{
|
||||
FreeFloating: []*token.Token{
|
||||
{
|
||||
ID: token.T_WHITESPACE,
|
||||
Value: []byte(" "),
|
||||
Position: &position.Position{
|
||||
StartLine: 1,
|
||||
EndLine: 2,
|
||||
StartPos: 3,
|
||||
EndPos: 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
`
|
||||
actual := o.String()
|
||||
|
||||
//output:
|
||||
//&ast.Root{
|
||||
// Node: ast.Node{
|
||||
// Tokens: token.Collection{
|
||||
// token.Start: []*token.Token{
|
||||
// {
|
||||
// ID: token.T_WHITESPACE,
|
||||
// Value: []byte(" "),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// Position: &position.Position{
|
||||
// StartLine: 1,
|
||||
// EndLine: 1,
|
||||
// StartPos: 0,
|
||||
// EndPos: 1,
|
||||
// },
|
||||
// },
|
||||
// Stmts: []ast.Vertex{
|
||||
// &ast.Identifier{
|
||||
// Value: []byte(""),
|
||||
// },
|
||||
// &ast.Parameter{
|
||||
// Var: &ast.ExprVariable{
|
||||
// },
|
||||
// },
|
||||
// &ast.StmtInlineHtml{
|
||||
// Value: []byte("foo"),
|
||||
// },
|
||||
// },
|
||||
//}
|
||||
if expected != actual {
|
||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ func TestResolveNew(t *testing.T) {
|
||||
},
|
||||
},
|
||||
&ast.ExprNew{
|
||||
Class: nameBC,
|
||||
Class: nameBC,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -227,7 +227,7 @@ func TestResolveFunctionCall(t *testing.T) {
|
||||
},
|
||||
},
|
||||
&ast.ExprFunctionCall{
|
||||
Function: nameB,
|
||||
Function: nameB,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -309,10 +309,10 @@ func TestResolveGroupUse(t *testing.T) {
|
||||
Const: nameC,
|
||||
},
|
||||
&ast.ExprFunctionCall{
|
||||
Function: nameF,
|
||||
Function: nameF,
|
||||
},
|
||||
&ast.ExprFunctionCall{
|
||||
Function: nameE,
|
||||
Function: nameE,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -649,8 +649,8 @@ func TestResolveNamespaces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
&ast.ExprStaticCall{
|
||||
Class: nameFG,
|
||||
Call: &ast.Identifier{Value: []byte("foo")},
|
||||
Class: nameFG,
|
||||
Call: &ast.Identifier{Value: []byte("foo")},
|
||||
},
|
||||
&ast.StmtNamespace{
|
||||
Stmts: []ast.Vertex{},
|
||||
@@ -666,12 +666,12 @@ func TestResolveNamespaces(t *testing.T) {
|
||||
},
|
||||
},
|
||||
&ast.ExprStaticCall{
|
||||
Class: relativeNameCE,
|
||||
Call: &ast.Identifier{Value: []byte("foo")},
|
||||
Class: relativeNameCE,
|
||||
Call: &ast.Identifier{Value: []byte("foo")},
|
||||
},
|
||||
&ast.ExprStaticCall{
|
||||
Class: nameCF,
|
||||
Call: &ast.Identifier{Value: []byte("foo")},
|
||||
Class: nameCF,
|
||||
Call: &ast.Identifier{Value: []byte("foo")},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -697,8 +697,8 @@ func TestResolveStaticCallDinamicClassName(t *testing.T) {
|
||||
stxTree := &ast.StmtStmtList{
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.ExprStaticCall{
|
||||
Class: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}},
|
||||
Call: &ast.Identifier{Value: []byte("foo")},
|
||||
Class: &ast.ExprVariable{VarName: &ast.Identifier{Value: []byte("foo")}},
|
||||
Call: &ast.Identifier{Value: []byte("foo")},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -917,20 +917,20 @@ func TestDoNotResolveReservedSpecialNames(t *testing.T) {
|
||||
Stmts: []ast.Vertex{
|
||||
&ast.StmtExpression{
|
||||
Expr: &ast.ExprStaticCall{
|
||||
Class: nameSelf,
|
||||
Call: &ast.Identifier{Value: []byte("func")},
|
||||
Class: nameSelf,
|
||||
Call: &ast.Identifier{Value: []byte("func")},
|
||||
},
|
||||
},
|
||||
&ast.StmtExpression{
|
||||
Expr: &ast.ExprStaticCall{
|
||||
Class: nameStatic,
|
||||
Call: &ast.Identifier{Value: []byte("func")},
|
||||
Class: nameStatic,
|
||||
Call: &ast.Identifier{Value: []byte("func")},
|
||||
},
|
||||
},
|
||||
&ast.StmtExpression{
|
||||
Expr: &ast.ExprStaticCall{
|
||||
Class: nameParent,
|
||||
Call: &ast.Identifier{Value: []byte("func")},
|
||||
Class: nameParent,
|
||||
Call: &ast.Identifier{Value: []byte("func")},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user