update scalar tests

This commit is contained in:
z7zmey 2018-01-12 15:13:05 +02:00
parent b8ef8314af
commit 289f4d0767
2 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package scalar_test
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/parser"
)
func TestMagicConstant(t *testing.T) {
src := `<? __DIR__;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Expression{
Expr: &scalar.MagicConstant{Value: "__DIR__"},
},
},
}
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
if diff := pretty.Compare(expected, actual); diff != "" {
t.Errorf("diff: (-expected +actual)\n%s", diff)
}
}

103
node/scalar/visitor_test.go Normal file
View File

@ -0,0 +1,103 @@
package scalar_test
import (
"reflect"
"testing"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/scalar"
)
var nameNodesTests = []struct {
node node.Node // node
expectedVisitedKeys []string // visited keys
expectedAttributes map[string]interface{}
}{
{
&scalar.String{Value: "foo"},
[]string{},
map[string]interface{}{"Value": "foo"},
},
{
&scalar.Lnumber{Value: "0"},
[]string{},
map[string]interface{}{"Value": "0"},
},
{
&scalar.Dnumber{Value: "0.1"},
[]string{},
map[string]interface{}{"Value": "0.1"},
},
{
&scalar.MagicConstant{Value: "__DIR__"},
[]string{},
map[string]interface{}{"Value": "__DIR__"},
},
{
&scalar.EncapsedStringPart{Value: "foo"},
[]string{},
map[string]interface{}{"Value": "foo"},
},
{
&scalar.Encapsed{Parts: []node.Node{&scalar.EncapsedStringPart{Value: "foo"}}},
[]string{"Parts"},
nil,
},
}
type visitorMock struct {
visitChildren bool
visitedKeys []string
}
func (v *visitorMock) EnterNode(n node.Node) bool { return v.visitChildren }
func (v *visitorMock) GetChildrenVisitor(key string) node.Visitor {
v.visitedKeys = append(v.visitedKeys, key)
return &visitorMock{v.visitChildren, nil}
}
func (v *visitorMock) LeaveNode(n node.Node) {}
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)
}
}
}