-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move rewrite-core tests from Kotlin to Java
- Loading branch information
1 parent
c98e8a1
commit dbd7ce8
Showing
60 changed files
with
2,599 additions
and
2,617 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
rewrite-core/src/test/java/org/openrewrite/ApplicabilityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
rewrite-core/src/test/java/org/openrewrite/CursorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
rewrite-core/src/test/java/org/openrewrite/ExecutionContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.