php-parser/node/expr/cast/t_cast_test.go
2018-06-25 23:30:10 +03:00

668 lines
14 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"
"github.com/z7zmey/php-parser/position"
)
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &cast.Array{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
Expr: &cast.Bool{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &cast.Bool{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 11,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 11,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &cast.Double{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &cast.Double{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
Expr: &cast.Int{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Expr: &cast.Int{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 10,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 10,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &cast.Object{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &cast.String{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
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 TestBinaryString(t *testing.T) {
src := `<? (binary)$a;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 14,
},
Expr: &cast.String{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
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{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Expr: &cast.Unset{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 12,
},
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 11,
EndPos: 12,
},
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)
}