assign_op tests

This commit is contained in:
z7zmey 2018-02-10 01:21:54 +02:00
parent 9dbc898d7f
commit d301afb65c
17 changed files with 568 additions and 81 deletions

View File

@ -1,11 +0,0 @@
package assign_op
import (
"github.com/z7zmey/php-parser/node"
)
// AssignOp node
type AssignOp struct {
Variable node.Node
Expression node.Node
}

View File

@ -7,16 +7,15 @@ import (
// Assign node
type Assign struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewAssign node constuctor
func NewAssign(Variable node.Node, Expression node.Node) *Assign {
return &Assign{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// AssignRef node
type AssignRef struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewAssignRef node constuctor
func NewAssignRef(Variable node.Node, Expression node.Node) *AssignRef {
return &AssignRef{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// BitwiseAnd node
type BitwiseAnd struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewBitwiseAnd node constuctor
func NewBitwiseAnd(Variable node.Node, Expression node.Node) *BitwiseAnd {
return &BitwiseAnd{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// BitwiseOr node
type BitwiseOr struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewBitwiseOr node constuctor
func NewBitwiseOr(Variable node.Node, Expression node.Node) *BitwiseOr {
return &BitwiseOr{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// BitwiseXor node
type BitwiseXor struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewBitwiseXor node constuctor
func NewBitwiseXor(Variable node.Node, Expression node.Node) *BitwiseXor {
return &BitwiseXor{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// Concat node
type Concat struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewConcat node constuctor
func NewConcat(Variable node.Node, Expression node.Node) *Concat {
return &Concat{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// Div node
type Div struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewDiv node constuctor
func NewDiv(Variable node.Node, Expression node.Node) *Div {
return &Div{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// Minus node
type Minus struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewMinus node constuctor
func NewMinus(Variable node.Node, Expression node.Node) *Minus {
return &Minus{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// Mod node
type Mod struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewMod node constuctor
func NewMod(Variable node.Node, Expression node.Node) *Mod {
return &Mod{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// Mul node
type Mul struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewMul node constuctor
func NewMul(Variable node.Node, Expression node.Node) *Mul {
return &Mul{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// Plus node
type Plus struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewPlus node constuctor
func NewPlus(Variable node.Node, Expression node.Node) *Plus {
return &Plus{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// Pow node
type Pow struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewPow node constuctor
func NewPow(Variable node.Node, Expression node.Node) *Pow {
return &Pow{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// ShiftLeft node
type ShiftLeft struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewShiftLeft node constuctor
func NewShiftLeft(Variable node.Node, Expression node.Node) *ShiftLeft {
return &ShiftLeft{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -7,16 +7,15 @@ import (
// ShiftRight node
type ShiftRight struct {
AssignOp
Variable node.Node
Expression node.Node
}
// NewShiftRight node constuctor
func NewShiftRight(Variable node.Node, Expression node.Node) *ShiftRight {
return &ShiftRight{
AssignOp{
Variable,
Expression,
},
Variable,
Expression,
}
}

View File

@ -0,0 +1,323 @@
package assign_op_test
import (
"bytes"
"reflect"
"testing"
"github.com/z7zmey/php-parser/node/expr/assign_op"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7"
)
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
diff := pretty.Compare(expected, actual)
if diff != "" {
t.Errorf("diff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("expected and actual are not equal\n")
}
}
}
func TestAssignRef(t *testing.T) {
src := `<? $a =& $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.AssignRef{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestAssign(t *testing.T) {
src := `<? $a = $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Assign{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestBitwiseAnd(t *testing.T) {
src := `<? $a &= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.BitwiseAnd{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestBitwiseOr(t *testing.T) {
src := `<? $a |= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.BitwiseOr{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestBitwiseXor(t *testing.T) {
src := `<? $a ^= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.BitwiseXor{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestConcat(t *testing.T) {
src := `<? $a .= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Concat{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestDiv(t *testing.T) {
src := `<? $a /= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Div{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestMinus(t *testing.T) {
src := `<? $a -= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Minus{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestMod(t *testing.T) {
src := `<? $a %= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Mod{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestMul(t *testing.T) {
src := `<? $a *= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Mul{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestPlus(t *testing.T) {
src := `<? $a += $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Plus{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestPow(t *testing.T) {
src := `<? $a **= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.Pow{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestShiftLeft(t *testing.T) {
src := `<? $a <<= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.ShiftLeft{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestShiftRight(t *testing.T) {
src := `<? $a >>= $b;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &assign_op.ShiftRight{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}

View File

@ -0,0 +1,189 @@
package assign_op_test
import (
"reflect"
"testing"
"github.com/z7zmey/php-parser/node/expr/assign_op"
"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{}
}{
{
&assign_op.AssignRef{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.Assign{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.BitwiseAnd{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.BitwiseOr{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.BitwiseXor{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.Concat{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.Div{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.Minus{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.Mod{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.Mul{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.Plus{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.Pow{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.ShiftLeft{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
map[string]interface{}{},
},
{
&assign_op.ShiftRight{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Expression: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
},
[]string{"Variable", "Expression"},
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)
}
}
}