2018-02-08 16:26:28 +00:00
|
|
|
package stmt_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
"gotest.tools/assert"
|
|
|
|
|
2018-02-08 16:26:28 +00:00
|
|
|
"github.com/z7zmey/php-parser/node/scalar"
|
2018-06-24 07:19:44 +00:00
|
|
|
"github.com/z7zmey/php-parser/position"
|
2018-02-08 16:26:28 +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 TestAltSwitch(t *testing.T) {
|
|
|
|
src := `<?
|
|
|
|
switch (1) :
|
|
|
|
case 1:
|
|
|
|
default:
|
|
|
|
case 2;
|
|
|
|
endswitch;
|
|
|
|
`
|
|
|
|
|
2018-05-02 09:14:24 +00:00
|
|
|
expected := &node.Root{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 6,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 6,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 65,
|
|
|
|
},
|
2018-02-08 16:26:28 +00:00
|
|
|
Stmts: []node.Node{
|
2018-02-18 18:39:41 +00:00
|
|
|
&stmt.AltSwitch{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 6,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 6,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 65,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 2,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 14,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 15,
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
CaseList: &stmt.CaseList{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: -1,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 22,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: -1,
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: -1,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 22,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: -1,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: 3,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 27,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 28,
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
&stmt.Default{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: -1,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 33,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: -1,
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
&stmt.Case{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 5,
|
|
|
|
EndLine: -1,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 45,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: -1,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 5,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 50,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 51,
|
|
|
|
},
|
|
|
|
Value: "2",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
2018-02-08 16:26:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php7parser := php7.NewParser([]byte(src), "7.4")
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-08 16:26:28 +00:00
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php5parser := php5.NewParser([]byte(src), "5.6")
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-12 21:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAltSwitchSemicolon(t *testing.T) {
|
|
|
|
src := `<?
|
|
|
|
switch (1) :;
|
|
|
|
case 1;
|
|
|
|
case 2;
|
|
|
|
endswitch;
|
|
|
|
`
|
|
|
|
|
2018-05-02 09:14:24 +00:00
|
|
|
expected := &node.Root{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 6,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 54,
|
|
|
|
},
|
2018-02-12 21:10:53 +00:00
|
|
|
Stmts: []node.Node{
|
2018-02-18 18:39:41 +00:00
|
|
|
&stmt.AltSwitch{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 6,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 54,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 2,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 14,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 15,
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
CaseList: &stmt.CaseList{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: -1,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 23,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: -1,
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: -1,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 23,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: -1,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: 3,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 28,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 29,
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
|
|
|
&stmt.Case{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: -1,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 34,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: -1,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: 4,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 39,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 40,
|
|
|
|
},
|
|
|
|
Value: "2",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{},
|
|
|
|
},
|
2018-02-12 21:10:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php7parser := php7.NewParser([]byte(src), "7.4")
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-08 16:26:28 +00:00
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php5parser := php5.NewParser([]byte(src), "5.6")
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-08 16:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSwitch(t *testing.T) {
|
|
|
|
src := `<?
|
|
|
|
switch (1) {
|
|
|
|
case 1: break;
|
2018-02-12 21:10:53 +00:00
|
|
|
case 2: break;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-05-02 09:14:24 +00:00
|
|
|
expected := &node.Root{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 6,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 58,
|
|
|
|
},
|
2018-02-12 21:10:53 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Switch{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 6,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 58,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 2,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 14,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 15,
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
CaseList: &stmt.CaseList{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 17,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 58,
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: 3,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 22,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 36,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: 3,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 27,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 28,
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{
|
2018-06-24 07:19:44 +00:00
|
|
|
&stmt.Break{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: 3,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 30,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 36,
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
},
|
2018-02-12 21:10:53 +00:00
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
&stmt.Case{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: 4,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 40,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 54,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: 4,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 45,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 46,
|
|
|
|
},
|
|
|
|
Value: "2",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{
|
2018-06-24 07:19:44 +00:00
|
|
|
&stmt.Break{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: 4,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 48,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 54,
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
},
|
2018-02-12 21:10:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php7parser := php7.NewParser([]byte(src), "7.4")
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-12 21:10:53 +00:00
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php5parser := php5.NewParser([]byte(src), "5.6")
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-12 21:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSwitchSemicolon(t *testing.T) {
|
|
|
|
src := `<?
|
|
|
|
switch (1) {;
|
|
|
|
case 1; break;
|
2018-02-08 16:26:28 +00:00
|
|
|
case 2; break;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-05-02 09:14:24 +00:00
|
|
|
expected := &node.Root{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 6,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 59,
|
|
|
|
},
|
2018-02-08 16:26:28 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Switch{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 6,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 59,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 2,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 14,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 15,
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
CaseList: &stmt.CaseList{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 2,
|
|
|
|
EndLine: 5,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 17,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 59,
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Cases: []node.Node{
|
|
|
|
&stmt.Case{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: 3,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 23,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 37,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: 3,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 28,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 29,
|
|
|
|
},
|
|
|
|
Value: "1",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{
|
2018-06-24 07:19:44 +00:00
|
|
|
&stmt.Break{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 3,
|
|
|
|
EndLine: 3,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 31,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 37,
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
},
|
2018-02-08 16:26:28 +00:00
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
&stmt.Case{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: 4,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 41,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 55,
|
|
|
|
},
|
|
|
|
Cond: &scalar.Lnumber{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: 4,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 46,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 47,
|
|
|
|
},
|
|
|
|
Value: "2",
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
Stmts: []node.Node{
|
2018-06-24 07:19:44 +00:00
|
|
|
&stmt.Break{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 4,
|
|
|
|
EndLine: 4,
|
2019-03-10 21:37:01 +00:00
|
|
|
StartPos: 49,
|
2018-06-24 07:19:44 +00:00
|
|
|
EndPos: 55,
|
|
|
|
},
|
|
|
|
},
|
2018-04-29 20:10:56 +00:00
|
|
|
},
|
2018-02-08 16:26:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php7parser := php7.NewParser([]byte(src), "7.4")
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-08 16:26:28 +00:00
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
php5parser := php5.NewParser([]byte(src), "5.6")
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-12 21:10:53 +00:00
|
|
|
}
|