php-parser/node/t_node_test.go

349 lines
10 KiB
Go
Raw Normal View History

2018-01-13 01:40:08 +00:00
package node_test
import (
"bytes"
"reflect"
"testing"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/expr"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
2018-02-08 10:48:38 +00:00
"github.com/z7zmey/php-parser/php5"
2018-02-04 19:44:58 +00:00
"github.com/z7zmey/php-parser/php7"
2018-01-13 01:40:08 +00:00
)
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 TestIdentifier(t *testing.T) {
src := `<? $foo;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.Variable{
2018-03-18 14:50:19 +00:00
VarName: &node.Identifier{Value: "foo"},
2018-01-13 01:40:08 +00:00
},
},
},
}
2018-02-04 19:44:58 +00:00
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
2018-02-08 10:48:38 +00:00
assertEqual(t, expected, actual)
2018-01-13 01:40:08 +00:00
2018-02-08 10:48:38 +00:00
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
2018-01-13 01:40:08 +00:00
assertEqual(t, expected, actual)
}
2018-02-08 10:48:38 +00:00
func TestPhp7ArgumentNode(t *testing.T) {
2018-01-13 01:40:08 +00:00
src := `<?
foo($a, ...$b);
$foo($a, ...$b);
$foo->bar($a, ...$b);
foo::bar($a, ...$b);
$foo::bar($a, ...$b);
new foo($a, ...$b);
/** anonymous class */
new class ($a, ...$b) {};
`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-01-13 01:40:08 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.FunctionCall{
2018-03-18 14:50:19 +00:00
Function: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
2018-01-13 01:40:08 +00:00
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-01-13 01:40:08 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.MethodCall{
2018-03-18 14:50:19 +00:00
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
2018-01-13 01:40:08 +00:00
Method: &node.Identifier{Value: "bar"},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-01-13 01:40:08 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.StaticCall{
Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
Call: &node.Identifier{Value: "bar"},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-01-13 01:40:08 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.StaticCall{
2018-03-18 14:50:19 +00:00
Class: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
2018-01-13 01:40:08 +00:00
Call: &node.Identifier{Value: "bar"},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-01-13 01:40:08 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.New{
Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-01-13 01:40:08 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.New{
Class: &stmt.Class{
PhpDocComment: "/** anonymous class */",
Args: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-01-13 01:40:08 +00:00
},
Stmts: []node.Node{},
},
},
},
},
}
2018-02-04 19:44:58 +00:00
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
2018-02-08 10:48:38 +00:00
assertEqual(t, expected, actual)
}
func TestPhp5ArgumentNode(t *testing.T) {
src := `<?
foo($a, ...$b);
$foo($a, ...$b);
$foo->bar($a, ...$b);
foo::bar($a, ...$b);
$foo::bar($a, ...$b);
new foo($a, ...$b);
`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.FunctionCall{
Function: &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-02-08 10:48:38 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.FunctionCall{
2018-03-18 14:50:19 +00:00
Function: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
2018-02-08 10:48:38 +00:00
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-02-08 10:48:38 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.MethodCall{
2018-03-18 14:50:19 +00:00
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
2018-02-08 10:48:38 +00:00
Method: &node.Identifier{Value: "bar"},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-02-08 10:48:38 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.StaticCall{
Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
Call: &node.Identifier{Value: "bar"},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-02-08 10:48:38 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.StaticCall{
2018-03-18 14:50:19 +00:00
Class: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
2018-02-08 10:48:38 +00:00
Call: &node.Identifier{Value: "bar"},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-02-08 10:48:38 +00:00
},
},
},
&stmt.Expression{
Expr: &expr.New{
Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}},
Arguments: []node.Node{
2018-03-18 14:50:19 +00:00
&node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "a"}}},
&node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "b"}}},
2018-02-08 10:48:38 +00:00
},
},
},
},
}
2018-01-13 01:40:08 +00:00
2018-02-08 10:48:38 +00:00
actual, _, _ := php5.Parse(bytes.NewBufferString(src), "test.php")
2018-01-13 01:40:08 +00:00
assertEqual(t, expected, actual)
}
2018-02-08 10:48:38 +00:00
func TestPhp7ParameterNode(t *testing.T) {
2018-01-13 01:40:08 +00:00
src := `<?
function foo(?bar $bar=null, baz &...$baz) {}
class foo {public function foo(?bar $bar=null, baz &...$baz) {}}
function(?bar $bar=null, baz &...$baz) {};
static function(?bar $bar=null, baz &...$baz) {};
`
expectedParams := []node.Node{
&node.Parameter{
ByRef: false,
Variadic: false,
VariableType: &node.Nullable{Expr: &name.Name{Parts: []node.Node{&name.NamePart{Value: "bar"}}}},
2018-03-18 14:50:19 +00:00
Variable: &expr.Variable{VarName: &node.Identifier{Value: "bar"}},
2018-01-13 01:40:08 +00:00
DefaultValue: &expr.ConstFetch{Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}}},
},
&node.Parameter{
ByRef: true,
Variadic: true,
VariableType: &name.Name{Parts: []node.Node{&name.NamePart{Value: "baz"}}},
2018-03-18 14:50:19 +00:00
Variable: &expr.Variable{VarName: &node.Identifier{Value: "baz"}},
2018-01-13 01:40:08 +00:00
},
}
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Function{
ReturnsRef: false,
PhpDocComment: "",
FunctionName: &node.Identifier{Value: "foo"},
Params: expectedParams,
Stmts: []node.Node{},
},
&stmt.Class{
ClassName: &node.Identifier{Value: "foo"},
Stmts: []node.Node{
&stmt.ClassMethod{
MethodName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
Params: expectedParams,
Stmts: []node.Node{},
},
},
},
&stmt.Expression{
Expr: &expr.Closure{
Params: expectedParams,
Uses: []node.Node{},
Stmts: []node.Node{},
},
},
&stmt.Expression{
Expr: &expr.Closure{
Static: true,
Params: expectedParams,
Uses: []node.Node{},
Stmts: []node.Node{},
},
},
},
}
2018-02-04 19:44:58 +00:00
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
2018-02-08 10:48:38 +00:00
assertEqual(t, expected, actual)
}
func TestPhp5ParameterNode(t *testing.T) {
src := `<?
function foo(bar $bar=null, baz &...$baz) {}
class foo {public function foo(bar $bar=null, baz &...$baz) {}}
function(bar $bar=null, baz &...$baz) {};
static function(bar $bar=null, baz &...$baz) {};
`
expectedParams := []node.Node{
&node.Parameter{
ByRef: false,
Variadic: false,
VariableType: &name.Name{Parts: []node.Node{&name.NamePart{Value: "bar"}}},
2018-03-18 14:50:19 +00:00
Variable: &expr.Variable{VarName: &node.Identifier{Value: "bar"}},
2018-02-08 10:48:38 +00:00
DefaultValue: &expr.ConstFetch{Constant: &name.Name{Parts: []node.Node{&name.NamePart{Value: "null"}}}},
},
&node.Parameter{
ByRef: true,
Variadic: true,
VariableType: &name.Name{Parts: []node.Node{&name.NamePart{Value: "baz"}}},
2018-03-18 14:50:19 +00:00
Variable: &expr.Variable{VarName: &node.Identifier{Value: "baz"}},
2018-02-08 10:48:38 +00:00
},
}
2018-01-13 01:40:08 +00:00
2018-02-08 10:48:38 +00:00
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Function{
ReturnsRef: false,
PhpDocComment: "",
FunctionName: &node.Identifier{Value: "foo"},
Params: expectedParams,
Stmts: []node.Node{},
},
&stmt.Class{
ClassName: &node.Identifier{Value: "foo"},
Stmts: []node.Node{
&stmt.ClassMethod{
MethodName: &node.Identifier{Value: "foo"},
Modifiers: []node.Node{&node.Identifier{Value: "public"}},
Params: expectedParams,
Stmts: []node.Node{},
},
},
},
&stmt.Expression{
Expr: &expr.Closure{
Params: expectedParams,
Uses: []node.Node{},
Stmts: []node.Node{},
},
},
&stmt.Expression{
Expr: &expr.Closure{
Static: true,
Params: expectedParams,
Uses: []node.Node{},
Stmts: []node.Node{},
},
},
},
}
2018-02-12 10:09:56 +00:00
actual, _, _ := php5.Parse(bytes.NewBufferString(src), "test.php")
2018-01-13 01:40:08 +00:00
assertEqual(t, expected, actual)
}