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

269 lines
6.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")
}
}
}
2018-04-05 08:59:29 +00:00
func TestArray(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (array)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Array{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
2018-04-05 08:59:29 +00:00
func TestBool(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (boolean)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Bool{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
2018-04-05 08:59:29 +00:00
func TestBoolShort(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (bool)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Bool{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
2018-04-05 08:59:29 +00:00
func TestDouble(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (double)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Double{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
func TestCastFloat(t *testing.T) {
src := `<? (float)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Double{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
2018-04-05 08:59:29 +00:00
func TestInt(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (integer)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Int{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
2018-04-05 08:59:29 +00:00
func TestIntShort(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (int)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Int{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
2018-04-05 08:59:29 +00:00
func TestObject(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (object)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Object{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
2018-04-05 08:59:29 +00:00
func TestString(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (string)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.String{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}
2018-04-05 08:59:29 +00:00
func TestUnset(t *testing.T) {
2018-02-09 17:44:17 +00:00
src := `<? (unset)$a;`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-09 17:44:17 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-04-05 08:59:29 +00:00
Expr: &cast.Unset{
2018-03-18 14:50:19 +00:00
Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}},
2018-02-09 17:44:17 +00:00
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-09 17:44:17 +00:00
assertEqual(t, expected, actual)
}