2018-02-08 18:06:01 +00:00
|
|
|
package stmt_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-02-19 11:00:58 +00:00
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
2018-02-08 18:06:01 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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() {} }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
PhpDocComment: "",
|
2018-02-19 11:00:58 +00:00
|
|
|
MethodName: &node.Identifier{Value: "bar"},
|
|
|
|
Stmts: []node.Node{},
|
2018-02-08 18:06:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-08 18:06:01 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-08 18:06:01 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2018-02-13 10:16:53 +00:00
|
|
|
func TestPrivateProtectedClassMethod(t *testing.T) {
|
|
|
|
src := `<? class foo{ final private function bar() {} protected function baz() {} }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
PhpDocComment: "",
|
|
|
|
ReturnsRef: false,
|
|
|
|
MethodName: &node.Identifier{Value: "bar"},
|
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "final"},
|
|
|
|
&node.Identifier{Value: "private"},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
PhpDocComment: "",
|
|
|
|
ReturnsRef: false,
|
|
|
|
MethodName: &node.Identifier{Value: "baz"},
|
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "protected"},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-13 10:16:53 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-13 10:16:53 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2018-02-08 18:06:01 +00:00
|
|
|
func TestPhp5ClassMethod(t *testing.T) {
|
|
|
|
src := `<? class foo{ public static function &bar() {} }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
PhpDocComment: "",
|
2018-02-19 11:00:58 +00:00
|
|
|
ReturnsRef: true,
|
|
|
|
MethodName: &node.Identifier{Value: "bar"},
|
2018-02-08 18:06:01 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
&node.Identifier{Value: "static"},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php5.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-08 18:06:01 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPhp7ClassMethod(t *testing.T) {
|
|
|
|
src := `<? class foo{ public static function &bar(): void {} }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
PhpDocComment: "",
|
2018-02-19 11:00:58 +00:00
|
|
|
ReturnsRef: true,
|
|
|
|
MethodName: &node.Identifier{Value: "bar"},
|
2018-02-08 18:06:01 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
&node.Identifier{Value: "static"},
|
|
|
|
},
|
|
|
|
ReturnType: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "void"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-13 10:16:53 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAbstractClassMethod(t *testing.T) {
|
|
|
|
src := `<? abstract class Foo{ abstract public function bar(); }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
|
|
|
ClassName: &node.Identifier{Value: "Foo"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
PhpDocComment: "",
|
2018-02-19 11:00:58 +00:00
|
|
|
ReturnsRef: false,
|
|
|
|
MethodName: &node.Identifier{Value: "bar"},
|
2018-02-13 10:16:53 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "abstract"},
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-13 10:16:53 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-13 10:16:53 +00:00
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPhp7AbstractClassMethod(t *testing.T) {
|
|
|
|
src := `<? abstract class Foo{ public function bar(): void; }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
Modifiers: []node.Node{&node.Identifier{Value: "abstract"}},
|
|
|
|
ClassName: &node.Identifier{Value: "Foo"},
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.ClassMethod{
|
|
|
|
PhpDocComment: "",
|
2018-02-19 11:00:58 +00:00
|
|
|
ReturnsRef: false,
|
|
|
|
MethodName: &node.Identifier{Value: "bar"},
|
2018-02-13 10:16:53 +00:00
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "public"},
|
|
|
|
},
|
|
|
|
ReturnType: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "void"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:26:44 +00:00
|
|
|
actual, _, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
|
2018-02-08 18:06:01 +00:00
|
|
|
assertEqual(t, expected, actual)
|
2018-02-19 11:00:58 +00:00
|
|
|
}
|