Cast tests

This commit is contained in:
z7zmey 2018-02-09 19:44:17 +02:00
parent 31b81540f3
commit 6fdebcbcb3
2 changed files with 387 additions and 0 deletions

View File

@ -0,0 +1,248 @@
package cast_test
import (
"bytes"
"reflect"
"testing"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/expr/cast"
"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 TestCastArray(t *testing.T) {
src := `<? (array)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastArray{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastBool(t *testing.T) {
src := `<? (boolean)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastBool{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastBoolShort(t *testing.T) {
src := `<? (bool)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastBool{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastDouble(t *testing.T) {
src := `<? (double)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastDouble{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastFloat(t *testing.T) {
src := `<? (float)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastDouble{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastInt(t *testing.T) {
src := `<? (integer)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastInt{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastIntShort(t *testing.T) {
src := `<? (int)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastInt{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastObject(t *testing.T) {
src := `<? (object)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastObject{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastString(t *testing.T) {
src := `<? (string)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastString{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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 TestCastUnset(t *testing.T) {
src := `<? (unset)$a;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.CastUnset{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
},
},
}
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,139 @@
package cast_test
import (
"reflect"
"testing"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/expr/cast"
"github.com/z7zmey/php-parser/walker"
)
var nodesToTest = []struct {
node node.Node // node
expectedVisitedKeys []string // visited keys
expectedAttributes map[string]interface{}
}{
{
&cast.CastArray{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
[]string{"Expr"},
map[string]interface{}{},
},
{
&cast.CastBool{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
[]string{"Expr"},
map[string]interface{}{},
},
{
&cast.CastDouble{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
[]string{"Expr"},
map[string]interface{}{},
},
{
&cast.CastInt{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
[]string{"Expr"},
map[string]interface{}{},
},
{
&cast.CastObject{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
[]string{"Expr"},
map[string]interface{}{},
},
{
&cast.CastString{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
[]string{"Expr"},
map[string]interface{}{},
},
{
&cast.CastUnset{
Cast: cast.Cast{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
},
},
[]string{"Expr"},
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)
}
}
}