2018-01-12 07:41:08 +00:00
|
|
|
package name_test
|
2018-01-12 07:14:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/node/expr"
|
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
2018-01-17 16:58:45 +00:00
|
|
|
"github.com/z7zmey/php-parser/walker"
|
2018-01-12 07:14:11 +00:00
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/node/stmt"
|
|
|
|
"github.com/z7zmey/php-parser/parser"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 TestName(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"}},
|
|
|
|
},
|
|
|
|
Arguments: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-17 17:18:35 +00:00
|
|
|
actual, _, _ := parser.ParsePhp7(bytes.NewBufferString(src), "test.php")
|
2018-01-12 07:14:11 +00:00
|
|
|
|
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFullyQualified(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"}},
|
|
|
|
},
|
|
|
|
Arguments: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-17 17:18:35 +00:00
|
|
|
actual, _, _ := parser.ParsePhp7(bytes.NewBufferString(src), "test.php")
|
2018-01-12 07:14:11 +00:00
|
|
|
|
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRelative(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"}},
|
|
|
|
},
|
|
|
|
Arguments: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-17 17:18:35 +00:00
|
|
|
actual, _, _ := parser.ParsePhp7(bytes.NewBufferString(src), "test.php")
|
2018-01-12 07:14:11 +00:00
|
|
|
|
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
2018-01-12 09:26:00 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:58:45 +00:00
|
|
|
func (v *visitorMock) EnterNode(n walker.Walker) bool { return v.visitChildren }
|
|
|
|
func (v *visitorMock) GetChildrenVisitor(key string) walker.Visitor {
|
2018-01-12 09:26:00 +00:00
|
|
|
v.visitedKeys = append(v.visitedKeys, key)
|
|
|
|
return &visitorMock{v.visitChildren, nil}
|
|
|
|
}
|
2018-01-17 16:58:45 +00:00
|
|
|
func (v *visitorMock) LeaveNode(n walker.Walker) {}
|
2018-01-12 09:26:00 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|