php-parser/node/expr/t_visitor_test.go

417 lines
7.0 KiB
Go
Raw Normal View History

2018-02-10 00:02:54 +00:00
package expr_test
import (
"testing"
"gotest.tools/assert"
2018-02-10 00:02:54 +00:00
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/scalar"
"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-06-18 20:29:52 +00:00
Variable: &expr.Variable{},
2018-02-10 00:02:54 +00:00
Dim: &scalar.Lnumber{Value: "1"},
},
[]string{"Variable", "Dim"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.ArrayItem{
2019-12-29 20:42:52 +00:00
Key: &scalar.String{Value: "key"},
Val: &scalar.Lnumber{Value: "1"},
Unpack: true,
2018-02-10 00:02:54 +00:00
},
[]string{"Key", "Val"},
2019-12-29 20:42:52 +00:00
map[string]interface{}{"Unpack": true},
2018-02-10 00:02:54 +00:00
},
{
&expr.Array{
Items: []node.Node{
2018-06-18 20:29:52 +00:00
&expr.ArrayItem{},
2018-02-10 00:02:54 +00:00
},
},
[]string{"Items"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.BitwiseNot{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.BooleanNot{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.ClassConstFetch{
2018-06-18 20:29:52 +00:00
Class: &expr.Variable{},
2018-02-10 00:02:54 +00:00
ConstantName: &node.Identifier{Value: "foo"},
},
[]string{"Class", "ConstantName"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Clone{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.ClosureUse{
Uses: []node.Node{
2018-06-18 20:29:52 +00:00
&expr.Variable{},
},
2018-02-10 00:02:54 +00:00
},
[]string{"Uses"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Closure{
ReturnsRef: true,
Static: false,
PhpDocComment: "",
Params: []node.Node{&node.Parameter{}},
ClosureUse: &expr.ClosureUse{},
2018-02-10 00:02:54 +00:00
ReturnType: &name.Name{},
Stmts: []node.Node{&stmt.Nop{}},
},
[]string{"Params", "ClosureUse", "ReturnType", "Stmts"},
2018-02-10 00:02:54 +00:00
map[string]interface{}{"ReturnsRef": true, "Static": false, "PhpDocComment": ""},
},
{
&expr.ArrowFunction{
ReturnsRef: true,
Static: false,
PhpDocComment: "",
Params: []node.Node{&node.Parameter{}},
ReturnType: &name.Name{},
Expr: &expr.Variable{},
},
[]string{"Params", "ReturnType", "Expr"},
map[string]interface{}{"ReturnsRef": true, "Static": false, "PhpDocComment": ""},
},
2018-02-10 00:02:54 +00:00
{
&expr.ConstFetch{
Constant: &node.Identifier{Value: "foo"},
},
[]string{"Constant"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Empty{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.ErrorSuppress{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Eval{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Exit{
Die: true,
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
map[string]interface{}{"Die": true},
2018-02-10 00:02:54 +00:00
},
{
&expr.FunctionCall{
2018-06-18 20:29:52 +00:00
Function: &expr.Variable{},
2018-04-29 16:58:49 +00:00
ArgumentList: &node.ArgumentList{},
2018-02-10 00:02:54 +00:00
},
2018-04-29 16:58:49 +00:00
[]string{"Function", "ArgumentList"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.IncludeOnce{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Include{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.InstanceOf{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
Class: &name.Name{},
},
[]string{"Expr", "Class"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Isset{
Variables: []node.Node{
2018-06-18 20:29:52 +00:00
&expr.Variable{},
2018-02-10 00:02:54 +00:00
},
},
[]string{"Variables"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.List{
Items: []node.Node{
&expr.ArrayItem{},
},
},
[]string{"Items"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.MethodCall{
2018-06-18 20:29:52 +00:00
Variable: &expr.Variable{},
2018-04-29 16:58:49 +00:00
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"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&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"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.PostDec{
2018-06-18 20:29:52 +00:00
Variable: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Variable"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.PostInc{
2018-06-18 20:29:52 +00:00
Variable: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Variable"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.PreDec{
2018-06-18 20:29:52 +00:00
Variable: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Variable"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.PreInc{
2018-06-18 20:29:52 +00:00
Variable: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Variable"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Print{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.PropertyFetch{
2018-06-18 20:29:52 +00:00
Variable: &expr.Variable{},
2018-02-10 00:02:54 +00:00
Property: &node.Identifier{Value: "foo"},
},
[]string{"Variable", "Property"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Reference{
2018-06-18 20:29:52 +00:00
Variable: &expr.Variable{},
},
[]string{"Variable"},
nil,
},
2018-02-10 00:02:54 +00:00
{
&expr.RequireOnce{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Require{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.ShellExec{
Parts: []node.Node{
&scalar.EncapsedStringPart{},
},
},
[]string{"Parts"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.ShortArray{
Items: []node.Node{
&expr.ArrayItem{},
},
},
[]string{"Items"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.ShortList{
Items: []node.Node{
&expr.ArrayItem{},
},
},
[]string{"Items"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&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"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.StaticPropertyFetch{
Class: &name.Name{},
Property: &node.Identifier{Value: "foo"},
},
[]string{"Class", "Property"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Ternary{
2018-06-18 20:29:52 +00:00
Condition: &expr.Variable{},
IfTrue: &expr.Variable{},
IfFalse: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Condition", "IfTrue", "IfFalse"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.UnaryMinus{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.UnaryPlus{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
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"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.YieldFrom{
2018-06-18 20:29:52 +00:00
Expr: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Expr"},
nil,
2018-02-10 00:02:54 +00:00
},
{
&expr.Yield{
2018-06-18 20:29:52 +00:00
Key: &expr.Variable{},
Value: &expr.Variable{},
2018-02-10 00:02:54 +00:00
},
[]string{"Key", "Value"},
nil,
2018-02-10 00:02: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) {
v.visitedKeys = append(v.visitedKeys, key)
}
func (v *visitorMock) LeaveChildNode(key string, w walker.Walkable) {}
func (v *visitorMock) EnterChildList(key string, w walker.Walkable) {
2018-02-10 00:02:54 +00:00
v.visitedKeys = append(v.visitedKeys, key)
}
2018-06-18 20:29:52 +00:00
func (v *visitorMock) LeaveChildList(key string, w walker.Walkable) {}
2018-02-10 00:02:54 +00:00
func TestVisitorDisableChildren(t *testing.T) {
for _, tt := range nodesToTest {
v := &visitorMock{false, []string{}}
2018-02-10 00:02:54 +00:00
tt.node.Walk(v)
expected := []string{}
actual := v.visitedKeys
assert.DeepEqual(t, expected, actual)
2018-02-10 00:02:54 +00:00
}
}
func TestVisitor(t *testing.T) {
for _, tt := range nodesToTest {
v := &visitorMock{true, nil}
tt.node.Walk(v)
expected := tt.expectedVisitedKeys
actual := v.visitedKeys
assert.DeepEqual(t, expected, actual)
2018-02-10 00:02:54 +00:00
}
}
// test Attributes()
func TestNameAttributes(t *testing.T) {
for _, tt := range nodesToTest {
expected := tt.expectedAttributes
actual := tt.node.Attributes()
assert.DeepEqual(t, expected, actual)
2018-02-10 00:02:54 +00:00
}
}