2018-02-09 17:44:17 +00:00
|
|
|
package cast_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
2018-02-19 11:00:58 +00:00
|
|
|
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCastFloat(t *testing.T) {
|
|
|
|
src := `<? (float)$a;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
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;`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
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-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-09 17:44:17 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|