php-parser/node/stmt/t_class_test.go

182 lines
4.0 KiB
Go

package stmt_test
import (
"bytes"
"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"},
Stmts: []node.Node{},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
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{},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
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{},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
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{},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
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{},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
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{},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}