2018-02-09 10:00:41 +00:00
|
|
|
package stmt_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2018-02-12 21:10:53 +00:00
|
|
|
|
2019-02-13 20:18:07 +00:00
|
|
|
"gotest.tools/assert"
|
|
|
|
|
2018-02-09 10:00:41 +00:00
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/node/expr"
|
2018-02-12 21:10:53 +00:00
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
|
|
|
"github.com/z7zmey/php-parser/node/stmt"
|
2018-02-09 10:00:41 +00:00
|
|
|
"github.com/z7zmey/php-parser/php5"
|
|
|
|
"github.com/z7zmey/php-parser/php7"
|
2018-06-24 07:19:44 +00:00
|
|
|
"github.com/z7zmey/php-parser/position"
|
2018-02-09 10:00:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGlobal(t *testing.T) {
|
|
|
|
src := `<? global $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,
|
|
|
|
StartPos: 4,
|
|
|
|
EndPos: 13,
|
|
|
|
},
|
2018-02-09 10:00:41 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Global{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 4,
|
|
|
|
EndPos: 13,
|
|
|
|
},
|
2018-02-09 10:00:41 +00:00
|
|
|
Vars: []node.Node{
|
2018-06-24 07:19:44 +00:00
|
|
|
&expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 11,
|
|
|
|
EndPos: 12,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 11,
|
|
|
|
EndPos: 12,
|
|
|
|
},
|
|
|
|
Value: "a",
|
|
|
|
},
|
|
|
|
},
|
2018-02-09 10:00:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-09 10:00:41 +00:00
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-09 10:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobalVars(t *testing.T) {
|
2018-02-12 21:10:53 +00:00
|
|
|
src := `<? global $a, $b, $$c, ${foo()};`
|
2018-02-09 10:00:41 +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,
|
|
|
|
StartPos: 4,
|
|
|
|
EndPos: 32,
|
|
|
|
},
|
2018-02-09 10:00:41 +00:00
|
|
|
Stmts: []node.Node{
|
|
|
|
&stmt.Global{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 4,
|
|
|
|
EndPos: 32,
|
|
|
|
},
|
2018-02-09 10:00:41 +00:00
|
|
|
Vars: []node.Node{
|
2018-02-12 21:10:53 +00:00
|
|
|
&expr.Variable{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 11,
|
|
|
|
EndPos: 12,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 11,
|
|
|
|
EndPos: 12,
|
|
|
|
},
|
|
|
|
Value: "a",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 15,
|
|
|
|
EndPos: 16,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 15,
|
|
|
|
EndPos: 16,
|
|
|
|
},
|
|
|
|
Value: "b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 19,
|
|
|
|
EndPos: 21,
|
|
|
|
},
|
|
|
|
VarName: &expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 20,
|
|
|
|
EndPos: 21,
|
|
|
|
},
|
|
|
|
VarName: &node.Identifier{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 20,
|
|
|
|
EndPos: 21,
|
|
|
|
},
|
|
|
|
Value: "c",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&expr.Variable{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 24,
|
|
|
|
EndPos: 31,
|
|
|
|
},
|
2018-02-12 21:10:53 +00:00
|
|
|
VarName: &expr.FunctionCall{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 26,
|
|
|
|
EndPos: 30,
|
|
|
|
},
|
2018-02-12 21:10:53 +00:00
|
|
|
Function: &name.Name{
|
2018-06-24 07:19:44 +00:00
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 26,
|
|
|
|
EndPos: 28,
|
|
|
|
},
|
2018-02-12 21:10:53 +00:00
|
|
|
Parts: []node.Node{
|
2018-06-24 07:19:44 +00:00
|
|
|
&name.NamePart{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 26,
|
|
|
|
EndPos: 28,
|
|
|
|
},
|
|
|
|
Value: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ArgumentList: &node.ArgumentList{
|
|
|
|
Position: &position.Position{
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 1,
|
|
|
|
StartPos: 29,
|
|
|
|
EndPos: 30,
|
2018-02-12 21:10:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-02-09 10:00:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php7parser.Parse()
|
|
|
|
actual := php7parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-09 10:00:41 +00:00
|
|
|
|
2018-04-10 12:23:13 +00:00
|
|
|
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
|
|
|
|
php5parser.Parse()
|
|
|
|
actual = php5parser.GetRootNode()
|
2019-02-13 20:18:07 +00:00
|
|
|
assert.DeepEqual(t, expected, actual)
|
2018-02-09 10:00:41 +00:00
|
|
|
}
|