php-parser/test/node/scalar/node_scalar_string_test.go

124 lines
3.1 KiB
Go
Raw Normal View History

2017-12-04 10:40:36 +00:00
package test
import (
"bytes"
"testing"
2017-12-04 10:56:55 +00:00
"github.com/kylelemons/godebug/pretty"
2017-12-04 10:40:36 +00:00
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/token"
2017-12-04 22:02:24 +00:00
"github.com/z7zmey/php-parser/node/scalar"
2017-12-04 10:40:36 +00:00
)
2017-12-04 17:50:22 +00:00
func TestDoubleQuotedScalarString(t *testing.T) {
2017-12-04 10:40:36 +00:00
src := `<? "test";`
strToken := token.NewToken([]byte("\"test\""), 1, 1)
2017-12-04 22:02:24 +00:00
strNode := scalar.NewString(strToken)
expected := node.NewSimpleNode("Statements").Append(strNode)
2017-12-04 10:40:36 +00:00
node := parser.Parse(bytes.NewBufferString(src), "test.php")
2017-12-04 10:56:55 +00:00
if diff := pretty.Compare(expected, node); diff != "" {
2017-12-04 17:50:22 +00:00
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)
2017-12-04 22:02:24 +00:00
strNode := scalar.NewString(strToken)
expected := node.NewSimpleNode("Statements").Append(strNode)
2017-12-04 17:50:22 +00:00
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)
2017-12-04 22:02:24 +00:00
strNode := scalar.NewString(strToken)
expected := node.NewSimpleNode("Statements").Append(strNode)
2017-12-04 17:50:22 +00:00
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)
2017-12-04 22:02:24 +00:00
strNode := scalar.NewString(strToken)
expected := node.NewSimpleNode("Statements").Append(strNode)
2017-12-04 17:50:22 +00:00
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)
2017-12-04 22:02:24 +00:00
strNode := scalar.NewString(strToken)
expected := node.NewSimpleNode("Statements").Append(strNode)
2017-12-04 17:50:22 +00:00
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)
2017-12-04 22:02:24 +00:00
strNode := scalar.NewString(strToken)
expected := node.NewSimpleNode("Statements").Append(strNode)
2017-12-04 17:50:22 +00:00
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)
2017-12-04 22:02:24 +00:00
strNode := scalar.NewString(strToken)
expected := node.NewSimpleNode("Statements").Append(strNode)
2017-12-04 17:50:22 +00:00
node := parser.Parse(bytes.NewBufferString(src), "test.php")
if diff := pretty.Compare(expected, node); diff != "" {
t.Errorf("diff: (-expected +actual)\n%s", diff)
2017-12-04 10:40:36 +00:00
}
}