2018-02-08 18:31:56 +00:00
|
|
|
package stmt_test
|
|
|
|
|
|
|
|
import (
|
2018-02-19 11:00:58 +00:00
|
|
|
"bytes"
|
2018-02-08 18:31:56 +00:00
|
|
|
"github.com/z7zmey/php-parser/node/expr"
|
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
|
|
|
"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 TestSimpleClass(t *testing.T) {
|
|
|
|
src := `<? class foo{ }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
2018-02-19 11:00:58 +00:00
|
|
|
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{ }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "abstract"},
|
|
|
|
},
|
|
|
|
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 { }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "final"},
|
|
|
|
},
|
|
|
|
Extends: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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 { }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "final"},
|
|
|
|
},
|
|
|
|
Implements: []node.Node{
|
|
|
|
&name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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 { }`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Class{
|
|
|
|
ClassName: &node.Identifier{Value: "foo"},
|
|
|
|
Modifiers: []node.Node{
|
|
|
|
&node.Identifier{Value: "final"},
|
|
|
|
},
|
|
|
|
Implements: []node.Node{
|
|
|
|
&name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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 { };`
|
|
|
|
|
|
|
|
expected := &stmt.StmtList{
|
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Expression{
|
|
|
|
Expr: &expr.New{
|
|
|
|
Class: &stmt.Class{
|
|
|
|
Args: []node.Node{},
|
|
|
|
Extends: &name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Implements: []node.Node{
|
|
|
|
&name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&name.Name{
|
|
|
|
Parts: []node.Node{
|
|
|
|
&name.NamePart{Value: "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|