2018-01-13 01:40:08 +00:00
|
|
|
package node_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
"gotest.tools/assert"
|
2018-05-02 09:14:24 +00:00
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr"
|
2018-01-13 01:40:08 +00:00
|
|
|
"github.com/z7zmey/php-parser/node/scalar"
|
2019-02-13 20:18:07 +00:00
|
|
|
"github.com/z7zmey/php-parser/node/stmt"
|
2018-01-17 16:58:45 +00:00
|
|
|
"github.com/z7zmey/php-parser/walker"
|
2018-01-13 01:40:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
{
|
2018-06-18 20:29:52 +00:00
|
|
|
&node.Nullable{Expr: &expr.Variable{}},
|
2018-01-13 01:40:08 +00:00
|
|
|
[]string{"Expr"},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
2018-06-18 20:29:52 +00:00
|
|
|
&node.Argument{Variadic: true, Expr: &expr.Variable{}},
|
2018-01-13 01:40:08 +00:00
|
|
|
[]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"},
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
2018-01-13 01:40:08 +00:00
|
|
|
DefaultValue: &scalar.Lnumber{Value: "0"},
|
|
|
|
},
|
|
|
|
[]string{"VariableType", "Variable", "DefaultValue"},
|
|
|
|
map[string]interface{}{"ByRef": false, "Variadic": true},
|
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
{
|
|
|
|
&node.ArgumentList{
|
|
|
|
Arguments: []node.Node{
|
|
|
|
&node.Argument{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"Arguments"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-04-29 16:58:49 +00:00
|
|
|
},
|
2018-05-02 09:14:24 +00:00
|
|
|
{
|
|
|
|
&node.Root{
|
|
|
|
Stmts: []node.Node{&stmt.Expression{}},
|
|
|
|
},
|
|
|
|
[]string{"Stmts"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-05-02 09:14:24 +00:00
|
|
|
},
|
2018-01-13 01:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-18 20:29:52 +00:00
|
|
|
func (v *visitorMock) LeaveNode(n walker.Walkable) {}
|
|
|
|
func (v *visitorMock) EnterChildNode(key string, w walker.Walkable) {
|
2018-01-13 01:40:08 +00:00
|
|
|
v.visitedKeys = append(v.visitedKeys, key)
|
|
|
|
}
|
2018-06-18 20:29:52 +00:00
|
|
|
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) {}
|
2018-01-13 01:40:08 +00:00
|
|
|
|
|
|
|
func TestNameVisitorDisableChildren(t *testing.T) {
|
|
|
|
for _, tt := range nodesToTest {
|
2019-02-13 20:18:07 +00:00
|
|
|
v := &visitorMock{false, []string{}}
|
2018-01-13 01:40:08 +00:00
|
|
|
tt.node.Walk(v)
|
|
|
|
|
|
|
|
expected := []string{}
|
|
|
|
actual := v.visitedKeys
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-01-13 01:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameVisitor(t *testing.T) {
|
|
|
|
for _, tt := range nodesToTest {
|
2019-02-13 20:18:07 +00:00
|
|
|
v := &visitorMock{true, []string{}}
|
2018-01-13 01:40:08 +00:00
|
|
|
tt.node.Walk(v)
|
|
|
|
|
|
|
|
expected := tt.expectedVisitedKeys
|
|
|
|
actual := v.visitedKeys
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-01-13 01:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test Attributes()
|
|
|
|
|
|
|
|
func TestNameAttributes(t *testing.T) {
|
|
|
|
for _, tt := range nodesToTest {
|
|
|
|
expected := tt.expectedAttributes
|
|
|
|
actual := tt.node.Attributes()
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-01-13 01:40:08 +00:00
|
|
|
}
|
|
|
|
}
|