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

297 lines
5.8 KiB
Go
Raw Normal View History

2018-02-19 11:12:09 +00:00
package binary_test
2018-02-09 18:25:38 +00:00
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"
2018-02-19 11:12:09 +00:00
"github.com/z7zmey/php-parser/node/expr/binary"
2018-02-09 18:25:38 +00:00
"github.com/z7zmey/php-parser/walker"
)
var nodesToTest = []struct {
node node.Node // node
expectedVisitedKeys []string // visited keys
expectedAttributes map[string]interface{}
}{
{
2018-02-19 11:12:09 +00:00
&binary.BitwiseAnd{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.BitwiseOr{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.BitwiseXor{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.BooleanAnd{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.BooleanOr{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Coalesce{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Concat{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Div{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Equal{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.GreaterOrEqual{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Greater{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Identical{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.LogicalAnd{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.LogicalOr{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.LogicalXor{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Minus{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Mod{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Mul{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.NotEqual{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.NotIdentical{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Plus{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Pow{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.ShiftLeft{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.ShiftRight{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.SmallerOrEqual{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Smaller{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
map[string]interface{}{},
},
{
2018-02-19 11:12:09 +00:00
&binary.Spaceship{
2018-06-18 20:29:52 +00:00
Left: &expr.Variable{},
Right: &expr.Variable{},
2018-02-09 18:25:38 +00:00
},
[]string{"Left", "Right"},
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-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 18:25:38 +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 18:25:38 +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)
}
}
}