extract positions package
This commit is contained in:
@@ -16,16 +16,20 @@ import (
|
||||
func TestSimpleVar(t *testing.T) {
|
||||
src := `<? "test $var";`
|
||||
|
||||
varName := node.NewIdentifier("$var").SetPosition(&node.Position{1, 1, 10, 13})
|
||||
parts := []node.Node{
|
||||
scalar.NewEncapsedStringPart("test ").SetPosition(&node.Position{1, 1, 5, 9}),
|
||||
expr.NewVariable(varName).SetPosition(&node.Position{1, 1, 10, 13}),
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Encapsed{
|
||||
Parts: []node.Node{
|
||||
&scalar.EncapsedStringPart{Value: "test "},
|
||||
&expr.Variable{VarName: &node.Identifier{Value: "$var"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
encapsed := scalar.NewEncapsed(parts).SetPosition(&node.Position{1, 1, 4, 14})
|
||||
expr := stmt.NewExpression(encapsed).SetPosition(&node.Position{1, 1, 4, 15})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 1, 4, 15})
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -35,22 +39,24 @@ func TestSimpleVar(t *testing.T) {
|
||||
func TestSimpleVarPropertyFetch(t *testing.T) {
|
||||
src := `<? "test $foo->bar()";`
|
||||
|
||||
varName := node.NewIdentifier("$foo").SetPosition(&node.Position{1, 1, 10, 13})
|
||||
variable := expr.NewVariable(varName).SetPosition(&node.Position{1, 1, 10, 13})
|
||||
|
||||
property := node.NewIdentifier("bar").SetPosition(&node.Position{1, 1, 16, 18})
|
||||
propertyFetch := expr.NewPropertyFetch(variable, property).SetPosition(&node.Position{1, 1, 10, 18})
|
||||
|
||||
parts := []node.Node{
|
||||
scalar.NewEncapsedStringPart("test ").SetPosition(&node.Position{1, 1, 5, 9}),
|
||||
propertyFetch,
|
||||
scalar.NewEncapsedStringPart("()").SetPosition(&node.Position{1, 1, 19, 20}),
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Encapsed{
|
||||
Parts: []node.Node{
|
||||
&scalar.EncapsedStringPart{Value: "test "},
|
||||
&expr.PropertyFetch{
|
||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$foo"}},
|
||||
Property: &node.Identifier{Value: "bar"},
|
||||
},
|
||||
&scalar.EncapsedStringPart{Value: "()"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
encapsed := scalar.NewEncapsed(parts).SetPosition(&node.Position{1, 1, 4, 21})
|
||||
expr := stmt.NewExpression(encapsed).SetPosition(&node.Position{1, 1, 4, 22})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 1, 4, 22})
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -60,17 +66,20 @@ func TestSimpleVarPropertyFetch(t *testing.T) {
|
||||
func TestDollarOpenCurlyBraces(t *testing.T) {
|
||||
src := `<? "test ${foo}";`
|
||||
|
||||
varName := node.NewIdentifier("foo").SetPosition(&node.Position{1, 1, 12, 14})
|
||||
variable := expr.NewVariable(varName).SetPosition(&node.Position{1, 1, 10, 15})
|
||||
parts := []node.Node{
|
||||
scalar.NewEncapsedStringPart("test ").SetPosition(&node.Position{1, 1, 5, 9}),
|
||||
variable,
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Encapsed{
|
||||
Parts: []node.Node{
|
||||
&scalar.EncapsedStringPart{Value: "test "},
|
||||
&expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
encapsed := scalar.NewEncapsed(parts).SetPosition(&node.Position{1, 1, 4, 16})
|
||||
expr := stmt.NewExpression(encapsed).SetPosition(&node.Position{1, 1, 4, 17})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 1, 4, 17})
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -80,22 +89,23 @@ func TestDollarOpenCurlyBraces(t *testing.T) {
|
||||
func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
|
||||
src := `<? "test ${foo[0]}";`
|
||||
|
||||
varName := node.NewIdentifier("foo").SetPosition(&node.Position{1, 1, 12, 14})
|
||||
variable := expr.NewVariable(varName).SetPosition(&node.Position{1, 1, 12, 14})
|
||||
|
||||
lNumber := scalar.NewLnumber("0").SetPosition(&node.Position{1, 1, 16, 16})
|
||||
|
||||
arrayDimFetch := expr.NewArrayDimFetch(variable, lNumber).SetPosition(&node.Position{1, 1, 10, 18})
|
||||
|
||||
parts := []node.Node{
|
||||
scalar.NewEncapsedStringPart("test ").SetPosition(&node.Position{1, 1, 5, 9}),
|
||||
arrayDimFetch,
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Encapsed{
|
||||
Parts: []node.Node{
|
||||
&scalar.EncapsedStringPart{Value: "test "},
|
||||
&expr.ArrayDimFetch{
|
||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "foo"}},
|
||||
Dim: &scalar.Lnumber{Value: "0"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
encapsed := scalar.NewEncapsed(parts).SetPosition(&node.Position{1, 1, 4, 19})
|
||||
expr := stmt.NewExpression(encapsed).SetPosition(&node.Position{1, 1, 4, 20})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 1, 4, 20})
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -105,22 +115,23 @@ func TestDollarOpenCurlyBracesDimNumber(t *testing.T) {
|
||||
func TestCurlyOpenMethodCall(t *testing.T) {
|
||||
src := `<? "test {$foo->bar()}";`
|
||||
|
||||
varName := node.NewIdentifier("$foo").SetPosition(&node.Position{1, 1, 11, 14})
|
||||
variable := expr.NewVariable(varName).SetPosition(&node.Position{1, 1, 11, 14})
|
||||
|
||||
method := node.NewIdentifier("bar").SetPosition(&node.Position{1, 1, 17, 19})
|
||||
|
||||
methodCall := expr.NewMethodCall(variable, method, nil).SetPosition(&node.Position{1, 1, 11, 21})
|
||||
|
||||
parts := []node.Node{
|
||||
scalar.NewEncapsedStringPart("test ").SetPosition(&node.Position{1, 1, 5, 9}),
|
||||
methodCall,
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Encapsed{
|
||||
Parts: []node.Node{
|
||||
&scalar.EncapsedStringPart{Value: "test "},
|
||||
&expr.MethodCall{
|
||||
Variable: &expr.Variable{VarName: &node.Identifier{Value: "$foo"}},
|
||||
Method: &node.Identifier{Value: "bar"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
encapsed := scalar.NewEncapsed(parts).SetPosition(&node.Position{1, 1, 4, 23})
|
||||
expr := stmt.NewExpression(encapsed).SetPosition(&node.Position{1, 1, 4, 24})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 1, 4, 24})
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
|
||||
@@ -14,11 +14,15 @@ import (
|
||||
func TestDoubleQuotedScalarString(t *testing.T) {
|
||||
src := `<? "test";`
|
||||
|
||||
str := scalar.NewString("\"test\"").SetPosition(&node.Position{1, 1, 4, 9})
|
||||
expr := stmt.NewExpression(str).SetPosition(&node.Position{1, 1, 4, 10})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 1, 4, 10})
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\"test\""},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -27,11 +31,15 @@ func TestDoubleQuotedScalarString(t *testing.T) {
|
||||
func TestDoubleQuotedScalarStringWithEscapedVar(t *testing.T) {
|
||||
src := `<? "\$test";`
|
||||
|
||||
str := scalar.NewString("\"\\$test\"").SetPosition(&node.Position{1, 1, 4, 11})
|
||||
expr := stmt.NewExpression(str).SetPosition(&node.Position{1, 1, 4, 12})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 1, 4, 12})
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\"\\$test\""},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -43,11 +51,15 @@ func TestMultilineDoubleQuotedScalarString(t *testing.T) {
|
||||
test
|
||||
";`
|
||||
|
||||
str := scalar.NewString("\"\n\ttest\n\t\"").SetPosition(&node.Position{1, 3, 4, 13})
|
||||
expr := stmt.NewExpression(str).SetPosition(&node.Position{1, 3, 4, 14})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 3, 4, 14})
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\"\n\ttest\n\t\""},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -57,11 +69,15 @@ func TestMultilineDoubleQuotedScalarString(t *testing.T) {
|
||||
func TestSingleQuotedScalarString(t *testing.T) {
|
||||
src := `<? '$test';`
|
||||
|
||||
str := scalar.NewString("'$test'").SetPosition(&node.Position{1, 1, 4, 10})
|
||||
expr := stmt.NewExpression(str).SetPosition(&node.Position{1, 1, 4, 11})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 1, 4, 11})
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "'$test'"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -73,11 +89,15 @@ func TestMultilineSingleQuotedScalarString(t *testing.T) {
|
||||
$test
|
||||
';`
|
||||
|
||||
str := scalar.NewString("'\n\t$test\n\t'").SetPosition(&node.Position{1, 3, 4, 14})
|
||||
expr := stmt.NewExpression(str).SetPosition(&node.Position{1, 3, 4, 15})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 3, 4, 15})
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "'\n\t$test\n\t'"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -90,11 +110,15 @@ func TestPlainHeredocScalarString(t *testing.T) {
|
||||
CAD;
|
||||
`
|
||||
|
||||
str := scalar.NewString("\thello\n").SetPosition(&node.Position{1, 3, 4, 20})
|
||||
expr := stmt.NewExpression(str).SetPosition(&node.Position{1, 3, 4, 21})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 3, 4, 21})
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\thello\n"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -107,11 +131,15 @@ func TestHeredocScalarString(t *testing.T) {
|
||||
CAD;
|
||||
`
|
||||
|
||||
str := scalar.NewString("\thello\n").SetPosition(&node.Position{1, 3, 4, 22})
|
||||
expr := stmt.NewExpression(str).SetPosition(&node.Position{1, 3, 4, 23})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 3, 4, 23})
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\thello\n"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
@@ -124,11 +152,15 @@ func TestNowdocScalarString(t *testing.T) {
|
||||
CAD;
|
||||
`
|
||||
|
||||
str := scalar.NewString("\thello $world\n").SetPosition(&node.Position{1, 3, 4, 29})
|
||||
expr := stmt.NewExpression(str).SetPosition(&node.Position{1, 3, 4, 30})
|
||||
expected := stmt.NewStmtList([]node.Node{expr}).SetPosition(&node.Position{1, 3, 4, 30})
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\thello $world\n"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
|
||||
Reference in New Issue
Block a user