php-parser/node/expr/cast/t_cast_test.go
2018-06-02 18:57:30 +03:00

269 lines
6.1 KiB
Go

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 TestArray(t *testing.T) {
src := `<? (array)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Array{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestBool(t *testing.T) {
src := `<? (boolean)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Bool{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestBoolShort(t *testing.T) {
src := `<? (bool)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Bool{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestDouble(t *testing.T) {
src := `<? (double)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Double{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestCastFloat(t *testing.T) {
src := `<? (float)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Double{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestInt(t *testing.T) {
src := `<? (integer)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Int{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestIntShort(t *testing.T) {
src := `<? (int)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Int{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestObject(t *testing.T) {
src := `<? (object)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Object{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestString(t *testing.T) {
src := `<? (string)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.String{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestUnset(t *testing.T) {
src := `<? (unset)$a;`
expected := &node.Root{
Stmts: []node.Node{
&stmt.Expression{
Expr: &cast.Unset{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
assertEqual(t, expected, actual)
}