Class tests

This commit is contained in:
z7zmey 2018-02-08 20:31:56 +02:00
parent 14493d67e9
commit a209b603d8

181
node/stmt/t_class_test.go Normal file
View File

@ -0,0 +1,181 @@
package stmt_test
import (
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/name"
"bytes"
"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)
}