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

323 lines
6.8 KiB
Go

package expr_test
import (
"bytes"
"testing"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7"
)
func TestNew(t *testing.T) {
src := `<? new Foo;`
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: &expr.New{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 10,
},
Class: &name.Name{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
Parts: []node.Node{
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 10,
},
Value: "Foo",
},
},
},
},
},
},
}
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 TestNewRelative(t *testing.T) {
src := `<? new namespace\Foo();`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 23,
},
Expr: &expr.New{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 22,
},
Class: &name.Relative{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 20,
},
Parts: []node.Node{
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
Value: "Foo",
},
},
},
ArgumentList: &node.ArgumentList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 21,
EndPos: 22,
},
},
},
},
},
}
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 TestNewFullyQualified(t *testing.T) {
src := `<? new \Foo();`
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: &expr.New{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 13,
},
Class: &name.FullyQualified{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 11,
},
Parts: []node.Node{
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 11,
},
Value: "Foo",
},
},
},
ArgumentList: &node.ArgumentList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
},
},
},
},
}
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 TestNewAnonymous(t *testing.T) {
src := `<? new class ($a, ...$b) {};`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 28,
},
Stmts: []node.Node{
&stmt.Expression{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 28,
},
Expr: &expr.New{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 27,
},
Class: &stmt.Class{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 27,
},
PhpDocComment: "",
ArgumentList: &node.ArgumentList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 24,
},
Arguments: []node.Node{
&node.Argument{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 16,
},
Variadic: false,
IsReference: false,
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 16,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 16,
},
Value: "a",
},
},
},
&node.Argument{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 23,
},
IsReference: false,
Variadic: true,
Expr: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 23,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 22,
EndPos: 23,
},
Value: "b",
},
},
},
},
},
Stmts: []node.Node{},
},
},
},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
}