php-parser/node/stmt/t_class_test.go

513 lines
11 KiB
Go
Raw Normal View History

2018-02-08 18:31:56 +00:00
package stmt_test
import (
"bytes"
2018-04-29 16:58:49 +00:00
"testing"
2018-02-08 18:31:56 +00:00
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/name"
2018-06-24 07:19:44 +00:00
"github.com/z7zmey/php-parser/position"
2018-02-08 18:31:56 +00:00
"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"
)
func TestSimpleClass(t *testing.T) {
src := `<? class foo{ }`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
2018-02-08 18:31:56 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 15,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 10,
EndPos: 12,
},
Value: "foo",
},
Stmts: []node.Node{},
2018-02-08 18:31:56 +00:00
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
}
func TestAbstractClass(t *testing.T) {
src := `<? abstract class foo{ }`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 24,
},
2018-02-08 18:31:56 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 24,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 19,
EndPos: 21,
},
Value: "foo",
},
2018-02-08 18:31:56 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 11,
},
Value: "abstract",
},
2018-02-08 18:31:56 +00:00
},
Stmts: []node.Node{},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
}
func TestClassExtends(t *testing.T) {
src := `<? final class foo extends bar { }`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 34,
},
2018-02-08 18:31:56 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 34,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
Value: "foo",
},
2018-02-08 18:31:56 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 8,
},
Value: "final",
},
2018-02-08 18:31:56 +00:00
},
Extends: &stmt.ClassExtends{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 30,
},
ClassName: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 28,
EndPos: 30,
},
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 28,
EndPos: 30,
},
Value: "bar",
},
},
2018-02-08 18:31:56 +00:00
},
},
Stmts: []node.Node{},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
}
func TestClassImplement(t *testing.T) {
src := `<? final class foo implements bar { }`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 37,
},
2018-02-08 18:31:56 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 37,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
Value: "foo",
},
2018-02-08 18:31:56 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 8,
},
Value: "final",
},
2018-02-08 18:31:56 +00:00
},
Implements: &stmt.ClassImplements{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 33,
},
InterfaceNames: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 31,
EndPos: 33,
},
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 31,
EndPos: 33,
},
Value: "bar",
},
},
2018-02-08 18:31:56 +00:00
},
},
},
Stmts: []node.Node{},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
}
func TestClassImplements(t *testing.T) {
src := `<? final class foo implements bar, baz { }`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 42,
},
2018-02-08 18:31:56 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 42,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 18,
},
Value: "foo",
},
2018-02-08 18:31:56 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 8,
},
Value: "final",
},
2018-02-08 18:31:56 +00:00
},
Implements: &stmt.ClassImplements{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 38,
},
InterfaceNames: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 31,
EndPos: 33,
},
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 31,
EndPos: 33,
},
Value: "bar",
},
},
2018-02-08 18:31:56 +00:00
},
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 36,
EndPos: 38,
},
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 36,
EndPos: 38,
},
Value: "baz",
},
},
2018-02-08 18:31:56 +00:00
},
},
},
Stmts: []node.Node{},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
2018-04-10 12:23:13 +00:00
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual = php5parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
}
func TestAnonimousClass(t *testing.T) {
src := `<? new class() extends foo implements bar, baz { };`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 51,
},
2018-02-08 18:31:56 +00:00
Stmts: []node.Node{
&stmt.Expression{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 51,
},
2018-02-08 18:31:56 +00:00
Expr: &expr.New{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 4,
EndPos: 50,
},
2018-02-08 18:31:56 +00:00
Class: &stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 8,
EndPos: 50,
},
PhpDocComment: "",
ArgumentList: &node.ArgumentList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 13,
EndPos: 14,
},
},
Extends: &stmt.ClassExtends{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 16,
EndPos: 26,
},
ClassName: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 26,
},
2018-02-08 18:31:56 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 26,
},
Value: "foo",
},
2018-02-08 18:31:56 +00:00
},
},
},
Implements: &stmt.ClassImplements{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 28,
EndPos: 46,
},
InterfaceNames: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 39,
EndPos: 41,
},
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 39,
EndPos: 41,
},
Value: "bar",
},
},
},
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 44,
EndPos: 46,
},
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 44,
EndPos: 46,
},
Value: "baz",
},
},
2018-02-08 18:31:56 +00:00
},
},
},
Stmts: []node.Node{},
},
},
},
},
}
2018-04-10 12:23:13 +00:00
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetRootNode()
2018-02-08 18:31:56 +00:00
assertEqual(t, expected, actual)
}