php-parser/node/stmt/t_class_test.go

205 lines
4.6 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"
"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-02-08 18:31:56 +00:00
Stmts: []node.Node{
&stmt.Class{
ClassName: &node.Identifier{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-02-08 18:31:56 +00:00
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 { }`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-08 18:31:56 +00:00
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 { }`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-08 18:31:56 +00:00
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 { }`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-08 18:31:56 +00:00
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 { };`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-02-08 18:31:56 +00:00
Stmts: []node.Node{
&stmt.Expression{
Expr: &expr.New{
Class: &stmt.Class{
2018-04-29 16:58:49 +00:00
ArgumentList: &node.ArgumentList{},
2018-02-08 18:31:56 +00:00
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)
}