ClassConstList tests

This commit is contained in:
z7zmey 2018-02-08 19:52:22 +02:00
parent 47e365658a
commit 1ef722b4bb
3 changed files with 84 additions and 2 deletions

View File

@ -0,0 +1,82 @@
package stmt_test
import (
"bytes"
"testing"
"github.com/z7zmey/php-parser/node/scalar"
"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 TestClassConstList(t *testing.T) {
src := `<? class foo{ public const FOO = 1, BAR = 2; }`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Class{
ClassName: &node.Identifier{Value: "foo"},
Stmts: []node.Node{
&stmt.ClassConstList{
Modifiers: []node.Node{
&node.Identifier{Value: "public"},
},
Consts: []node.Node{
&stmt.Constant{
PhpDocComment: "",
ConstantName: &node.Identifier{Value: "FOO"},
Expr: &scalar.Lnumber{Value: "1"},
},
&stmt.Constant{
PhpDocComment: "",
ConstantName: &node.Identifier{Value: "BAR"},
Expr: &scalar.Lnumber{Value: "2"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestClassConstListWithoutModifiers(t *testing.T) {
src := `<? class foo{ const FOO = 1, BAR = 2; }`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Class{
ClassName: &node.Identifier{Value: "foo"},
Stmts: []node.Node{
&stmt.ClassConstList{
Consts: []node.Node{
&stmt.Constant{
PhpDocComment: "",
ConstantName: &node.Identifier{Value: "FOO"},
Expr: &scalar.Lnumber{Value: "1"},
},
&stmt.Constant{
PhpDocComment: "",
ConstantName: &node.Identifier{Value: "BAR"},
Expr: &scalar.Lnumber{Value: "2"},
},
},
},
},
},
},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}

View File

@ -4079,7 +4079,7 @@ yydefault:
positions.AddPosition(constant, positionBuilder.NewTokenNodePosition(yyDollar[3].token, yyDollar[5].node))
comments.AddComments(constant, yyDollar[3].token.Comments())
yyDollar[1].node.(*stmt.ConstList).Consts = append(yyDollar[1].node.(*stmt.ConstList).Consts, constant)
yyDollar[1].node.(*stmt.ClassConstList).Consts = append(yyDollar[1].node.(*stmt.ClassConstList).Consts, constant)
positions.AddPosition(yyDollar[1].node, positionBuilder.NewNodesPosition(yyDollar[1].node, yyDollar[5].node))
yyVAL.node = yyDollar[1].node

View File

@ -1721,7 +1721,7 @@ class_constant_declaration:
positions.AddPosition(constant, positionBuilder.NewTokenNodePosition($3, $5))
comments.AddComments(constant, $3.Comments())
$1.(*stmt.ConstList).Consts = append($1.(*stmt.ConstList).Consts, constant)
$1.(*stmt.ClassConstList).Consts = append($1.(*stmt.ClassConstList).Consts, constant)
positions.AddPosition($1, positionBuilder.NewNodesPosition($1, $5))
$$ = $1