2018-02-19 11:12:09 +00:00
|
|
|
package assign_test
|
2018-02-09 23:21:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
"gotest.tools/assert"
|
2018-02-09 23:21:54 +00:00
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr"
|
2019-02-13 20:18:07 +00:00
|
|
|
"github.com/z7zmey/php-parser/node/expr/assign"
|
2018-02-09 23:21:54 +00:00
|
|
|
"github.com/z7zmey/php-parser/walker"
|
|
|
|
)
|
|
|
|
|
|
|
|
var nodesToTest = []struct {
|
|
|
|
node node.Node // node
|
|
|
|
expectedVisitedKeys []string // visited keys
|
|
|
|
expectedAttributes map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
2018-04-05 09:03:32 +00:00
|
|
|
&assign.Reference{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.Assign{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.BitwiseAnd{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.BitwiseOr{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.BitwiseXor{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.Concat{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.Div{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.Minus{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.Mod{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.Mul{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.Plus{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.Pow{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.ShiftLeft{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-02-19 11:12:09 +00:00
|
|
|
&assign.ShiftRight{
|
2018-06-18 20:29:52 +00:00
|
|
|
Variable: &expr.Variable{},
|
|
|
|
Expression: &expr.Variable{},
|
2018-02-09 23:21:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable", "Expression"},
|
2019-02-13 20:18:07 +00:00
|
|
|
nil,
|
2018-02-09 23:21:54 +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-02-09 23:21:54 +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-02-09 23:21:54 +00:00
|
|
|
|
|
|
|
func TestVisitorDisableChildren(t *testing.T) {
|
|
|
|
for _, tt := range nodesToTest {
|
2019-02-13 20:18:07 +00:00
|
|
|
v := &visitorMock{false, []string{}}
|
2018-02-09 23:21:54 +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-02-09 23:21:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVisitor(t *testing.T) {
|
|
|
|
for _, tt := range nodesToTest {
|
2019-02-13 20:18:07 +00:00
|
|
|
v := &visitorMock{true, []string{}}
|
2018-02-09 23:21:54 +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-02-09 23:21:54 +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-02-09 23:21:54 +00:00
|
|
|
}
|
|
|
|
}
|