php-parser/node/stmt/t_if_test.go

546 lines
11 KiB
Go
Raw Normal View History

2018-02-08 21:31:38 +00:00
package stmt_test
import (
"testing"
"gotest.tools/assert"
2018-02-08 21:31:38 +00:00
"github.com/z7zmey/php-parser/node/expr"
2018-06-24 07:19:44 +00:00
"github.com/z7zmey/php-parser/position"
2018-02-08 21:31:38 +00:00
"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 TestIf(t *testing.T) {
src := `<? if ($a) {}`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 13,
},
2018-02-08 21:31:38 +00:00
Stmts: []node.Node{
&stmt.If{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 13,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
Value: "a",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 11,
2018-06-24 07:19:44 +00:00
EndPos: 13,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
},
},
}
2019-03-10 21:37:01 +00:00
php7parser := php7.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
2019-03-10 21:37:01 +00:00
php5parser := php5.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
}
func TestElseIf(t *testing.T) {
2018-06-24 07:19:44 +00:00
src := `<? if ($a) {} elseif ($b) {}`
2018-02-08 21:31:38 +00:00
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 28,
},
2018-02-08 21:31:38 +00:00
Stmts: []node.Node{
&stmt.If{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 28,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
Value: "a",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 11,
2018-06-24 07:19:44 +00:00
EndPos: 13,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
ElseIf: []node.Node{
&stmt.ElseIf{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 28,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 22,
2018-06-24 07:19:44 +00:00
EndPos: 24,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 22,
2018-06-24 07:19:44 +00:00
EndPos: 24,
},
Value: "b",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 26,
2018-06-24 07:19:44 +00:00
EndPos: 28,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
},
},
},
},
}
2019-03-10 21:37:01 +00:00
php7parser := php7.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
2019-03-10 21:37:01 +00:00
php5parser := php5.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
}
func TestElse(t *testing.T) {
src := `<? if ($a) {} else {}`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 21,
},
2018-02-08 21:31:38 +00:00
Stmts: []node.Node{
&stmt.If{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 21,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
Value: "a",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 11,
2018-06-24 07:19:44 +00:00
EndPos: 13,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
Else: &stmt.Else{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 21,
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 19,
2018-06-24 07:19:44 +00:00
EndPos: 21,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
},
},
},
}
2019-03-10 21:37:01 +00:00
php7parser := php7.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
2019-03-10 21:37:01 +00:00
php5parser := php5.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
}
func TestElseElseIf(t *testing.T) {
src := `<? if ($a) {} elseif ($b) {} elseif ($c) {} else {}`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 51,
},
2018-02-08 21:31:38 +00:00
Stmts: []node.Node{
&stmt.If{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 51,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
Value: "a",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 11,
2018-06-24 07:19:44 +00:00
EndPos: 13,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
ElseIf: []node.Node{
&stmt.ElseIf{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 28,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 22,
2018-06-24 07:19:44 +00:00
EndPos: 24,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 22,
2018-06-24 07:19:44 +00:00
EndPos: 24,
},
Value: "b",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 26,
2018-06-24 07:19:44 +00:00
EndPos: 28,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
},
&stmt.ElseIf{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 29,
2018-06-24 07:19:44 +00:00
EndPos: 43,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 37,
2018-06-24 07:19:44 +00:00
EndPos: 39,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 37,
2018-06-24 07:19:44 +00:00
EndPos: 39,
},
Value: "c",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 41,
2018-06-24 07:19:44 +00:00
EndPos: 43,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
},
},
Else: &stmt.Else{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 44,
2018-06-24 07:19:44 +00:00
EndPos: 51,
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 49,
2018-06-24 07:19:44 +00:00
EndPos: 51,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
},
},
},
}
2019-03-10 21:37:01 +00:00
php7parser := php7.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
2019-03-10 21:37:01 +00:00
php5parser := php5.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
}
func TestElseIfElseIfElse(t *testing.T) {
src := `<? if ($a) {} elseif ($b) {} else if ($c) {} else {}`
2018-05-02 09:14:24 +00:00
expected := &node.Root{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 52,
},
2018-02-08 21:31:38 +00:00
Stmts: []node.Node{
&stmt.If{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 3,
2018-06-24 07:19:44 +00:00
EndPos: 52,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 7,
2018-06-24 07:19:44 +00:00
EndPos: 9,
},
Value: "a",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 11,
2018-06-24 07:19:44 +00:00
EndPos: 13,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
ElseIf: []node.Node{
&stmt.ElseIf{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 14,
2018-06-24 07:19:44 +00:00
EndPos: 28,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 22,
2018-06-24 07:19:44 +00:00
EndPos: 24,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 22,
2018-06-24 07:19:44 +00:00
EndPos: 24,
},
Value: "b",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 26,
2018-06-24 07:19:44 +00:00
EndPos: 28,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
},
},
Else: &stmt.Else{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 29,
2018-06-24 07:19:44 +00:00
EndPos: 52,
},
2018-02-08 21:31:38 +00:00
Stmt: &stmt.If{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 34,
2018-06-24 07:19:44 +00:00
EndPos: 52,
},
Cond: &expr.Variable{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 38,
2018-06-24 07:19:44 +00:00
EndPos: 40,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 38,
2018-06-24 07:19:44 +00:00
EndPos: 40,
},
Value: "c",
},
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 42,
2018-06-24 07:19:44 +00:00
EndPos: 44,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
Else: &stmt.Else{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 45,
2018-06-24 07:19:44 +00:00
EndPos: 52,
},
Stmt: &stmt.StmtList{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 50,
2018-06-24 07:19:44 +00:00
EndPos: 52,
},
Stmts: []node.Node{},
},
2018-02-08 21:31:38 +00:00
},
},
},
},
},
}
2019-03-10 21:37:01 +00:00
php7parser := php7.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php7parser.Parse()
actual := php7parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
2019-03-10 21:37:01 +00:00
php5parser := php5.NewParser([]byte(src))
2018-04-10 12:23:13 +00:00
php5parser.Parse()
actual = php5parser.GetRootNode()
assert.DeepEqual(t, expected, actual)
2018-02-08 21:31:38 +00:00
}