php-parser/node/stmt/t_for_test.go
2019-03-20 21:06:56 +02:00

292 lines
6.0 KiB
Go

package stmt_test
import (
"testing"
"gotest.tools/assert"
"github.com/z7zmey/php-parser/node/expr/binary"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/expr/assign"
"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 TestFor(t *testing.T) {
src := `<? for($i = 0; $i < 10; $i++, $i++) {}`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 38,
},
Stmts: []node.Node{
&stmt.For{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 38,
},
Init: []node.Node{
&assign.Assign{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 7,
EndPos: 13,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 7,
EndPos: 9,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 7,
EndPos: 9,
},
Value: "i",
},
},
Expression: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 12,
EndPos: 13,
},
Value: "0",
},
},
},
Cond: []node.Node{
&binary.Smaller{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 22,
},
Left: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 17,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 15,
EndPos: 17,
},
Value: "i",
},
},
Right: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 20,
EndPos: 22,
},
Value: "10",
},
},
},
Loop: []node.Node{
&expr.PostInc{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 28,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 26,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 24,
EndPos: 26,
},
Value: "i",
},
},
},
&expr.PostInc{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 34,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 30,
EndPos: 32,
},
Value: "i",
},
},
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 36,
EndPos: 38,
},
Stmts: []node.Node{},
},
},
},
}
php7parser := php7.NewParser([]byte(src))
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
php5parser := php5.NewParser([]byte(src))
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}
func TestAltFor(t *testing.T) {
src := `<? for(; $i < 10; $i++) : endfor;`
expected := &node.Root{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 33,
},
Stmts: []node.Node{
&stmt.AltFor{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 3,
EndPos: 33,
},
Cond: []node.Node{
&binary.Smaller{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 16,
},
Left: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 11,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 9,
EndPos: 11,
},
Value: "i",
},
},
Right: &scalar.Lnumber{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 14,
EndPos: 16,
},
Value: "10",
},
},
},
Loop: []node.Node{
&expr.PostInc{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 22,
},
Variable: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 18,
EndPos: 20,
},
Value: "i",
},
},
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: -1,
EndLine: -1,
StartPos: -1,
EndPos: -1,
},
Stmts: []node.Node{},
},
},
},
}
php7parser := php7.NewParser([]byte(src))
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
php5parser := php5.NewParser([]byte(src))
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
}