php-parser/node/expr/cast/t_cast_test.go

229 lines
5.1 KiB
Go
Raw Normal View History

2018-02-09 17:44:17 +00:00
package cast_test
import (
"bytes"
"reflect"
"testing"
"github.com/kylelemons/godebug/pretty"
2018-02-09 17:44:17 +00:00
"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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
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{
2018-02-09 17:50:20 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}