From 0dac524c626b197489beb3b98cc9025f7212fd93 Mon Sep 17 00:00:00 2001 From: z7zmey Date: Thu, 8 Feb 2018 12:48:38 +0200 Subject: [PATCH] add php5 tests --- node/name/t_name_test.go | 93 +-- node/name/t_visitor_test.go | 95 +++ node/scalar/t_encapsed_test.go | 33 +- node/scalar/t_magic_constant_test.go | 9 +- node/scalar/t_numbers_test.go | 15 + node/scalar/t_string_test.go | 50 +- node/stmt/t_alt_if_test.go | 15 +- node/stmt/t_break_test.go | 10 + node/t_node_test.go | 152 ++++- php5/php5.go | 910 ++++++++++++++------------- php5/php5.y | 12 +- 11 files changed, 800 insertions(+), 594 deletions(-) create mode 100644 node/name/t_visitor_test.go diff --git a/node/name/t_name_test.go b/node/name/t_name_test.go index b76fb1b..d14087c 100644 --- a/node/name/t_name_test.go +++ b/node/name/t_name_test.go @@ -7,11 +7,11 @@ import ( "github.com/z7zmey/php-parser/node/expr" "github.com/z7zmey/php-parser/node/name" - "github.com/z7zmey/php-parser/walker" "github.com/kylelemons/godebug/pretty" "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" ) @@ -45,7 +45,9 @@ func TestName(t *testing.T) { } actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) + actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php") assertEqual(t, expected, actual) } @@ -66,7 +68,9 @@ func TestFullyQualified(t *testing.T) { } actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) + actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php") assertEqual(t, expected, actual) } @@ -87,91 +91,8 @@ func TestRelative(t *testing.T) { } actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) + actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php") assertEqual(t, expected, actual) } - -// Test Name nodes visitors - -var nameNodesTests = []struct { - node node.Node // node - expectedVisitedKeys []string // visited keys - expectedAttributes map[string]interface{} -}{ - { - &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}}, - []string{"Parts"}, - nil, - }, - { - &name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "foo"}}}, - []string{"Parts"}, - nil, - }, - { - &name.Relative{Parts: []node.Node{&name.NamePart{Value: "foo"}}}, - []string{"Parts"}, - nil, - }, - { - &name.NamePart{Value: "foo"}, - []string{}, - map[string]interface{}{"Value": "foo"}, - }, -} - -type visitorMock struct { - visitChildren bool - visitedKeys []string -} - -func (v *visitorMock) EnterNode(n walker.Walker) bool { return v.visitChildren } -func (v *visitorMock) GetChildrenVisitor(key string) walker.Visitor { - v.visitedKeys = append(v.visitedKeys, key) - return &visitorMock{v.visitChildren, nil} -} -func (v *visitorMock) LeaveNode(n walker.Walker) {} - -func TestNameVisitorDisableChildren(t *testing.T) { - for _, tt := range nameNodesTests { - v := &visitorMock{false, nil} - tt.node.Walk(v) - - expected := []string{} - actual := v.visitedKeys - - diff := pretty.Compare(expected, actual) - if diff != "" { - t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) - } - } -} - -func TestNameVisitor(t *testing.T) { - for _, tt := range nameNodesTests { - v := &visitorMock{true, nil} - tt.node.Walk(v) - - expected := tt.expectedVisitedKeys - actual := v.visitedKeys - - diff := pretty.Compare(expected, actual) - if diff != "" { - t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) - } - } -} - -// test Attributes() - -func TestNameAttributes(t *testing.T) { - for _, tt := range nameNodesTests { - expected := tt.expectedAttributes - actual := tt.node.Attributes() - - diff := pretty.Compare(expected, actual) - if diff != "" { - t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) - } - } -} diff --git a/node/name/t_visitor_test.go b/node/name/t_visitor_test.go new file mode 100644 index 0000000..8a4fd1e --- /dev/null +++ b/node/name/t_visitor_test.go @@ -0,0 +1,95 @@ +package name_test + +import ( + "reflect" + "testing" + + "github.com/z7zmey/php-parser/node/name" + "github.com/z7zmey/php-parser/walker" + + "github.com/kylelemons/godebug/pretty" + "github.com/z7zmey/php-parser/node" +) + +var nameNodesTests = []struct { + node node.Node // node + expectedVisitedKeys []string // visited keys + expectedAttributes map[string]interface{} +}{ + { + &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}}, + []string{"Parts"}, + nil, + }, + { + &name.FullyQualified{Parts: []node.Node{&name.NamePart{Value: "foo"}}}, + []string{"Parts"}, + nil, + }, + { + &name.Relative{Parts: []node.Node{&name.NamePart{Value: "foo"}}}, + []string{"Parts"}, + nil, + }, + { + &name.NamePart{Value: "foo"}, + []string{}, + map[string]interface{}{"Value": "foo"}, + }, +} + +type visitorMock struct { + visitChildren bool + visitedKeys []string +} + +func (v *visitorMock) EnterNode(n walker.Walker) bool { return v.visitChildren } +func (v *visitorMock) GetChildrenVisitor(key string) walker.Visitor { + v.visitedKeys = append(v.visitedKeys, key) + return &visitorMock{v.visitChildren, nil} +} +func (v *visitorMock) LeaveNode(n walker.Walker) {} + +func TestNameVisitorDisableChildren(t *testing.T) { + for _, tt := range nameNodesTests { + v := &visitorMock{false, nil} + tt.node.Walk(v) + + expected := []string{} + actual := v.visitedKeys + + diff := pretty.Compare(expected, actual) + if diff != "" { + t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) + } + } +} + +func TestNameVisitor(t *testing.T) { + for _, tt := range nameNodesTests { + v := &visitorMock{true, nil} + tt.node.Walk(v) + + expected := tt.expectedVisitedKeys + actual := v.visitedKeys + + diff := pretty.Compare(expected, actual) + if diff != "" { + t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) + } + } +} + +// test Attributes() + +func TestNameAttributes(t *testing.T) { + for _, tt := range nameNodesTests { + expected := tt.expectedAttributes + actual := tt.node.Attributes() + + diff := pretty.Compare(expected, actual) + if diff != "" { + t.Errorf("%s diff: (-expected +actual)\n%s", reflect.TypeOf(tt.node), diff) + } + } +} diff --git a/node/scalar/t_encapsed_test.go b/node/scalar/t_encapsed_test.go index 19ceac7..ab00e7d 100644 --- a/node/scalar/t_encapsed_test.go +++ b/node/scalar/t_encapsed_test.go @@ -6,10 +6,10 @@ import ( "github.com/z7zmey/php-parser/node/expr" - "github.com/kylelemons/godebug/pretty" "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/stmt" + "github.com/z7zmey/php-parser/php5" "github.com/z7zmey/php-parser/php7" ) @@ -30,10 +30,10 @@ func TestSimpleVar(t *testing.T) { } actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) - if diff := pretty.Compare(expected, actual); diff != "" { - t.Errorf("diff: (-expected +actual)\n%s", diff) - } + actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) } func TestSimpleVarPropertyFetch(t *testing.T) { @@ -57,10 +57,10 @@ func TestSimpleVarPropertyFetch(t *testing.T) { } actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) - if diff := pretty.Compare(expected, actual); diff != "" { - t.Errorf("diff: (-expected +actual)\n%s", diff) - } + actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) } func TestDollarOpenCurlyBraces(t *testing.T) { @@ -80,10 +80,10 @@ func TestDollarOpenCurlyBraces(t *testing.T) { } actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) - if diff := pretty.Compare(expected, actual); diff != "" { - t.Errorf("diff: (-expected +actual)\n%s", diff) - } + actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) } func TestDollarOpenCurlyBracesDimNumber(t *testing.T) { @@ -106,10 +106,10 @@ func TestDollarOpenCurlyBracesDimNumber(t *testing.T) { } actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) - if diff := pretty.Compare(expected, actual); diff != "" { - t.Errorf("diff: (-expected +actual)\n%s", diff) - } + actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) } func TestCurlyOpenMethodCall(t *testing.T) { @@ -124,6 +124,7 @@ func TestCurlyOpenMethodCall(t *testing.T) { &expr.MethodCall{ Variable: &expr.Variable{VarName: &node.Identifier{Value: "$foo"}}, Method: &node.Identifier{Value: "bar"}, + Arguments: []node.Node{}, }, }, }, @@ -132,8 +133,8 @@ func TestCurlyOpenMethodCall(t *testing.T) { } actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) - if diff := pretty.Compare(expected, actual); diff != "" { - t.Errorf("diff: (-expected +actual)\n%s", diff) - } + actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) } diff --git a/node/scalar/t_magic_constant_test.go b/node/scalar/t_magic_constant_test.go index e6b9b82..45707c4 100644 --- a/node/scalar/t_magic_constant_test.go +++ b/node/scalar/t_magic_constant_test.go @@ -4,14 +4,15 @@ import ( "bytes" "testing" - "github.com/kylelemons/godebug/pretty" "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/stmt" + "github.com/z7zmey/php-parser/php5" "github.com/z7zmey/php-parser/php7" ) func TestMagicConstant(t *testing.T) { + // TODO: test all magic constants src := `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{ + &node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}}, + &node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$b"}}}, + }, + }, + }, + &stmt.Expression{ + Expr: &expr.FunctionCall{ + Function: &expr.Variable{VarName: &node.Identifier{Value: "$foo"}}, + Arguments: []node.Node{ + &node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}}, + &node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$b"}}}, + }, + }, + }, + &stmt.Expression{ + Expr: &expr.MethodCall{ + Variable: &expr.Variable{VarName: &node.Identifier{Value: "$foo"}}, + Method: &node.Identifier{Value: "bar"}, + Arguments: []node.Node{ + &node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}}, + &node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$b"}}}, + }, + }, + }, + &stmt.Expression{ + Expr: &expr.StaticCall{ + Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}}, + Call: &node.Identifier{Value: "bar"}, + Arguments: []node.Node{ + &node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}}, + &node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$b"}}}, + }, + }, + }, + &stmt.Expression{ + Expr: &expr.StaticCall{ + Class: &expr.Variable{VarName: &node.Identifier{Value: "$foo"}}, + Call: &node.Identifier{Value: "bar"}, + Arguments: []node.Node{ + &node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}}, + &node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$b"}}}, + }, + }, + }, + &stmt.Expression{ + Expr: &expr.New{ + Class: &name.Name{Parts: []node.Node{&name.NamePart{Value: "foo"}}}, + Arguments: []node.Node{ + &node.Argument{Variadic: false, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}}}, + &node.Argument{Variadic: true, Expr: &expr.Variable{VarName: &node.Identifier{Value: "$b"}}}, + }, + }, + }, + }, + } + + actual, _, _ := php5.Parse(bytes.NewBufferString(src), "test.php") + assertEqual(t, expected, actual) +} + +func TestPhp7ParameterNode(t *testing.T) { src := `