120 lines
2.2 KiB
Go
120 lines
2.2 KiB
Go
package cast_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gotest.tools/assert"
|
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
"github.com/z7zmey/php-parser/node/expr"
|
|
"github.com/z7zmey/php-parser/node/expr/cast"
|
|
"github.com/z7zmey/php-parser/walker"
|
|
)
|
|
|
|
var nodesToTest = []struct {
|
|
node node.Node // node
|
|
expectedVisitedKeys []string // visited keys
|
|
expectedAttributes map[string]interface{}
|
|
}{
|
|
{
|
|
&cast.Array{
|
|
Expr: &expr.Variable{},
|
|
},
|
|
[]string{"Expr"},
|
|
nil,
|
|
},
|
|
{
|
|
&cast.Bool{
|
|
Expr: &expr.Variable{},
|
|
},
|
|
[]string{"Expr"},
|
|
nil,
|
|
},
|
|
{
|
|
&cast.Double{
|
|
Expr: &expr.Variable{},
|
|
},
|
|
[]string{"Expr"},
|
|
nil,
|
|
},
|
|
{
|
|
&cast.Int{
|
|
Expr: &expr.Variable{},
|
|
},
|
|
[]string{"Expr"},
|
|
nil,
|
|
},
|
|
{
|
|
&cast.Object{
|
|
Expr: &expr.Variable{},
|
|
},
|
|
[]string{"Expr"},
|
|
nil,
|
|
},
|
|
{
|
|
&cast.String{
|
|
Expr: &expr.Variable{},
|
|
},
|
|
[]string{"Expr"},
|
|
nil,
|
|
},
|
|
{
|
|
&cast.Unset{
|
|
Expr: &expr.Variable{},
|
|
},
|
|
[]string{"Expr"},
|
|
nil,
|
|
},
|
|
}
|
|
|
|
type visitorMock struct {
|
|
visitChildren bool
|
|
visitedKeys []string
|
|
}
|
|
|
|
func (v *visitorMock) EnterNode(n walker.Walkable) bool { return v.visitChildren }
|
|
func (v *visitorMock) LeaveNode(n walker.Walkable) {}
|
|
func (v *visitorMock) EnterChildNode(key string, w walker.Walkable) {
|
|
v.visitedKeys = append(v.visitedKeys, key)
|
|
}
|
|
func (v *visitorMock) LeaveChildNode(key string, w walker.Walkable) {}
|
|
func (v *visitorMock) EnterChildList(key string, w walker.Walkable) {
|
|
v.visitedKeys = append(v.visitedKeys, key)
|
|
}
|
|
func (v *visitorMock) LeaveChildList(key string, w walker.Walkable) {}
|
|
|
|
func TestVisitorDisableChildren(t *testing.T) {
|
|
for _, tt := range nodesToTest {
|
|
v := &visitorMock{false, []string{}}
|
|
tt.node.Walk(v)
|
|
|
|
expected := []string{}
|
|
actual := v.visitedKeys
|
|
|
|
assert.DeepEqual(t, expected, actual)
|
|
}
|
|
}
|
|
|
|
func TestVisitor(t *testing.T) {
|
|
for _, tt := range nodesToTest {
|
|
v := &visitorMock{true, []string{}}
|
|
tt.node.Walk(v)
|
|
|
|
expected := tt.expectedVisitedKeys
|
|
actual := v.visitedKeys
|
|
|
|
assert.DeepEqual(t, expected, actual)
|
|
}
|
|
}
|
|
|
|
// test Attributes()
|
|
|
|
func TestNameAttributes(t *testing.T) {
|
|
for _, tt := range nodesToTest {
|
|
expected := tt.expectedAttributes
|
|
actual := tt.node.Attributes()
|
|
|
|
assert.DeepEqual(t, expected, actual)
|
|
}
|
|
}
|