Skip to content

Commit

Permalink
Move rewrite-core tests from Kotlin to Java
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Dec 26, 2022
1 parent c98e8a1 commit dbd7ce8
Show file tree
Hide file tree
Showing 60 changed files with 2,599 additions and 2,617 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void setup() throws URISyntaxException, IOException {

sourceFiles = new ArrayList<>(1_000);
for (int i = 0; i < 1_000; i++) {
Files.write(test.resolve("Test" + i + ".java"),
("package test; class Test" + i + " {}").getBytes());
Files.writeString(test.resolve("Test" + i + ".java"),
"package test; class Test" + i + " {}");
}
}

Expand Down
2 changes: 0 additions & 2 deletions rewrite-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ dependencies {
implementation("org.yaml:snakeyaml:latest.release")

testImplementation(project(":rewrite-test"))

testRuntimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}

tasks.withType<ShadowJar> {
Expand Down
13 changes: 8 additions & 5 deletions rewrite-core/src/main/java/org/openrewrite/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public boolean causesAnotherCycle() {
* @return This recipe.
*/
public Recipe doNext(Recipe recipe) {
if (recipe == this) {
throw new IllegalArgumentException("Cannot add a recipe to itself.");
}
recipeList.add(recipe);
return this;
}
Expand Down Expand Up @@ -216,7 +219,7 @@ protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
*/
@SuppressWarnings("unused")
public Recipe addApplicableTest(TreeVisitor<?, ExecutionContext> test) {
if(applicableTests == null) {
if (applicableTests == null) {
applicableTests = new ArrayList<>(1);
}
applicableTests.add(test);
Expand Down Expand Up @@ -252,7 +255,7 @@ protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
* @return A tree visitor that performs an applicability test.
*/
public Recipe addSingleSourceApplicableTest(TreeVisitor<?, ExecutionContext> test) {
if(singleSourceApplicableTests == null) {
if (singleSourceApplicableTests == null) {
singleSourceApplicableTests = new ArrayList<>(1);
}
singleSourceApplicableTests.add(test);
Expand All @@ -269,7 +272,7 @@ public List<TreeVisitor<?, ExecutionContext>> getSingleSourceApplicableTests() {
* Note that here, as throughout OpenRewrite, we use referential equality to detect that a change has occured.
* To indicate to rewrite that the recipe has made changes a different instance must be returned than the instance
* passed in as "before".
*
* <p>
* Currently, the list passed in as "before" is not immutable, but you should treat it as such anyway.
*
* @param before The set of source files to operate on.
Expand Down Expand Up @@ -303,7 +306,7 @@ public final RecipeRun run(List<? extends SourceFile> before,
public Validated validate(ExecutionContext ctx) {
Validated validated = validate();

for(Recipe recipe : recipeList) {
for (Recipe recipe : recipeList) {
validated = validated.and(recipe.validate(ctx));
}
return validated;
Expand All @@ -326,7 +329,7 @@ public Validated validate() {
logger.warn("Unable to validate the field [{}] on the class [{}]", field.getName(), this.getClass().getName());
}
}
for(Recipe recipe : recipeList) {
for (Recipe recipe : recipeList) {
validated = validated.and(recipe.validate());
}
return validated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ CategoryTree<G> findOrAddCategory(G group, CategoryDescriptor category) {

void addRecipe(G group, RecipeDescriptor recipe) {
if (!recipe.getName().contains(".")) {
throw new IllegalArgumentException("Expected recipe with name '" + recipe.getName() + "' to have" +
throw new IllegalArgumentException("Expected recipe with name '" + recipe.getName() + "' to have " +
"a package, but it did not.");
}
String category = recipe.getName().substring(0, recipe.getName().lastIndexOf('.'));
Expand Down
118 changes: 118 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/ApplicabilityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2022 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 org.openrewrite;

import org.junit.jupiter.api.Test;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.text.PlainText;
import org.openrewrite.text.PlainTextVisitor;

import static org.openrewrite.test.SourceSpecs.text;

class ApplicabilityTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.validateRecipeSerialization(false);
}

@Test
void not() {
rewriteRun(
spec -> spec.recipe(recipe(Applicability.not(contains("z")))),
text("hello", "goodbye")
);
}

@Test
void notNot() {
rewriteRun(
spec -> spec.recipe(recipe(Applicability.not(contains("h")))),
text("hello")
);
}


@Test
void or() {
rewriteRun(
spec -> spec.recipe(recipe(Applicability.or(contains("h"), contains("z")))),
text("hello", "goodbye")
);
}

@Test
void notOr() {
rewriteRun(
spec -> spec.recipe(recipe(Applicability.or(contains("x"), contains("z")))),
text("hello")
);
}

@Test
void and() {
rewriteRun(
spec -> spec.recipe(recipe(Applicability.and(contains("h"), contains("ello")))),
text("hello", "goodbye")
);
}

@Test
void notAnd() {
rewriteRun(
spec -> spec.recipe(recipe(Applicability.and(contains("h"), contains("z")))),
text("hello")
);
}

Recipe recipe(TreeVisitor<?, ExecutionContext> applicability) {
return new Recipe() {
@Override
public String getDisplayName() {
return "Say goodbye";
}

@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return applicability;
}

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
return text.withText("goodbye");
}
};
}
};
}

PlainTextVisitor<ExecutionContext> contains(String s) {
return new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
if (text.getText().contains(s)) {
return SearchResult.found(text);
}
return text;
}
};
}
}
67 changes: 67 additions & 0 deletions rewrite-core/src/test/java/org/openrewrite/CursorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2020 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 org.openrewrite;

import org.junit.jupiter.api.Test;
import org.openrewrite.marker.Markers;
import org.openrewrite.text.PlainText;

import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.Tree.randomId;

class CursorTest {
@Test
void peekMessages() {
var t = new PlainText(randomId(), Paths.get("test.txt"), Markers.EMPTY, null, false, null, null, "test");
var cursor = new Cursor(null, t);

cursor.putMessage("key", 1);
assertThat(cursor.<Integer>getNearestMessage("key")).isEqualTo(1);

var child = new Cursor(cursor, t);
assertThat(child.<Integer>getNearestMessage("key")).isEqualTo(1);
}

@Test
void pollMessages() {
var t = new PlainText(randomId(), Paths.get("test.txt"), Markers.EMPTY, null, false, null, null, "test");
var cursor = new Cursor(null, t);

cursor.putMessage("key", 1);
assertThat(cursor.<Integer>pollNearestMessage("key")).isEqualTo(1);
assertThat(cursor.<Integer>pollNearestMessage("key")).isNull();

cursor.putMessage("key", 1);
var child = new Cursor(cursor, t);
assertThat(child.<Integer>getNearestMessage("key")).isEqualTo(1);
}

@Test
void pathPredicates() {
var t = new PlainText(randomId(), Paths.get("test.txt"), Markers.EMPTY, null, false, null, null, "test");
var cursor = new Cursor(new Cursor(new Cursor(null, 1), t), 2);
assertThat(cursor.getPath(v -> v instanceof PlainText).next()).isSameAs(t);
}

@Test
void pathAsStreamPredicates() {
var t = new PlainText(randomId(), Paths.get("test.txt"), Markers.EMPTY, null, false, null, null, "test");
var cursor = new Cursor(new Cursor(new Cursor(null, 1), t), 2);
assertThat(cursor.getPathAsStream(v -> v instanceof PlainText).toList()).containsExactly(t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2020 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 org.openrewrite;

import org.junit.jupiter.api.Test;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.text.PlainText;
import org.openrewrite.text.PlainTextVisitor;

import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.test.RewriteTest.toRecipe;
import static org.openrewrite.test.SourceSpecs.text;

class ExecutionContextTest implements RewriteTest {

@Test
void anotherCycleIfNewMessagesInExecutionContext() {
var cycles = new AtomicInteger();
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new PlainTextVisitor<>() {
@Override
public @Nullable PlainText visit(@Nullable Tree tree, ExecutionContext p) {
if(p.pollMessage("test") == null) {
p.putMessage("test", "test");
}
cycles.incrementAndGet();
return super.visit(tree, p);
}
}).withCausesAnotherCycle(true)),
text("hello world")
);
assertThat(cycles.get()).isEqualTo(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
import java.util.HashMap;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class RecipeBasicsTest {

@Test
void recipeDoNextWithItself() {
Recipe r = Recipe.noop();
assertThrows(IllegalArgumentException.class, () -> r.doNext(r));
}

@Test
void cloneRecipe() throws JsonMappingException {
ChangeText ct = new ChangeText("hi");
Expand Down
Loading

0 comments on commit dbd7ce8

Please sign in to comment.