Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple TemplateProcessorTest from RefasterTemplateProcessor #60

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.java.template.processor.RefasterTemplateProcessor;
import org.openrewrite.java.template.processor.TemplateProcessor;
import org.openrewrite.java.template.processor.TypeAwareProcessor;

import java.io.File;
import java.net.URL;
Expand Down Expand Up @@ -80,10 +80,14 @@ void nestedRecipes(String recipeName) {
.hasSourceEquivalentTo(JavaFileObjects.forResource("refaster/" + recipeName + "Recipes.java"));
}

static Compilation compile(String resourceName) {
private static Compilation compile(String resourceName) {
return compile(resourceName, new RefasterTemplateProcessor());
}

static Compilation compile(String resourceName, TypeAwareProcessor processor) {
// As per https://github.com/google/compile-testing/blob/v0.21.0/src/main/java/com/google/testing/compile/package-info.java#L53-L55
return javac()
.withProcessors(new RefasterTemplateProcessor(), new TemplateProcessor())
.withProcessors(processor)
.withClasspath(Arrays.asList(
fileForClass(BeforeTemplate.class),
fileForClass(AfterTemplate.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.java.template.processor.TemplateProcessor;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static org.openrewrite.java.template.RefasterTemplateProcessorTest.compile;

class TemplateProcessorTest {

@ParameterizedTest
@ValueSource(strings = {
"Unqualified",
Expand All @@ -35,7 +35,7 @@ class TemplateProcessorTest {
})
void qualification(String qualifier) {
// As per https://github.com/google/compile-testing/blob/v0.21.0/src/main/java/com/google/testing/compile/package-info.java#L53-L55
Compilation compilation = compile("template/ShouldAddClasspath.java");
Compilation compilation = compile("template/ShouldAddClasspathRecipes.java", new TemplateProcessor());
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("foo/ShouldAddClasspathRecipes$" + qualifier + "Recipe$1_before")
Expand All @@ -47,7 +47,7 @@ void qualification(String qualifier) {

@Test
void parameterReuse() {
Compilation compilation = compile("template/ParameterReuse.java");
Compilation compilation = compile("template/ParameterReuseRecipe.java", new TemplateProcessor());
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("foo/ParameterReuseRecipe$1_before")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@
*/
package foo;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.openrewrite.java.template.Matches;
import org.openrewrite.java.template.MethodInvocationMatcher;
import org.openrewrite.java.template.NotMatches;
import org.openrewrite.ExecutionContext;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.template.Semantics;

public class ParameterReuse {
@BeforeTemplate
boolean before(String s) {
return s.equals(s);
}

@AfterTemplate
boolean after() {
return true;
}
public class ParameterReuseRecipe {
JavaIsoVisitor visitor = new JavaIsoVisitor<ExecutionContext>() {
JavaTemplate.Builder before = Semantics.expression(this, "before", (String s) -> s.equals(s));
JavaTemplate.Builder after = Semantics.expression(this, "after", (String s) -> true);
Comment on lines +25 to +26
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};
}
78 changes: 0 additions & 78 deletions src/test/resources/template/ShouldAddClasspath.java

This file was deleted.

58 changes: 58 additions & 0 deletions src/test/resources/template/ShouldAddClasspathRecipes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package foo;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.openrewrite.ExecutionContext;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.template.Semantics;

import static java.util.regex.Pattern.DOTALL;
import static org.slf4j.LoggerFactory.getLogger;

public class ShouldAddClasspathRecipes {

class UnqualifiedRecipe {
JavaIsoVisitor visitor = new JavaIsoVisitor<ExecutionContext>() {
JavaTemplate.Builder before = Semantics.statement(this, "before", (String message) -> System.out.println(message));
JavaTemplate.Builder after = Semantics.statement(this, "after", (String message) -> getLogger(message));
};
}

class FullyQualifiedRecipe {
JavaIsoVisitor visitor = new JavaIsoVisitor<ExecutionContext>() {
JavaTemplate.Builder before = Semantics.statement(this, "before", (String message) -> System.out.println(message));
JavaTemplate.Builder after = Semantics.statement(this, "after", (String message) -> org.slf4j.LoggerFactory.getLogger(message));
};
}

class FullyQualifiedFieldRecipe {
JavaIsoVisitor visitor = new JavaIsoVisitor<ExecutionContext>() {
JavaTemplate.Builder before = Semantics.statement(this, "before", (String message) -> java.util.regex.Pattern.compile(message, DOTALL));
JavaTemplate.Builder after = Semantics.statement(this, "after", (String message) -> System.out.println(message));
};
}

class PrimitiveRecipe {
JavaIsoVisitor visitor = new JavaIsoVisitor<ExecutionContext>() {
JavaTemplate.Builder before = Semantics.statement(this, "before", (@org.openrewrite.java.template.Primitive Integer i) -> System.out.println(i));
JavaTemplate.Builder after = Semantics.statement(this, "after", (@org.openrewrite.java.template.Primitive Integer i) -> System.out.print(i));
};
}

}