Foreach tests

This commit is contained in:
z7zmey 2018-02-09 00:02:12 +02:00
parent 92b600775f
commit 8f40777c96
3 changed files with 175 additions and 44 deletions

125
node/stmt/t_foreach_test.go Normal file
View File

@ -0,0 +1,125 @@
package stmt_test
import (
"bytes"
"testing"
"github.com/z7zmey/php-parser/node/expr"
"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 TestForeach(t *testing.T) {
src := `<? foreach ($a as $v) {}`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Foreach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{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 TestAltForeach(t *testing.T) {
src := `<? foreach ($a as $v) : endforeach;`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Foreach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{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 TestForeachWithKey(t *testing.T) {
src := `<? foreach ($a as $k => $v) {}`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Foreach{
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "$k"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{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 TestForeachWithRef(t *testing.T) {
src := `<? foreach ($a as $k => &$v) {}`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Foreach{
ByRef: true,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "$k"}},
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
Stmt: &stmt.StmtList{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 TestForeachWithList(t *testing.T) {
src := `<? foreach ($a as $k => list($v)) {}`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.Foreach{
ByRef: false,
Expr: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Key: &expr.Variable{VarName: &node.Identifier{Value: "$k"}},
Variable: &expr.List{
Items: []node.Node{
&expr.ArrayItem{
ByRef: false,
Val: &expr.Variable{VarName: &node.Identifier{Value: "$v"}},
},
},
},
Stmt: &stmt.StmtList{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)
}

View File

@ -345,7 +345,7 @@ const yyEofCode = 1
const yyErrCode = 2
const yyInitialStackSize = 16
//line php5/php5.y:3744
//line php5/php5.y:3748
//line yacctab:1
var yyExca = [...]int{
@ -6266,11 +6266,13 @@ yydefault:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3428
{
yyVAL.node = yyDollar[1].node
yyVAL.node = expr.NewArrayItem(nil, yyDollar[1].node, false)
positions.AddPosition(yyVAL.node, positionBuilder.NewNodePosition(yyDollar[1].node))
comments.AddComments(yyVAL.node, comments[yyDollar[1].node])
}
case 477:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3430
//line php5/php5.y:3434
{
yyVAL.node = expr.NewList(yyDollar[3].list)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6278,25 +6280,25 @@ yydefault:
}
case 478:
yyDollar = yyS[yypt-0 : yypt+1]
//line php5/php5.y:3436
//line php5/php5.y:3440
{
yyVAL.node = nil
}
case 479:
yyDollar = yyS[yypt-0 : yypt+1]
//line php5/php5.y:3442
//line php5/php5.y:3446
{
yyVAL.list = nil
}
case 480:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3444
//line php5/php5.y:3448
{
yyVAL.list = yyDollar[1].list
}
case 481:
yyDollar = yyS[yypt-5 : yypt+1]
//line php5/php5.y:3449
//line php5/php5.y:3453
{
arrayItem := expr.NewArrayItem(yyDollar[3].node, yyDollar[5].node, false)
positions.AddPosition(arrayItem, positionBuilder.NewNodesPosition(yyDollar[3].node, yyDollar[5].node))
@ -6306,7 +6308,7 @@ yydefault:
}
case 482:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3457
//line php5/php5.y:3461
{
arrayItem := expr.NewArrayItem(nil, yyDollar[3].node, false)
positions.AddPosition(arrayItem, positionBuilder.NewNodePosition(yyDollar[3].node))
@ -6316,7 +6318,7 @@ yydefault:
}
case 483:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3465
//line php5/php5.y:3469
{
arrayItem := expr.NewArrayItem(yyDollar[1].node, yyDollar[3].node, false)
positions.AddPosition(arrayItem, positionBuilder.NewNodesPosition(yyDollar[1].node, yyDollar[3].node))
@ -6326,7 +6328,7 @@ yydefault:
}
case 484:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3473
//line php5/php5.y:3477
{
arrayItem := expr.NewArrayItem(nil, yyDollar[1].node, false)
positions.AddPosition(arrayItem, positionBuilder.NewNodePosition(yyDollar[1].node))
@ -6336,7 +6338,7 @@ yydefault:
}
case 485:
yyDollar = yyS[yypt-6 : yypt+1]
//line php5/php5.y:3481
//line php5/php5.y:3485
{
arrayItem := expr.NewArrayItem(yyDollar[3].node, yyDollar[6].node, true)
positions.AddPosition(arrayItem, positionBuilder.NewNodesPosition(yyDollar[3].node, yyDollar[6].node))
@ -6346,7 +6348,7 @@ yydefault:
}
case 486:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3489
//line php5/php5.y:3493
{
arrayItem := expr.NewArrayItem(nil, yyDollar[4].node, true)
positions.AddPosition(arrayItem, positionBuilder.NewTokenNodePosition(yyDollar[3].token, yyDollar[4].node))
@ -6356,7 +6358,7 @@ yydefault:
}
case 487:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3497
//line php5/php5.y:3501
{
arrayItem := expr.NewArrayItem(yyDollar[1].node, yyDollar[4].node, true)
positions.AddPosition(arrayItem, positionBuilder.NewNodesPosition(yyDollar[1].node, yyDollar[4].node))
@ -6366,7 +6368,7 @@ yydefault:
}
case 488:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3505
//line php5/php5.y:3509
{
arrayItem := expr.NewArrayItem(nil, yyDollar[2].node, true)
positions.AddPosition(arrayItem, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6376,13 +6378,13 @@ yydefault:
}
case 489:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3516
//line php5/php5.y:3520
{
yyVAL.list = append(yyDollar[1].list, yyDollar[2].node)
}
case 490:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3518
//line php5/php5.y:3522
{
encapsed := scalar.NewEncapsedStringPart(yyDollar[2].token.Value)
positions.AddPosition(encapsed, positionBuilder.NewTokenPosition(yyDollar[2].token))
@ -6391,13 +6393,13 @@ yydefault:
}
case 491:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3525
//line php5/php5.y:3529
{
yyVAL.list = []node.Node{yyDollar[1].node}
}
case 492:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3527
//line php5/php5.y:3531
{
encapsed := scalar.NewEncapsedStringPart(yyDollar[1].token.Value)
positions.AddPosition(encapsed, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6406,7 +6408,7 @@ yydefault:
}
case 493:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3537
//line php5/php5.y:3541
{
name := node.NewIdentifier(yyDollar[1].token.Value)
positions.AddPosition(name, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6418,7 +6420,7 @@ yydefault:
}
case 494:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3547
//line php5/php5.y:3551
{
identifier := node.NewIdentifier(yyDollar[1].token.Value)
positions.AddPosition(identifier, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6433,7 +6435,7 @@ yydefault:
}
case 495:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3560
//line php5/php5.y:3564
{
identifier := node.NewIdentifier(yyDollar[1].token.Value)
positions.AddPosition(identifier, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6451,7 +6453,7 @@ yydefault:
}
case 496:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3576
//line php5/php5.y:3580
{
yyVAL.node = yyDollar[2].node
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[3].token))
@ -6459,7 +6461,7 @@ yydefault:
}
case 497:
yyDollar = yyS[yypt-6 : yypt+1]
//line php5/php5.y:3582
//line php5/php5.y:3586
{
identifier := node.NewIdentifier(yyDollar[2].token.Value)
positions.AddPosition(identifier, positionBuilder.NewTokenPosition(yyDollar[2].token))
@ -6474,13 +6476,13 @@ yydefault:
}
case 498:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3596
//line php5/php5.y:3600
{
yyVAL.node = yyDollar[2].node
}
case 499:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3601
//line php5/php5.y:3605
{
yyVAL.node = scalar.NewString(yyDollar[1].token.Value)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6488,7 +6490,7 @@ yydefault:
}
case 500:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3607
//line php5/php5.y:3611
{
// TODO: add option to handle 64 bit integer
if _, err := strconv.Atoi(yyDollar[1].token.Value); err == nil {
@ -6502,7 +6504,7 @@ yydefault:
}
case 501:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3619
//line php5/php5.y:3623
{
identifier := node.NewIdentifier(yyDollar[1].token.Value)
positions.AddPosition(identifier, positionBuilder.NewTokenPosition(yyDollar[1].token))
@ -6514,7 +6516,7 @@ yydefault:
}
case 502:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3632
//line php5/php5.y:3636
{
yyVAL.node = expr.NewIsset(yyDollar[3].list)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6522,7 +6524,7 @@ yydefault:
}
case 503:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3638
//line php5/php5.y:3642
{
yyVAL.node = expr.NewEmpty(yyDollar[3].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6530,7 +6532,7 @@ yydefault:
}
case 504:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3644
//line php5/php5.y:3648
{
yyVAL.node = expr.NewEmpty(yyDollar[3].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6538,7 +6540,7 @@ yydefault:
}
case 505:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3650
//line php5/php5.y:3654
{
yyVAL.node = expr.NewInclude(yyDollar[2].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6546,7 +6548,7 @@ yydefault:
}
case 506:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3656
//line php5/php5.y:3660
{
yyVAL.node = expr.NewIncludeOnce(yyDollar[2].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6554,7 +6556,7 @@ yydefault:
}
case 507:
yyDollar = yyS[yypt-4 : yypt+1]
//line php5/php5.y:3662
//line php5/php5.y:3666
{
yyVAL.node = expr.NewEval(yyDollar[3].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokensPosition(yyDollar[1].token, yyDollar[4].token))
@ -6562,7 +6564,7 @@ yydefault:
}
case 508:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3668
//line php5/php5.y:3672
{
yyVAL.node = expr.NewRequire(yyDollar[2].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6570,7 +6572,7 @@ yydefault:
}
case 509:
yyDollar = yyS[yypt-2 : yypt+1]
//line php5/php5.y:3674
//line php5/php5.y:3678
{
yyVAL.node = expr.NewRequireOnce(yyDollar[2].node)
positions.AddPosition(yyVAL.node, positionBuilder.NewTokenNodePosition(yyDollar[1].token, yyDollar[2].node))
@ -6578,31 +6580,31 @@ yydefault:
}
case 510:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3683
//line php5/php5.y:3687
{
yyVAL.list = []node.Node{yyDollar[1].node}
}
case 511:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3685
//line php5/php5.y:3689
{
yyVAL.list = append(yyDollar[1].list, yyDollar[3].node)
}
case 512:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3690
//line php5/php5.y:3694
{
yyVAL.node = yyDollar[1].node
}
case 513:
yyDollar = yyS[yypt-1 : yypt+1]
//line php5/php5.y:3692
//line php5/php5.y:3696
{
yyVAL.node = yyDollar[1].node
}
case 514:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3697
//line php5/php5.y:3701
{
target := node.NewIdentifier(yyDollar[3].token.Value)
positions.AddPosition(target, positionBuilder.NewTokenPosition(yyDollar[3].token))
@ -6614,7 +6616,7 @@ yydefault:
}
case 515:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3707
//line php5/php5.y:3711
{
target := node.NewIdentifier(yyDollar[3].token.Value)
positions.AddPosition(target, positionBuilder.NewTokenPosition(yyDollar[3].token))
@ -6626,7 +6628,7 @@ yydefault:
}
case 516:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3720
//line php5/php5.y:3724
{
target := node.NewIdentifier(yyDollar[3].token.Value)
positions.AddPosition(target, positionBuilder.NewTokenPosition(yyDollar[3].token))
@ -6638,7 +6640,7 @@ yydefault:
}
case 517:
yyDollar = yyS[yypt-3 : yypt+1]
//line php5/php5.y:3733
//line php5/php5.y:3737
{
target := node.NewIdentifier(yyDollar[3].token.Value)
positions.AddPosition(target, positionBuilder.NewTokenPosition(yyDollar[3].token))

View File

@ -3426,7 +3426,11 @@ assignment_list:
assignment_list_element:
variable
{ $$ = $1 }
{
$$ = expr.NewArrayItem(nil, $1, false)
positions.AddPosition($$, positionBuilder.NewNodePosition($1))
comments.AddComments($$, comments[$1])
}
| T_LIST '(' assignment_list ')'
{
$$ = expr.NewList($3)