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 // Assign node
type Assign struct { type Assign struct {
AssignOp Variable node.Node
Expression node.Node
} }
// NewAssign node constuctor // NewAssign node constuctor
func NewAssign(Variable node.Node, Expression node.Node) *Assign { func NewAssign(Variable node.Node, Expression node.Node) *Assign {
return &Assign{ return &Assign{
AssignOp{
Variable, Variable,
Expression, Expression,
},
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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