2023-03-25 20:55:24 +00:00
|
|
|
package traverser_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-12-09 21:36:19 +00:00
|
|
|
"git.maride.cc/maride/php-parser/pkg/ast"
|
|
|
|
"git.maride.cc/maride/php-parser/pkg/visitor"
|
|
|
|
"git.maride.cc/maride/php-parser/pkg/visitor/traverser"
|
2023-03-25 20:55:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// testCase is a traverser that does not go into any class statement or its children.
|
|
|
|
// If it does, the test fails.
|
|
|
|
type testCase struct {
|
|
|
|
t *testing.T
|
|
|
|
visitor.Null
|
|
|
|
traversedFunction bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ast.Visitor = &testCase{}
|
|
|
|
|
|
|
|
func (t *testCase) EnterNode(n ast.Vertex) bool {
|
|
|
|
t.t.Logf("EnterNode: %T", n)
|
|
|
|
if _, ok := n.(*ast.StmtClass); ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCase) LeaveNode(n ast.Vertex) {
|
|
|
|
t.t.Logf("LeaveNode: %T", n)
|
|
|
|
if _, ok := n.(*ast.Root); ok {
|
|
|
|
if !t.traversedFunction {
|
|
|
|
t.t.Error("traverser did not traverse function")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCase) StmtClass(n *ast.StmtClass) {
|
|
|
|
t.t.Errorf("traverser got to class")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCase) StmtClassMethod(n *ast.StmtClassMethod) {
|
|
|
|
t.t.Errorf("traverser got to method")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCase) StmtFunction(n *ast.StmtFunction) {
|
|
|
|
t.traversedFunction = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnterNodeIsRespected(t *testing.T) {
|
|
|
|
tc := &testCase{t: t}
|
|
|
|
tv := traverser.NewTraverser(tc)
|
|
|
|
|
|
|
|
root := &ast.Root{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtFunction{},
|
|
|
|
&ast.StmtClass{
|
|
|
|
Stmts: []ast.Vertex{
|
|
|
|
&ast.StmtClassMethod{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
root.Accept(tv)
|
|
|
|
}
|