test

package
v0.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpectMethodType

func ExpectMethodType(t *testing.T, root java.Tree, name string, expectedDeclaringFQN string)

ExpectMethodType walks the tree rooted at root and asserts that the first MethodInvocation or MethodDeclaration whose name matches `name` carries a non-nil JavaTypeMethod whose DeclaringType.FullyQualifiedName equals expectedDeclaringFQN.

For invocations across packages, expectedDeclaringFQN is the import path of the owning package (e.g. "fmt" for fmt.Println). For methods declared in the file under test, it is the package's full path (e.g. "main.Point" for a method on Point in package main).

func ExpectPrimitiveType

func ExpectPrimitiveType(t *testing.T, root java.Tree, name string, expectedKeyword string)

ExpectPrimitiveType asserts that the first identifier named `name` has a JavaTypePrimitive whose Keyword matches expectedKeyword (e.g. "int", "String", "bool"). Mirrors ExpectType for primitive type attribution.

func ExpectType

func ExpectType(t *testing.T, root java.Tree, name string, expectedFQN string)

ExpectType walks the tree rooted at root and asserts that the first identifier whose Name == name carries a fully-qualified type whose FQN matches expectedFQN. Use this for class/struct/parameterized types; for primitives use ExpectPrimitiveType.

Fails the test if no matching identifier is found, if its Type is nil, or if the type does not implement java.FullyQualified.

func FindGoResolutionResult

func FindGoResolutionResult(spec SourceSpec) *golang.GoResolutionResult

FindGoResolutionResult walks a SourceSpec's markers for the parsed go.mod marker. Returns nil if not present (e.g. on a Golang(...) source).

func TrimIndent

func TrimIndent(s string) string

TrimIndent removes the common leading whitespace from all non-empty lines, and strips a single leading and trailing blank line (artifacts of backtick string literals that start/end on their own line).

func ValidateSpaces

func ValidateSpaces(root java.Tree) []string

ValidateSpaces walks the tree and returns errors for any Space that contains non-whitespace content (which would indicate a parser bug). Thin shim over golang.WhitespaceValidationService — recipes call the service directly; tests use this name for source-level continuity.

Types

type JavaRecipeConfig

type JavaRecipeConfig struct {
	RecipeName string
	Options    map[string]any
}

JavaRecipeConfig holds config for a Java-delegated recipe test.

type JavaRpcClient

type JavaRpcClient struct {
	// contains filtered or unexported fields
}

JavaRpcClient communicates with a Java RPC server process (org.openrewrite.maven.rpc.JavaRewriteRpc) over stdin/stdout using Content-Length framed JSON-RPC 2.0.

func StartJavaRpcClient

func StartJavaRpcClient(t *testing.T, recipes []RecipeEntry) *JavaRpcClient

StartJavaRpcClient spawns a Java RPC server and returns a client. The classpath is read from test-classpath.txt (generated by Gradle). The marketplace CSV is written to a temp file.

func (*JavaRpcClient) SendRequest

func (c *JavaRpcClient) SendRequest(method string, params any) (json.RawMessage, error)

SendRequest sends a JSON-RPC request and waits for the response.

func (*JavaRpcClient) Shutdown

func (c *JavaRpcClient) Shutdown()

Shutdown gracefully stops the Java RPC server.

type RecipeEntry

type RecipeEntry struct {
	Name        string
	PackageName string
	Version     string
}

RecipeEntry describes a recipe for the marketplace CSV.

func DefaultJavaRecipes

func DefaultJavaRecipes() []RecipeEntry

DefaultJavaRecipes returns the common Java recipes used for testing.

type RecipeSpec

type RecipeSpec struct {
	CheckParsePrintIdempotence bool
	Recipe                     recipe.Recipe
	JavaRecipe                 *JavaRecipeConfig     // when set, delegates to Java RPC
	JavaRpcClient              *JavaRpcClient        // injected Java RPC client
	MarkerPrinter              printer.MarkerPrinter // printer for cross-cutting markers in recipe output; defaults to printer.DefaultMarkerPrinter
}

RecipeSpec configures a test run.

func NewRecipeSpec

func NewRecipeSpec() *RecipeSpec

NewRecipeSpec creates a new RecipeSpec with default settings.

func (*RecipeSpec) RewriteRun

func (spec *RecipeSpec) RewriteRun(t *testing.T, sources ...Sources)

RewriteRun parses each source, checks parse-print idempotence, attaches any markers contributed by project wrappers (GoProject), and (if configured) applies a recipe and checks the result. Accepts both bare SourceSpec values and project wrappers like GoProject — they both implement Sources.

func (*RecipeSpec) WithJavaRecipe

func (spec *RecipeSpec) WithJavaRecipe(recipeName string, options map[string]any) *RecipeSpec

WithJavaRecipe configures the test to delegate to a Java-defined recipe via RPC. Requires a JavaRpcClient to be set on the spec.

func (*RecipeSpec) WithJavaRpcClient

func (spec *RecipeSpec) WithJavaRpcClient(client *JavaRpcClient) *RecipeSpec

WithJavaRpcClient sets the Java RPC client for recipe delegation.

func (*RecipeSpec) WithMarkerPrinter added in v0.0.2

func (spec *RecipeSpec) WithMarkerPrinter(mp printer.MarkerPrinter) *RecipeSpec

WithMarkerPrinter overrides the MarkerPrinter used when printing recipe output. Mirrors RewriteTest's spec.markerPrinter(...) on the Java side. When unset, RewriteRun uses printer.DefaultMarkerPrinter so SearchResult/Markup markers surface as /*~~>*/ comments in the expected "after" source — matching Java and TypeScript test conventions.

func (*RecipeSpec) WithRecipe

func (spec *RecipeSpec) WithRecipe(r recipe.Recipe) *RecipeSpec

WithRecipe sets the recipe to apply during the test run.

type SourceSpec

type SourceSpec struct {
	Before      string
	After       *string // nil means no change expected (parse-print idempotence only)
	Path        string
	Markers     []java.Marker                                  // markers attached to the parsed source after parse
	AfterRecipe func(t *testing.T, cu *golang.CompilationUnit) // optional post-parse assertion callback (.go files only)
}

SourceSpec describes a Go source file for testing.

func GoMod

func GoMod(before string, after ...string) SourceSpec

GoMod creates a SourceSpec for go.mod content. The content is dedented the same way Golang(...) is and parsed (via parser.ParseGoMod) at construction time so the resulting golang.GoResolutionResult marker is already attached to spec.Markers — recipes / tests can read module path, requires, replaces, etc. without re-parsing.

If the content fails to parse the spec is returned with no GoResolutionResult marker; the test still round-trips the content verbatim, mirroring the Java goMod test helper's behavior on bad input.

When a sibling GoSum(...) exists in the same project, its parsed ResolvedDependencies are merged into the GoResolutionResult marker at project-expansion time (see project.Expand).

func GoSum

func GoSum(before string, after ...string) SourceSpec

GoSum creates a SourceSpec for go.sum content. The harness round-trips the content verbatim (no recipe processing today) and, when a sibling GoMod(...) is present in the same GoProject, merges the parsed ResolvedDependencies into the GoMod's GoResolutionResult marker.

func Golang

func Golang(before string, after ...string) SourceSpec

Golang creates a SourceSpec for Go source code. The source string is automatically dedented: the common leading whitespace across all non-empty lines is stripped, and a leading/trailing blank line (from the backtick) is removed. This lets test strings be indented to match their surrounding code:

Golang(`
	package main

	func hello() {
		return "hi"
	}
`)

func GolangRaw

func GolangRaw(before string, after ...string) SourceSpec

GolangRaw creates a SourceSpec from raw Go source (no indent trimming).

func (SourceSpec) Expand

func (s SourceSpec) Expand() []SourceSpec

Expand makes a SourceSpec usable wherever a Sources is expected.

func (SourceSpec) WithAfterRecipe

func (s SourceSpec) WithAfterRecipe(fn func(t *testing.T, cu *golang.CompilationUnit)) SourceSpec

WithAfterRecipe returns a copy of s with the post-parse callback set. The callback fires after parsing (with type attribution wired) and before recipe application; recipes can read s.Markers off the cu and assert on resolved types.

func (SourceSpec) WithPath

func (s SourceSpec) WithPath(p string) SourceSpec

WithPath returns a copy of s with Path set. Use this in multi-package projects so the test harness can locate each file's package directory.

type Sources

type Sources interface {
	Expand() []SourceSpec
}

Sources is the unit RewriteRun consumes. Single SourceSpecs and project wrappers (GoProject, etc.) both satisfy it so the same harness call can mix flat .go files with multi-file projects.

func GoProject

func GoProject(name string, sources ...Sources) Sources

GoProject groups a go.mod and one or more .go SourceSpecs as siblings of a single project. Every child receives a golang.GoProject marker. Mirrors the Java-side Assertions.goProject(name, sources...).

Example:

spec.RewriteRun(t,
    test.GoProject("foo",
        test.GoMod("module example.com/foo\ngo 1.22\n"),
        test.Golang("package main\nfunc main(){}\n"),
    ),
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL