php-parser/node/stmt/t_class_method_test.go

627 lines
13 KiB
Go
Raw Normal View History

2018-02-08 18:06:01 +00:00
package stmt_test
import (
"testing"
"gotest.tools/assert"
2018-06-03 06:35:44 +00:00
"github.com/z7zmey/php-parser/node/name"
2018-06-24 07:19:44 +00:00
"github.com/z7zmey/php-parser/position"
2018-06-03 06:35:44 +00:00
2018-02-08 18:06:01 +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 TestSimpleClassMethod(t *testing.T) {
src := `<? class foo{ function 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,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 33,
},
2018-02-08 18:06:01 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 33,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 9,
2018-06-24 07:19:44 +00:00
EndPos: 12,
},
Value: "foo",
},
2018-02-08 18:06:01 +00:00
Stmts: []node.Node{
&stmt.ClassMethod{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 31,
},
ReturnsRef: false,
2018-02-08 18:06:01 +00:00
PhpDocComment: "",
2018-06-24 07:19:44 +00:00
MethodName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 23,
2018-06-24 07:19:44 +00:00
EndPos: 26,
},
Value: "bar",
},
2018-06-03 06:35:44 +00:00
Stmt: &stmt.StmtList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 29,
2018-06-24 07:19:44 +00:00
EndPos: 31,
},
2018-06-03 06:35:44 +00:00
Stmts: []node.Node{},
},
2018-02-08 18:06:01 +00:00
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php7parser := php7.NewParser([]byte(src), "7.4")
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 18:06:01 +00:00
2019-12-26 15:57:56 +00:00
php5parser := php5.NewParser([]byte(src), "5.6")
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 18:06:01 +00:00
}
2018-02-13 10:16:53 +00:00
func TestPrivateProtectedClassMethod(t *testing.T) {
src := `<? class foo{ final private function bar() {} protected function 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,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 75,
},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 75,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 9,
2018-06-24 07:19:44 +00:00
EndPos: 12,
},
Value: "foo",
},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.ClassMethod{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 45,
},
2018-02-13 10:16:53 +00:00
ReturnsRef: false,
2018-06-24 07:19:44 +00:00
PhpDocComment: "",
MethodName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 37,
2018-06-24 07:19:44 +00:00
EndPos: 40,
},
Value: "bar",
},
2018-02-13 10:16:53 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 19,
},
Value: "final",
},
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 27,
},
Value: "private",
},
2018-02-13 10:16:53 +00:00
},
2018-06-03 06:35:44 +00:00
Stmt: &stmt.StmtList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 43,
2018-06-24 07:19:44 +00:00
EndPos: 45,
},
2018-06-03 06:35:44 +00:00
Stmts: []node.Node{},
},
2018-02-13 10:16:53 +00:00
},
&stmt.ClassMethod{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 46,
2018-06-24 07:19:44 +00:00
EndPos: 73,
},
2018-02-13 10:16:53 +00:00
ReturnsRef: false,
2018-06-24 07:19:44 +00:00
PhpDocComment: "",
MethodName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 65,
2018-06-24 07:19:44 +00:00
EndPos: 68,
},
Value: "baz",
},
2018-02-13 10:16:53 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 46,
2018-06-24 07:19:44 +00:00
EndPos: 55,
},
Value: "protected",
},
2018-02-13 10:16:53 +00:00
},
2018-06-03 06:35:44 +00:00
Stmt: &stmt.StmtList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 71,
2018-06-24 07:19:44 +00:00
EndPos: 73,
},
2018-06-03 06:35:44 +00:00
Stmts: []node.Node{},
},
2018-02-13 10:16:53 +00:00
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php7parser := php7.NewParser([]byte(src), "7.4")
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:16:53 +00:00
2019-12-26 15:57:56 +00:00
php5parser := php5.NewParser([]byte(src), "5.6")
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:16:53 +00:00
}
2018-02-08 18:06:01 +00:00
func TestPhp5ClassMethod(t *testing.T) {
src := `<? class foo{ public static function &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,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 48,
},
2018-02-08 18:06:01 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 48,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 9,
2018-06-24 07:19:44 +00:00
EndPos: 12,
},
Value: "foo",
},
2018-02-08 18:06:01 +00:00
Stmts: []node.Node{
&stmt.ClassMethod{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 46,
},
ReturnsRef: true,
2018-06-24 07:19:44 +00:00
PhpDocComment: "",
MethodName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 38,
2018-06-24 07:19:44 +00:00
EndPos: 41,
},
Value: "bar",
},
2018-02-08 18:06:01 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 20,
},
Value: "public",
},
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 21,
2018-06-24 07:19:44 +00:00
EndPos: 27,
},
Value: "static",
},
2018-02-08 18:06:01 +00:00
},
2018-06-03 06:35:44 +00:00
Stmt: &stmt.StmtList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 44,
2018-06-24 07:19:44 +00:00
EndPos: 46,
},
2018-06-03 06:35:44 +00:00
Stmts: []node.Node{},
},
2018-02-08 18:06:01 +00:00
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php5parser := php5.NewParser([]byte(src), "5.6")
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual := php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 18:06:01 +00:00
}
func TestPhp7ClassMethod(t *testing.T) {
src := `<? class foo{ public static function &bar(): void {} }`
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,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 54,
},
2018-02-08 18:06:01 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 54,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 9,
2018-06-24 07:19:44 +00:00
EndPos: 12,
},
Value: "foo",
},
2018-02-08 18:06:01 +00:00
Stmts: []node.Node{
&stmt.ClassMethod{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 52,
},
ReturnsRef: true,
2018-06-24 07:19:44 +00:00
PhpDocComment: "",
MethodName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 38,
2018-06-24 07:19:44 +00:00
EndPos: 41,
},
Value: "bar",
},
2018-02-08 18:06:01 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 20,
},
Value: "public",
},
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 21,
2018-06-24 07:19:44 +00:00
EndPos: 27,
},
Value: "static",
},
2018-02-08 18:06:01 +00:00
},
ReturnType: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 45,
2018-06-24 07:19:44 +00:00
EndPos: 49,
},
2018-02-08 18:06:01 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 45,
2018-06-24 07:19:44 +00:00
EndPos: 49,
},
Value: "void",
},
2018-02-08 18:06:01 +00:00
},
},
2018-06-03 06:35:44 +00:00
Stmt: &stmt.StmtList{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 50,
2018-06-24 07:19:44 +00:00
EndPos: 52,
},
2018-06-03 06:35:44 +00:00
Stmts: []node.Node{},
},
2018-02-08 18:06:01 +00:00
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php7parser := php7.NewParser([]byte(src), "7.4")
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:16:53 +00:00
}
func TestAbstractClassMethod(t *testing.T) {
src := `<? abstract class Foo{ abstract public function 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,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 56,
},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 56,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 18,
2018-06-24 07:19:44 +00:00
EndPos: 21,
},
Value: "Foo",
},
Modifiers: []node.Node{
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 11,
},
Value: "abstract",
},
},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.ClassMethod{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 23,
2018-06-24 07:19:44 +00:00
EndPos: 54,
},
ReturnsRef: false,
2018-06-24 07:19:44 +00:00
PhpDocComment: "",
MethodName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 48,
2018-06-24 07:19:44 +00:00
EndPos: 51,
},
Value: "bar",
},
2018-02-13 10:16:53 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 23,
2018-06-24 07:19:44 +00:00
EndPos: 31,
},
Value: "abstract",
},
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 32,
2018-06-24 07:19:44 +00:00
EndPos: 38,
},
Value: "public",
},
},
Stmt: &stmt.Nop{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 53,
2018-06-24 07:19:44 +00:00
EndPos: 54,
},
2018-02-13 10:16:53 +00:00
},
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php7parser := php7.NewParser([]byte(src), "7.4")
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:16:53 +00:00
2019-12-26 15:57:56 +00:00
php5parser := php5.NewParser([]byte(src), "5.6")
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-13 10:16:53 +00:00
}
func TestPhp7AbstractClassMethod(t *testing.T) {
src := `<? abstract class Foo{ public function bar(): void; }`
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,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 53,
},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.Class{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 53,
},
PhpDocComment: "",
ClassName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 18,
2018-06-24 07:19:44 +00:00
EndPos: 21,
},
Value: "Foo",
},
Modifiers: []node.Node{
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 11,
},
Value: "abstract",
},
},
2018-02-13 10:16:53 +00:00
Stmts: []node.Node{
&stmt.ClassMethod{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 23,
2018-06-24 07:19:44 +00:00
EndPos: 51,
},
ReturnsRef: false,
2018-06-24 07:19:44 +00:00
PhpDocComment: "",
MethodName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 39,
2018-06-24 07:19:44 +00:00
EndPos: 42,
},
Value: "bar",
},
2018-02-13 10:16:53 +00:00
Modifiers: []node.Node{
2018-06-24 07:19:44 +00:00
&node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 23,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
Value: "public",
},
2018-02-13 10:16:53 +00:00
},
ReturnType: &name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 46,
2018-06-24 07:19:44 +00:00
EndPos: 50,
},
2018-02-13 10:16:53 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 46,
2018-06-24 07:19:44 +00:00
EndPos: 50,
},
Value: "void",
},
},
},
Stmt: &stmt.Nop{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 50,
2018-06-24 07:19:44 +00:00
EndPos: 51,
2018-02-13 10:16:53 +00:00
},
},
},
},
},
},
}
2019-12-26 15:57:56 +00:00
php7parser := php7.NewParser([]byte(src), "7.4")
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}