diff --git a/internal/php8/dynamic_class_constant_fetch_test.go b/internal/php8/dynamic_class_constant_fetch_test.go new file mode 100644 index 0000000..651d785 --- /dev/null +++ b/internal/php8/dynamic_class_constant_fetch_test.go @@ -0,0 +1,98 @@ +package php8_test + +import ( + "bytes" + "testing" + + "github.com/rectorphp/php-parser-in-go/internal/php8" + "github.com/rectorphp/php-parser-in-go/pkg/ast" + "github.com/rectorphp/php-parser-in-go/pkg/conf" + "github.com/rectorphp/php-parser-in-go/pkg/errors" + "github.com/rectorphp/php-parser-in-go/pkg/version" + "github.com/rectorphp/php-parser-in-go/pkg/visitor/printer" +) + +// parseDynamicClassConstantFetch parses PHP 8.3 source and returns the root plus +// every parser error it collected. +func parseDynamicClassConstantFetch(source string) (ast.Vertex, []*errors.Error) { + var parserErrors []*errors.Error + config := conf.Config{ + Version: &version.Version{Major: 8, Minor: 3}, + ErrorHandlerFunc: func(parserError *errors.Error) { parserErrors = append(parserErrors, parserError) }, + } + lexer := php8.NewLexer([]byte(source), config) + parser := php8.NewParser(lexer, config) + parser.Parse() + return parser.GetRootNode(), parserErrors +} + +func classConstantFetchOf(test *testing.T, root ast.Vertex) *ast.ExprClassConstFetch { + test.Helper() + returnStmt := root.(*ast.Root).Stmts[0].(*ast.StmtReturn) + fetch, ok := returnStmt.Expr.(*ast.ExprClassConstFetch) + if !ok { + test.Fatalf("expected a class constant fetch, got %T", returnStmt.Expr) + } + return fetch +} + +func TestDynamicClassConstantFetchOnClassName(test *testing.T) { + root, parserErrors := parseDynamicClassConstantFetch(`value; +` + + root, parserErrors := parseDynamicClassConstantFetch(source) + if len(parserErrors) != 0 { + test.Fatalf("unexpected parser errors: %v", parserErrors) + } + + output := bytes.NewBufferString("") + root.Accept(printer.NewPrinter(output)) + if output.String() != source { + test.Errorf("printed output differs from source:\n%s", output.String()) + } +} diff --git a/internal/php8/new_expression_test.go b/internal/php8/new_expression_test.go new file mode 100644 index 0000000..8c39391 --- /dev/null +++ b/internal/php8/new_expression_test.go @@ -0,0 +1,82 @@ +package php8_test + +import ( + "bytes" + "testing" + + "github.com/rectorphp/php-parser-in-go/internal/php8" + "github.com/rectorphp/php-parser-in-go/pkg/ast" + "github.com/rectorphp/php-parser-in-go/pkg/conf" + "github.com/rectorphp/php-parser-in-go/pkg/errors" + "github.com/rectorphp/php-parser-in-go/pkg/version" + "github.com/rectorphp/php-parser-in-go/pkg/visitor/printer" +) + +// parseNewExpression parses PHP 8.0 source and returns the root plus every +// parser error it collected. +func parseNewExpression(source string) (ast.Vertex, []*errors.Error) { + var parserErrors []*errors.Error + config := conf.Config{ + Version: &version.Version{Major: 8, Minor: 0}, + ErrorHandlerFunc: func(parserError *errors.Error) { parserErrors = append(parserErrors, parserError) }, + } + lexer := php8.NewLexer([]byte(source), config) + parser := php8.NewParser(lexer, config) + parser.Parse() + return parser.GetRootNode(), parserErrors +} + +func TestNewWithExpressionClassName(test *testing.T) { + root, parserErrors := parseNewExpression(`newQuery();`) + if len(parserErrors) != 0 { + test.Fatalf("unexpected parser errors: %v", parserErrors) + } +} + +func TestNewWithExpressionClassNamePrintsBackUnchanged(test *testing.T) { + source := `