191 lines
4.3 KiB
Go
191 lines
4.3 KiB
Go
package expr_test
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/z7zmey/php-parser/node/scalar"
|
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr"
|
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
"github.com/z7zmey/php-parser/node/expr/binary"
|
|
"github.com/z7zmey/php-parser/node/stmt"
|
|
"github.com/z7zmey/php-parser/php5"
|
|
"github.com/z7zmey/php-parser/php7"
|
|
)
|
|
|
|
func TestFunctionCall(t *testing.T) {
|
|
src := `<? foo();`
|
|
|
|
expected := &stmt.StmtList{
|
|
Stmts: []node.Node{
|
|
&stmt.Expression{
|
|
Expr: &expr.FunctionCall{
|
|
Function: &name.Name{
|
|
Parts: []node.Node{
|
|
&name.NamePart{Value: "foo"},
|
|
},
|
|
},
|
|
ArgumentList: &node.ArgumentList{},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
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 TestFunctionCallRelative(t *testing.T) {
|
|
src := `<? namespace\foo();`
|
|
|
|
expected := &stmt.StmtList{
|
|
Stmts: []node.Node{
|
|
&stmt.Expression{
|
|
Expr: &expr.FunctionCall{
|
|
Function: &name.Relative{
|
|
Parts: []node.Node{
|
|
&name.NamePart{Value: "foo"},
|
|
},
|
|
},
|
|
ArgumentList: &node.ArgumentList{},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
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 TestFunctionFullyQualified(t *testing.T) {
|
|
src := `<? \foo([]);`
|
|
|
|
expected := &stmt.StmtList{
|
|
Stmts: []node.Node{
|
|
&stmt.Expression{
|
|
Expr: &expr.FunctionCall{
|
|
Function: &name.FullyQualified{
|
|
Parts: []node.Node{
|
|
&name.NamePart{Value: "foo"},
|
|
},
|
|
},
|
|
ArgumentList: &node.ArgumentList{
|
|
Arguments: []node.Node{
|
|
&node.Argument{
|
|
Variadic: false,
|
|
IsReference: false,
|
|
Expr: &expr.ShortArray{
|
|
Items: []node.Node{},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
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 TestFunctionCallVar(t *testing.T) {
|
|
src := `<? $foo(yield $a);`
|
|
|
|
expected := &stmt.StmtList{
|
|
Stmts: []node.Node{
|
|
&stmt.Expression{
|
|
Expr: &expr.FunctionCall{
|
|
Function: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
|
ArgumentList: &node.ArgumentList{
|
|
Arguments: []node.Node{
|
|
&node.Argument{
|
|
Variadic: false,
|
|
IsReference: false,
|
|
Expr: &expr.Yield{
|
|
Value: &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 TestFunctionCallExprArg(t *testing.T) {
|
|
src := `<? ceil($foo/3);`
|
|
|
|
expected := &stmt.StmtList{
|
|
Stmts: []node.Node{
|
|
&stmt.Expression{
|
|
Expr: &expr.FunctionCall{
|
|
Function: &name.Name{
|
|
Parts: []node.Node{
|
|
&name.NamePart{Value: "ceil"},
|
|
},
|
|
},
|
|
ArgumentList: &node.ArgumentList{
|
|
Arguments: []node.Node{
|
|
&node.Argument{
|
|
Variadic: false,
|
|
IsReference: false,
|
|
Expr: &binary.Div{
|
|
Left: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
|
Right: &scalar.Lnumber{Value: "3"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|