2018-02-10 00:02:54 +00:00
|
|
|
package expr_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/node/stmt"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/node/scalar"
|
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr"
|
|
|
|
"github.com/z7zmey/php-parser/walker"
|
|
|
|
)
|
|
|
|
|
|
|
|
var nodesToTest = []struct {
|
|
|
|
node node.Node // node
|
|
|
|
expectedVisitedKeys []string // visited keys
|
|
|
|
expectedAttributes map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&expr.ArrayDimFetch{
|
2018-03-18 14:50:19 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
Dim: &scalar.Lnumber{Value: "1"},
|
|
|
|
},
|
|
|
|
[]string{"Variable", "Dim"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.ArrayItem{
|
2018-05-14 15:09:11 +00:00
|
|
|
Key: &scalar.String{Value: "key"},
|
|
|
|
Val: &scalar.Lnumber{Value: "1"},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Key", "Val"},
|
2018-05-14 15:09:11 +00:00
|
|
|
map[string]interface{}{},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Array{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{
|
2018-05-14 15:09:11 +00:00
|
|
|
Key: &scalar.String{Value: "key"},
|
|
|
|
Val: &scalar.Lnumber{Value: "1"},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"Items"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.BitwiseNot{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.BooleanNot{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.ClassConstFetch{
|
2018-03-18 14:50:19 +00:00
|
|
|
Class: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
ConstantName: &node.Identifier{Value: "foo"},
|
|
|
|
},
|
|
|
|
[]string{"Class", "ConstantName"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Clone{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.ClosureUse{
|
2018-03-18 14:50:19 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable"},
|
2018-05-14 15:09:11 +00:00
|
|
|
map[string]interface{}{},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Closure{
|
|
|
|
ReturnsRef: true,
|
|
|
|
Static: false,
|
|
|
|
PhpDocComment: "",
|
|
|
|
Params: []node.Node{&node.Parameter{}},
|
|
|
|
Uses: []node.Node{&expr.ClosureUse{}},
|
|
|
|
ReturnType: &name.Name{},
|
|
|
|
Stmts: []node.Node{&stmt.Nop{}},
|
|
|
|
},
|
|
|
|
[]string{"Params", "Uses", "ReturnType", "Stmts"},
|
|
|
|
map[string]interface{}{"ReturnsRef": true, "Static": false, "PhpDocComment": ""},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.ConstFetch{
|
|
|
|
Constant: &node.Identifier{Value: "foo"},
|
|
|
|
},
|
|
|
|
[]string{"Constant"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Empty{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.ErrorSuppress{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Eval{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Exit{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
2018-02-18 17:09:18 +00:00
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Die{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-18 17:09:18 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.FunctionCall{
|
2018-04-29 16:58:49 +00:00
|
|
|
Function: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
ArgumentList: &node.ArgumentList{},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
[]string{"Function", "ArgumentList"},
|
2018-02-10 00:02:54 +00:00
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.IncludeOnce{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Include{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.InstanceOf{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
Class: &name.Name{},
|
|
|
|
},
|
|
|
|
[]string{"Expr", "Class"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Isset{
|
|
|
|
Variables: []node.Node{
|
2018-03-18 14:50:19 +00:00
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"Variables"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.List{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"Items"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.MethodCall{
|
2018-04-29 16:58:49 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Method: &node.Identifier{Value: "foo"},
|
|
|
|
ArgumentList: &node.ArgumentList{},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
[]string{"Variable", "Method", "ArgumentList"},
|
2018-02-10 00:02:54 +00:00
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.New{
|
2018-04-29 16:58:49 +00:00
|
|
|
Class: &name.Name{},
|
|
|
|
ArgumentList: &node.ArgumentList{},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
[]string{"Class", "ArgumentList"},
|
2018-02-10 00:02:54 +00:00
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.PostDec{
|
2018-03-18 14:50:19 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.PostInc{
|
2018-03-18 14:50:19 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.PreDec{
|
2018-03-18 14:50:19 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.PreInc{
|
2018-03-18 14:50:19 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Variable"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Print{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.PropertyFetch{
|
2018-03-18 14:50:19 +00:00
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
Property: &node.Identifier{Value: "foo"},
|
|
|
|
},
|
|
|
|
[]string{"Variable", "Property"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
2018-05-14 15:09:11 +00:00
|
|
|
{
|
|
|
|
&expr.Reference{
|
|
|
|
Variable: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
},
|
|
|
|
[]string{"Variable"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
2018-02-10 00:02:54 +00:00
|
|
|
{
|
|
|
|
&expr.RequireOnce{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Require{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.ShellExec{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&scalar.EncapsedStringPart{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"Parts"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.ShortArray{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"Items"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.ShortList{
|
|
|
|
Items: []node.Node{
|
|
|
|
&expr.ArrayItem{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"Items"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.StaticCall{
|
2018-04-29 16:58:49 +00:00
|
|
|
Class: &name.Name{},
|
|
|
|
Call: &node.Identifier{Value: "foo"},
|
|
|
|
ArgumentList: &node.ArgumentList{},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
2018-04-29 16:58:49 +00:00
|
|
|
[]string{"Class", "Call", "ArgumentList"},
|
2018-02-10 00:02:54 +00:00
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.StaticPropertyFetch{
|
|
|
|
Class: &name.Name{},
|
|
|
|
Property: &node.Identifier{Value: "foo"},
|
|
|
|
},
|
|
|
|
[]string{"Class", "Property"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Ternary{
|
2018-03-18 14:50:19 +00:00
|
|
|
Condition: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
IfTrue: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
|
|
|
IfFalse: &expr.Variable{VarName: &node.Identifier{Value: "c"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Condition", "IfTrue", "IfFalse"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.UnaryMinus{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.UnaryPlus{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
2018-03-18 14:50:19 +00:00
|
|
|
&expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
[]string{"VarName"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.YieldFrom{
|
2018-03-18 14:50:19 +00:00
|
|
|
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Expr"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&expr.Yield{
|
2018-03-18 14:50:19 +00:00
|
|
|
Key: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
|
|
|
|
Value: &expr.Variable{VarName: &node.Identifier{Value: "b"}},
|
2018-02-10 00:02:54 +00:00
|
|
|
},
|
|
|
|
[]string{"Key", "Value"},
|
|
|
|
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-10 00:02:54 +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-10 00:02:54 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|