add php5 tests

This commit is contained in:
z7zmey 2018-02-08 12:48:38 +02:00
parent 03f49187ce
commit 0dac524c62
11 changed files with 800 additions and 594 deletions

View File

@ -7,11 +7,11 @@ import (
"github.com/z7zmey/php-parser/node/expr" "github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/name" "github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/walker"
"github.com/kylelemons/godebug/pretty" "github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
) )
@ -45,7 +45,9 @@ func TestName(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -66,7 +68,9 @@ func TestFullyQualified(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -87,91 +91,8 @@ func TestRelative(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) 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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -6,10 +6,10 @@ import (
"github.com/z7zmey/php-parser/node/expr" "github.com/z7zmey/php-parser/node/expr"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
) )
@ -30,10 +30,10 @@ func TestSimpleVar(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestSimpleVarPropertyFetch(t *testing.T) { func TestSimpleVarPropertyFetch(t *testing.T) {
@ -57,10 +57,10 @@ func TestSimpleVarPropertyFetch(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestDollarOpenCurlyBraces(t *testing.T) { func TestDollarOpenCurlyBraces(t *testing.T) {
@ -80,10 +80,10 @@ func TestDollarOpenCurlyBraces(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestDollarOpenCurlyBracesDimNumber(t *testing.T) { func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
@ -106,10 +106,10 @@ func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestCurlyOpenMethodCall(t *testing.T) { func TestCurlyOpenMethodCall(t *testing.T) {
@ -124,6 +124,7 @@ func TestCurlyOpenMethodCall(t *testing.T) {
&expr.MethodCall{ &expr.MethodCall{
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$foo"}}, Variable: &expr.Variable{VarName: &node.Identifier{Value: "$foo"}},
Method: &node.Identifier{Value: "bar"}, 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") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }

View File

@ -4,14 +4,15 @@ import (
"bytes" "bytes"
"testing" "testing"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
) )
func TestMagicConstant(t *testing.T) { func TestMagicConstant(t *testing.T) {
// TODO: test all magic constants
src := `<? __DIR__;` src := `<? __DIR__;`
expected := &stmt.StmtList{ expected := &stmt.StmtList{
@ -23,8 +24,8 @@ func TestMagicConstant(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
) )
@ -37,7 +38,9 @@ func TestLNumber(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -53,7 +56,9 @@ func TestDNumber(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -69,7 +74,9 @@ func TestFloat(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -85,7 +92,9 @@ func TestBinaryLNumber(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -101,7 +110,9 @@ func TestBinaryDNumber(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -117,7 +128,9 @@ func TestHLNumber(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -133,6 +146,8 @@ func TestHDNumber(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }

View File

@ -4,10 +4,10 @@ import (
"bytes" "bytes"
"testing" "testing"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/scalar" "github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
) )
@ -23,10 +23,10 @@ func TestDoubleQuotedScalarString(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestDoubleQuotedScalarStringWithEscapedVar(t *testing.T) { func TestDoubleQuotedScalarStringWithEscapedVar(t *testing.T) {
src := `<? "\$test";` src := `<? "\$test";`
@ -40,10 +40,10 @@ func TestDoubleQuotedScalarStringWithEscapedVar(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestMultilineDoubleQuotedScalarString(t *testing.T) { func TestMultilineDoubleQuotedScalarString(t *testing.T) {
@ -60,10 +60,10 @@ func TestMultilineDoubleQuotedScalarString(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestSingleQuotedScalarString(t *testing.T) { func TestSingleQuotedScalarString(t *testing.T) {
@ -78,10 +78,10 @@ func TestSingleQuotedScalarString(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestMultilineSingleQuotedScalarString(t *testing.T) { func TestMultilineSingleQuotedScalarString(t *testing.T) {
@ -98,10 +98,10 @@ func TestMultilineSingleQuotedScalarString(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestPlainHeredocScalarString(t *testing.T) { func TestPlainHeredocScalarString(t *testing.T) {
@ -119,10 +119,10 @@ CAD;
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestHeredocScalarString(t *testing.T) { func TestHeredocScalarString(t *testing.T) {
@ -140,10 +140,10 @@ CAD;
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }
func TestNowdocScalarString(t *testing.T) { func TestNowdocScalarString(t *testing.T) {
@ -161,8 +161,8 @@ CAD;
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
if diff := pretty.Compare(expected, actual); diff != "" { actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
t.Errorf("diff: (-expected +actual)\n%s", diff) assertEqual(t, expected, actual)
}
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/kylelemons/godebug/pretty" "github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
) )
@ -20,7 +21,7 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if diff != "" { if diff != "" {
t.Errorf("diff: (-expected +actual)\n%s", diff) t.Errorf("diff: (-expected +actual)\n%s", diff)
} else { } else {
t.Errorf("expected and actual are not equal\n") t.Errorf("expected and actual are not equal\nexpectd: %+v\nactual: %+v\n", expected, actual)
} }
} }
@ -42,7 +43,10 @@ func TestAltIf(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -69,7 +73,10 @@ func TestAltElseIf(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -93,7 +100,10 @@ func TestAltElse(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -129,6 +139,9 @@ func TestAltElseElseIf(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }

View File

@ -8,6 +8,7 @@ import (
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
) )
@ -28,7 +29,10 @@ func TestBreakEmpty(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -51,7 +55,10 @@ func TestBreakLight(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
@ -74,6 +81,9 @@ func TestBreak(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }

View File

@ -12,6 +12,7 @@ import (
"github.com/kylelemons/godebug/pretty" "github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
) )
@ -42,11 +43,13 @@ func TestIdentifier(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
func TestArgumentNode(t *testing.T) { func TestPhp7ArgumentNode(t *testing.T) {
src := `<? src := `<?
foo($a, ...$b); foo($a, ...$b);
$foo($a, ...$b); $foo($a, ...$b);
@ -133,10 +136,86 @@ func TestArgumentNode(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
func TestParameterNode(t *testing.T) {
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{
&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 := `<? src := `<?
function foo(?bar $bar=null, baz &...$baz) {} function foo(?bar $bar=null, baz &...$baz) {}
class foo {public function foo(?bar $bar=null, baz &...$baz) {}} class foo {public function foo(?bar $bar=null, baz &...$baz) {}}
@ -199,6 +278,71 @@ func TestParameterNode(t *testing.T) {
} }
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
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"}}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$bar"}},
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"}}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$baz"}},
},
}
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{},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }

File diff suppressed because it is too large Load Diff

View File

@ -597,7 +597,7 @@ unticked_statement:
stmts := stmt.NewStmtList($4) stmts := stmt.NewStmtList($4)
positions.AddPosition(stmts, positionBuilder.NewNodeListPosition($4)) positions.AddPosition(stmts, positionBuilder.NewNodeListPosition($4))
$$ = stmt.NewIf($2, stmts, $5, $6) $$ = stmt.NewAltIf($2, stmts, $5, $6)
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $8)) positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $8))
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }
@ -694,7 +694,11 @@ unticked_statement:
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }
| expr ';' | expr ';'
{ $$ = $1 } {
$$ = stmt.NewExpression($1)
positions.AddPosition($$, positionBuilder.NewNodeTokenPosition($1, $2))
comments.AddComments($$, comments[$1])
}
| T_UNSET '(' unset_variables ')' ';' | T_UNSET '(' unset_variables ')' ';'
{ {
$$ = stmt.NewUnset($3) $$ = stmt.NewUnset($3)
@ -1128,7 +1132,7 @@ elseif_list:
new_elseif_list: new_elseif_list:
/* empty */ /* empty */
{ $$ = []node.Node{} } { $$ = nil }
| new_elseif_list T_ELSEIF parenthesis_expr ':' inner_statement_list | new_elseif_list T_ELSEIF parenthesis_expr ':' inner_statement_list
{ {
stmts := stmt.NewStmtList($5) stmts := stmt.NewStmtList($5)
@ -3571,7 +3575,7 @@ encaps_var:
} }
| T_DOLLAR_OPEN_CURLY_BRACES expr '}' | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
{ {
$$ = expr.NewVariable($2) $$ = $2
positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $3)) positions.AddPosition($$, positionBuilder.NewTokensPosition($1, $3))
comments.AddComments($$, $1.Comments()) comments.AddComments($$, $1.Comments())
} }