reorganize tests
This commit is contained in:
139
node/scalar/encapsed_test.go
Normal file
139
node/scalar/encapsed_test.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package scalar_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";`
|
||||
|
||||
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"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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()";`
|
||||
|
||||
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: "()"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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}";`
|
||||
|
||||
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"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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]}";`
|
||||
|
||||
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"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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()}";`
|
||||
|
||||
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"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
}
|
||||
}
|
||||
138
node/scalar/numbers_test.go
Normal file
138
node/scalar/numbers_test.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package scalar_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"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 assertEqual(t *testing.T, expected interface{}, actual interface{}) {
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
diff := pretty.Compare(expected, actual)
|
||||
|
||||
if diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
} else {
|
||||
t.Errorf("expected and actual are not equal\n")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestLNumber(t *testing.T) {
|
||||
src := `<? 1234567890123456789;`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Lnumber{Value: "1234567890123456789"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestDNumber(t *testing.T) {
|
||||
src := `<? 12345678901234567890;`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Dnumber{Value: "12345678901234567890"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestFloat(t *testing.T) {
|
||||
src := `<? 0.;`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Dnumber{Value: "0."},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestBinaryLNumber(t *testing.T) {
|
||||
src := `<? 0b0111111111111111111111111111111111111111111111111111111111111111;`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Lnumber{Value: "0b0111111111111111111111111111111111111111111111111111111111111111"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestBinaryDNumber(t *testing.T) {
|
||||
src := `<? 0b1111111111111111111111111111111111111111111111111111111111111111;`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Dnumber{Value: "0b1111111111111111111111111111111111111111111111111111111111111111"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestHLNumber(t *testing.T) {
|
||||
src := `<? 0x007111111111111111;`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Lnumber{Value: "0x007111111111111111"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestHDNumber(t *testing.T) {
|
||||
src := `<? 0x8111111111111111;`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.Dnumber{Value: "0x8111111111111111"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
168
node/scalar/string_test.go
Normal file
168
node/scalar/string_test.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package scalar_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";`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\"test\""},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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";`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\"\\$test\""},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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
|
||||
";`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\"\n\ttest\n\t\""},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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';`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "'$test'"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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
|
||||
';`
|
||||
|
||||
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")
|
||||
|
||||
if diff := pretty.Compare(expected, actual); diff != "" {
|
||||
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlainHeredocScalarString(t *testing.T) {
|
||||
src := `<? <<<CAD
|
||||
hello
|
||||
CAD;
|
||||
`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\thello\n"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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;
|
||||
`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\thello\n"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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;
|
||||
`
|
||||
|
||||
expected := &stmt.StmtList{
|
||||
Stmts: []node.Node{
|
||||
&stmt.Expression{
|
||||
Expr: &scalar.String{Value: "\thello $world\n"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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