php-parser/node/expr/cast/t_visitor_test.go

126 lines
2.7 KiB
Go
Raw Normal View History

2018-02-09 17:44:17 +00:00
package cast_test
import (
"reflect"
"testing"
2018-02-09 17:44:17 +00:00
"github.com/kylelemons/godebug/pretty"
2018-02-09 17:44:17 +00:00
"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{}
}{
{
2018-04-05 08:59:29 +00:00
&cast.Array{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
[]string{"Expr"},
map[string]interface{}{},
},
{
2018-04-05 08:59:29 +00:00
&cast.Bool{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
[]string{"Expr"},
map[string]interface{}{},
},
{
2018-04-05 08:59:29 +00:00
&cast.Double{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
[]string{"Expr"},
map[string]interface{}{},
},
{
2018-04-05 08:59:29 +00:00
&cast.Int{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
[]string{"Expr"},
map[string]interface{}{},
},
{
2018-04-05 08:59:29 +00:00
&cast.Object{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
[]string{"Expr"},
map[string]interface{}{},
},
{
2018-04-05 08:59:29 +00:00
&cast.String{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
[]string{"Expr"},
map[string]interface{}{},
},
{
2018-04-05 08:59:29 +00:00
&cast.Unset{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
[]string{"Expr"},
map[string]interface{}{},
},
}
type visitorMock struct {
visitChildren bool
visitedKeys []string
}
2018-02-20 17:52:07 +00:00
func (v *visitorMock) EnterNode(n walker.Walkable) bool { return v.visitChildren }
2018-02-09 17:44:17 +00:00
func (v *visitorMock) GetChildrenVisitor(key string) walker.Visitor {
v.visitedKeys = append(v.visitedKeys, key)
return &visitorMock{v.visitChildren, nil}
}
2018-02-20 17:52:07 +00:00
func (v *visitorMock) LeaveNode(n walker.Walkable) {}
2018-02-09 17:44:17 +00:00
func TestVisitorDisableChildren(t *testing.T) {
for _, tt := range nodesToTest {
v := &visitorMock{false, nil}
tt.node.Walk(v)
expected := []string{}
actual := v.visitedKeys
diff := pretty.Compare(expected, actual)
if diff != "" {
t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff)
}
}
}
func TestVisitor(t *testing.T) {
for _, tt := range nodesToTest {
v := &visitorMock{true, nil}
tt.node.Walk(v)
expected := tt.expectedVisitedKeys
actual := v.visitedKeys
diff := pretty.Compare(expected, actual)
if diff != "" {
t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff)
}
}
}
// test Attributes()
func TestNameAttributes(t *testing.T) {
for _, tt := range nodesToTest {
expected := tt.expectedAttributes
actual := tt.node.Attributes()
diff := pretty.Compare(expected, actual)
if diff != "" {
t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff)
}
}
}