strings tests
This commit is contained in:
parent
f58537742d
commit
45c085a3f4
BIN
test/node/scalar/debug.test
Executable file
BIN
test/node/scalar/debug.test
Executable file
Binary file not shown.
128
test/node/scalar/encapsed_test.go
Normal file
128
test/node/scalar/encapsed_test.go
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/z7zmey/php-parser/node/expr"
|
||||||
|
|
||||||
|
"github.com/kylelemons/godebug/pretty"
|
||||||
|
"github.com/z7zmey/php-parser/node"
|
||||||
|
"github.com/z7zmey/php-parser/node/scalar"
|
||||||
|
"github.com/z7zmey/php-parser/node/stmt"
|
||||||
|
"github.com/z7zmey/php-parser/parser"
|
||||||
|
)
|
||||||
|
|
||||||
|
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}),
|
||||||
|
}
|
||||||
|
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")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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}),
|
||||||
|
}
|
||||||
|
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")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
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")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
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")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
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")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
@ -1,123 +0,0 @@
|
|||||||
package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/kylelemons/godebug/pretty"
|
|
||||||
"github.com/z7zmey/php-parser/node"
|
|
||||||
"github.com/z7zmey/php-parser/parser"
|
|
||||||
"github.com/z7zmey/php-parser/token"
|
|
||||||
"github.com/z7zmey/php-parser/node/scalar"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestDoubleQuotedScalarString(t *testing.T) {
|
|
||||||
src := `<? "test";`
|
|
||||||
|
|
||||||
strToken := token.NewToken([]byte("\"test\""), 1, 1)
|
|
||||||
strNode := scalar.NewString(strToken)
|
|
||||||
expected := node.NewSimpleNode("Statements").Append(strNode)
|
|
||||||
|
|
||||||
node := parser.Parse(bytes.NewBufferString(src), "test.php")
|
|
||||||
|
|
||||||
if diff := pretty.Compare(expected, node); diff != "" {
|
|
||||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMultilineDoubleQuotedScalarString(t *testing.T) {
|
|
||||||
src := `<? "
|
|
||||||
test
|
|
||||||
";`
|
|
||||||
|
|
||||||
strToken := token.NewToken([]byte("\"\n\ttest\n\t\""), 1, 3)
|
|
||||||
strNode := scalar.NewString(strToken)
|
|
||||||
expected := node.NewSimpleNode("Statements").Append(strNode)
|
|
||||||
|
|
||||||
node := parser.Parse(bytes.NewBufferString(src), "test.php")
|
|
||||||
|
|
||||||
if diff := pretty.Compare(expected, node); diff != "" {
|
|
||||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSingleQuotedScalarString(t *testing.T) {
|
|
||||||
src := `<? '$test';`
|
|
||||||
|
|
||||||
strToken := token.NewToken([]byte("'$test'"), 1, 1)
|
|
||||||
strNode := scalar.NewString(strToken)
|
|
||||||
expected := node.NewSimpleNode("Statements").Append(strNode)
|
|
||||||
|
|
||||||
node := parser.Parse(bytes.NewBufferString(src), "test.php")
|
|
||||||
|
|
||||||
if diff := pretty.Compare(expected, node); diff != "" {
|
|
||||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMultilineSingleQuotedScalarString(t *testing.T) {
|
|
||||||
src := `<? '
|
|
||||||
$test
|
|
||||||
';`
|
|
||||||
|
|
||||||
strToken := token.NewToken([]byte("'\n\t$test\n\t'"), 1, 3)
|
|
||||||
strNode := scalar.NewString(strToken)
|
|
||||||
expected := node.NewSimpleNode("Statements").Append(strNode)
|
|
||||||
|
|
||||||
node := parser.Parse(bytes.NewBufferString(src), "test.php")
|
|
||||||
|
|
||||||
if diff := pretty.Compare(expected, node); diff != "" {
|
|
||||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPlainHeredocScalarString(t *testing.T) {
|
|
||||||
src := `<? <<<CAD
|
|
||||||
hello
|
|
||||||
CAD;
|
|
||||||
`
|
|
||||||
|
|
||||||
strToken := token.NewToken([]byte("\thello\n"), 2, 3)
|
|
||||||
strNode := scalar.NewString(strToken)
|
|
||||||
expected := node.NewSimpleNode("Statements").Append(strNode)
|
|
||||||
|
|
||||||
node := parser.Parse(bytes.NewBufferString(src), "test.php")
|
|
||||||
|
|
||||||
if diff := pretty.Compare(expected, node); diff != "" {
|
|
||||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHeredocScalarString(t *testing.T) {
|
|
||||||
src := `<? <<<"CAD"
|
|
||||||
hello
|
|
||||||
CAD;
|
|
||||||
`
|
|
||||||
|
|
||||||
strToken := token.NewToken([]byte("\thello\n"), 2, 3)
|
|
||||||
strNode := scalar.NewString(strToken)
|
|
||||||
expected := node.NewSimpleNode("Statements").Append(strNode)
|
|
||||||
|
|
||||||
node := parser.Parse(bytes.NewBufferString(src), "test.php")
|
|
||||||
|
|
||||||
if diff := pretty.Compare(expected, node); diff != "" {
|
|
||||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNowdocScalarString(t *testing.T) {
|
|
||||||
src := `<? <<<'CAD'
|
|
||||||
hello $world
|
|
||||||
CAD;
|
|
||||||
`
|
|
||||||
|
|
||||||
strToken := token.NewToken([]byte("\thello $world\n"), 2, 3)
|
|
||||||
strNode := scalar.NewString(strToken)
|
|
||||||
expected := node.NewSimpleNode("Statements").Append(strNode)
|
|
||||||
|
|
||||||
node := parser.Parse(bytes.NewBufferString(src), "test.php")
|
|
||||||
|
|
||||||
if diff := pretty.Compare(expected, node); diff != "" {
|
|
||||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
|
||||||
}
|
|
||||||
}
|
|
136
test/node/scalar/string_test.go
Normal file
136
test/node/scalar/string_test.go
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kylelemons/godebug/pretty"
|
||||||
|
"github.com/z7zmey/php-parser/node"
|
||||||
|
"github.com/z7zmey/php-parser/node/scalar"
|
||||||
|
"github.com/z7zmey/php-parser/node/stmt"
|
||||||
|
"github.com/z7zmey/php-parser/parser"
|
||||||
|
)
|
||||||
|
|
||||||
|
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})
|
||||||
|
|
||||||
|
actual := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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})
|
||||||
|
|
||||||
|
actual := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultilineDoubleQuotedScalarString(t *testing.T) {
|
||||||
|
src := `<? "
|
||||||
|
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})
|
||||||
|
|
||||||
|
actual := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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})
|
||||||
|
|
||||||
|
actual := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultilineSingleQuotedScalarString(t *testing.T) {
|
||||||
|
src := `<? '
|
||||||
|
$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})
|
||||||
|
|
||||||
|
actual := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPlainHeredocScalarString(t *testing.T) {
|
||||||
|
src := `<? <<<CAD
|
||||||
|
hello
|
||||||
|
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})
|
||||||
|
|
||||||
|
actual := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHeredocScalarString(t *testing.T) {
|
||||||
|
src := `<? <<<"CAD"
|
||||||
|
hello
|
||||||
|
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})
|
||||||
|
|
||||||
|
actual := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNowdocScalarString(t *testing.T) {
|
||||||
|
src := `<? <<<'CAD'
|
||||||
|
hello $world
|
||||||
|
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})
|
||||||
|
|
||||||
|
actual := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||||
|
|
||||||
|
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user