php-parser/node/stmt/t_try_catch_finnaly_test.go

641 lines
13 KiB
Go
Raw Normal View History

2018-02-08 16:42:01 +00:00
package stmt_test
import (
2018-06-24 07:19:44 +00:00
"testing"
"gotest.tools/assert"
2018-02-08 16:42:01 +00:00
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/name"
2018-06-24 07:19:44 +00:00
"github.com/z7zmey/php-parser/position"
2018-02-08 16:42:01 +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 TestTry(t *testing.T) {
src := `<?
try {}
`
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: -1,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: -1,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{
&stmt.Try{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: -1,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: -1,
},
Stmts: []node.Node{},
2018-02-08 16:42:01 +00:00
Catches: []node.Node{},
},
},
}
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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +00:00
}
func TestTryCatch(t *testing.T) {
src := `<?
try {} catch (Exception $e) {}
`
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: 2,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: 36,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{
&stmt.Try{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: 36,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{},
Catches: []node.Node{
&stmt.Catch{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 13,
2018-06-24 07:19:44 +00:00
EndPos: 36,
},
2018-02-08 16:42:01 +00:00
Types: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
2018-02-08 16:42:01 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
Value: "Exception",
},
2018-02-08 16:42:01 +00:00
},
},
},
Variable: &expr.Variable{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 30,
2018-06-24 07:19:44 +00:00
EndPos: 32,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 30,
2018-06-24 07:19:44 +00:00
EndPos: 32,
},
Value: "e",
},
2018-02-08 16:42:01 +00:00
},
Stmts: []node.Node{},
},
},
},
},
}
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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +00:00
}
func TestPhp7TryCatch(t *testing.T) {
src := `<?
try {} catch (Exception|RuntimeException $e) {}
`
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: 2,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: 53,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{
&stmt.Try{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: 53,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{},
Catches: []node.Node{
&stmt.Catch{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 13,
2018-06-24 07:19:44 +00:00
EndPos: 53,
},
2018-02-08 16:42:01 +00:00
Types: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
2018-02-08 16:42:01 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
Value: "Exception",
},
2018-02-08 16:42:01 +00:00
},
},
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 30,
2018-06-24 07:19:44 +00:00
EndPos: 46,
},
2018-02-08 16:42:01 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 30,
2018-06-24 07:19:44 +00:00
EndPos: 46,
},
Value: "RuntimeException",
},
2018-02-08 16:42:01 +00:00
},
},
},
Variable: &expr.Variable{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 47,
2018-06-24 07:19:44 +00:00
EndPos: 49,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 47,
2018-06-24 07:19:44 +00:00
EndPos: 49,
},
Value: "e",
},
2018-02-08 16:42:01 +00:00
},
Stmts: []node.Node{},
},
},
},
},
}
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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +00:00
}
func TestTryCatchCatch(t *testing.T) {
src := `<?
try {} catch (Exception $e) {} catch (RuntimeException $e) {}
`
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: 2,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: 67,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{
&stmt.Try{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: 67,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{},
Catches: []node.Node{
&stmt.Catch{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 13,
2018-06-24 07:19:44 +00:00
EndPos: 36,
},
2018-02-08 16:42:01 +00:00
Types: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
2018-02-08 16:42:01 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
Value: "Exception",
},
2018-02-08 16:42:01 +00:00
},
},
},
Variable: &expr.Variable{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 30,
2018-06-24 07:19:44 +00:00
EndPos: 32,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 30,
2018-06-24 07:19:44 +00:00
EndPos: 32,
},
Value: "e",
},
2018-02-08 16:42:01 +00:00
},
Stmts: []node.Node{},
},
&stmt.Catch{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 37,
2018-06-24 07:19:44 +00:00
EndPos: 67,
},
2018-02-08 16:42:01 +00:00
Types: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 44,
2018-06-24 07:19:44 +00:00
EndPos: 60,
},
2018-02-08 16:42:01 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 44,
2018-06-24 07:19:44 +00:00
EndPos: 60,
},
Value: "RuntimeException",
},
2018-02-08 16:42:01 +00:00
},
},
},
Variable: &expr.Variable{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 61,
2018-06-24 07:19:44 +00:00
EndPos: 63,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 61,
2018-06-24 07:19:44 +00:00
EndPos: 63,
},
Value: "e",
},
2018-02-08 16:42:01 +00:00
},
Stmts: []node.Node{},
},
},
},
},
}
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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +00:00
}
func TestTryCatchFinally(t *testing.T) {
src := `<?
try {} catch (Exception $e) {} finally {}
`
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: 2,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: 47,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{
&stmt.Try{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 6,
2018-06-24 07:19:44 +00:00
EndPos: 47,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{},
Catches: []node.Node{
&stmt.Catch{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 13,
2018-06-24 07:19:44 +00:00
EndPos: 36,
},
2018-02-08 16:42:01 +00:00
Types: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
2018-02-08 16:42:01 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 20,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
Value: "Exception",
},
2018-02-08 16:42:01 +00:00
},
},
},
Variable: &expr.Variable{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 30,
2018-06-24 07:19:44 +00:00
EndPos: 32,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 30,
2018-06-24 07:19:44 +00:00
EndPos: 32,
},
Value: "e",
},
2018-02-08 16:42:01 +00:00
},
Stmts: []node.Node{},
},
},
Finally: &stmt.Finally{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
2019-03-10 21:37:01 +00:00
StartPos: 37,
2018-06-24 07:19:44 +00:00
EndPos: 47,
},
2018-02-08 16:42:01 +00:00
Stmts: []node.Node{},
},
},
},
}
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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +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()
assert.DeepEqual(t, expected, actual)
2018-02-08 16:42:01 +00:00
}
2018-02-13 17:38:37 +00:00
func TestTryCatchCatchCatch(t *testing.T) {
src := `<? try {} catch (Exception $e) {} catch (\RuntimeException $e) {} catch (namespace\AdditionException $e) {}`
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: 107,
},
2018-02-13 17:38:37 +00:00
Stmts: []node.Node{
&stmt.Try{
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: 107,
},
2018-02-13 17:38:37 +00:00
Stmts: []node.Node{},
Catches: []node.Node{
&stmt.Catch{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 10,
2018-06-24 07:19:44 +00:00
EndPos: 33,
},
2018-02-13 17:38:37 +00:00
Types: []node.Node{
&name.Name{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 17,
2018-06-24 07:19:44 +00:00
EndPos: 26,
},
2018-02-13 17:38:37 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 17,
2018-06-24 07:19:44 +00:00
EndPos: 26,
},
Value: "Exception",
},
2018-02-13 17:38:37 +00:00
},
},
},
Variable: &expr.Variable{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 27,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 27,
2018-06-24 07:19:44 +00:00
EndPos: 29,
},
Value: "e",
},
2018-02-13 17:38:37 +00:00
},
Stmts: []node.Node{},
},
&stmt.Catch{
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: 65,
},
2018-02-13 17:38:37 +00:00
Types: []node.Node{
&name.FullyQualified{
2018-06-24 07:19:44 +00:00
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: 58,
},
2018-02-13 17:38:37 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
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: 58,
},
Value: "RuntimeException",
},
2018-02-13 17:38:37 +00:00
},
},
},
Variable: &expr.Variable{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 59,
2018-06-24 07:19:44 +00:00
EndPos: 61,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 59,
2018-06-24 07:19:44 +00:00
EndPos: 61,
},
Value: "e",
},
2018-02-13 17:38:37 +00:00
},
Stmts: []node.Node{},
},
&stmt.Catch{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 66,
2018-06-24 07:19:44 +00:00
EndPos: 107,
},
2018-02-13 17:38:37 +00:00
Types: []node.Node{
&name.Relative{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 73,
2018-06-24 07:19:44 +00:00
EndPos: 100,
},
2018-02-13 17:38:37 +00:00
Parts: []node.Node{
2018-06-24 07:19:44 +00:00
&name.NamePart{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 83,
2018-06-24 07:19:44 +00:00
EndPos: 100,
},
Value: "AdditionException",
},
2018-02-13 17:38:37 +00:00
},
},
},
Variable: &expr.Variable{
2018-06-24 07:19:44 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 101,
2018-06-24 07:19:44 +00:00
EndPos: 103,
},
VarName: &node.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
2019-03-10 21:37:01 +00:00
StartPos: 101,
2018-06-24 07:19:44 +00:00
EndPos: 103,
},
Value: "e",
},
2018-02-13 17:38:37 +00:00
},
Stmts: []node.Node{},
},
},
},
},
}
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()
assert.DeepEqual(t, expected, actual)
2018-02-13 17:38:37 +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()
assert.DeepEqual(t, expected, actual)
2018-02-13 17:38:37 +00:00
}