openrewrite / rewrite-templating

Automated templating using code snippets.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow tests to be written with Java 17 or 21 for access to text blocks

timtebeek opened this issue · comments

What problem are you trying to solve?

Right now our tests use resource files for in and outputs, but these mean there's a disconnect between a test method and what it actually verifies.

Describe the solution you'd like

With text blocks and JavaFileObjects.forSourceString we can instead adopt a test style similar to what we use elsewhere in OpenRewrite. That should help familiarity, and seeing at a glance what a test verifies.

static Compilation compileSource(String fqn, @Language("java") String source, TypeAwareProcessor processor) {
    return compileResource(JavaFileObjects.forSourceString(fqn, source), processor);
}

Additional context

The compileSource method above as already been added in

My quick attempts so far have failed to produce a working setup with Gradle; something to do with toolchains that I think I'm close to, but some help would be appreciated to get this in.

Through #57 there's now already two methods that can take a String input.

@SuppressWarnings("unused") // use when text blocks are available
static Compilation compileSource(String fqn, @Language("java") String source) {
return compile(JavaFileObjects.forSourceString(fqn, source), new RefasterTemplateProcessor());
}
static Compilation compileSource(String fqn, @Language("java") String source, TypeAwareProcessor processor) {
return compile(JavaFileObjects.forSourceString(fqn, source), processor);
}