Foreach tests
This commit is contained in:
125
node/stmt/t_foreach_test.go
Normal file
125
node/stmt/t_foreach_test.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user