php-parser/node/expr/binary_op/t_visitor_test.go

293 lines
7.6 KiB
Go
Raw Normal View History

2018-02-09 18:25:38 +00:00
package binary_op_test
import (
"reflect"
"testing"
2018-02-09 23:02:50 +00:00
2018-02-09 18:25:38 +00:00
"github.com/kylelemons/godebug/pretty"
2018-02-09 23:02:50 +00:00
2018-02-09 18:25:38 +00:00
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/expr/binary_op"
"github.com/z7zmey/php-parser/walker"
)
var nodesToTest = []struct {
node node.Node // node
expectedVisitedKeys []string // visited keys
expectedAttributes map[string]interface{}
}{
{
&binary_op.BitwiseAnd{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.BitwiseOr{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.BitwiseXor{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.BooleanAnd{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.BooleanOr{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Coalesce{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Concat{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Div{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Equal{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.GreaterOrEqual{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Greater{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Identical{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.LogicalAnd{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.LogicalOr{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.LogicalXor{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Minus{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Mod{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Mul{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.NotEqual{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.NotIdentical{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Plus{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Pow{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.ShiftLeft{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.ShiftRight{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.SmallerOrEqual{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Smaller{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
&binary_op.Spaceship{
2018-02-09 23:02:50 +00:00
Left: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Right: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
}
type visitorMock struct {
visitChildren bool
visitedKeys []string
}
func (v *visitorMock) EnterNode(n walker.Walker) bool { return v.visitChildren }
func (v *visitorMock) GetChildrenVisitor(key string) walker.Visitor {
v.visitedKeys = append(v.visitedKeys, key)
return &visitorMock{v.visitChildren, nil}
}
func (v *visitorMock) LeaveNode(n walker.Walker) {}
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)
}
}
}