php-parser/node/t_visitor_test.go

104 lines
2.5 KiB
Go
Raw Normal View History

2018-01-13 01:40:08 +00:00
package node_test
import (
"reflect"
"testing"
"github.com/z7zmey/php-parser/node/scalar"
2018-01-17 16:58:45 +00:00
"github.com/z7zmey/php-parser/walker"
2018-01-13 01:40:08 +00:00
"github.com/z7zmey/php-parser/node/expr"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node"
)
var nodesToTest = []struct {
node node.Node // node
expectedVisitedKeys []string // visited keys
expectedAttributes map[string]interface{}
}{
{
&node.Identifier{Value: "foo"},
[]string{},
map[string]interface{}{"Value": "foo"},
},
{
&node.Nullable{Expr: &expr.Variable{VarName: &node.Identifier{Value: "foo"}}},
[]string{"Expr"},
nil,
},
{
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "foo"}}},
[]string{"Expr"},
2018-02-04 19:44:58 +00:00
map[string]interface{}{"IsReference": false, "Variadic": true},
2018-01-13 01:40:08 +00:00
},
{
&node.Parameter{
ByRef: false,
Variadic: true,
VariableType: &node.Identifier{Value: "foo"},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "bar"}},
DefaultValue: &scalar.Lnumber{Value: "0"},
},
[]string{"VariableType", "Variable", "DefaultValue"},
map[string]interface{}{"ByRef": false, "Variadic": true},
},
}
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-01-17 16:58:45 +00:00
func (v *visitorMock) GetChildrenVisitor(key string) walker.Visitor {
2018-01-13 01:40:08 +00:00
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-01-13 01:40:08 +00:00
func TestNameVisitorDisableChildren(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 TestNameVisitor(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)
}
}
}