|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate.trait; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.openrewrite.DocumentExample; |
| 20 | +import org.openrewrite.InMemoryExecutionContext; |
| 21 | +import org.openrewrite.Recipe; |
| 22 | +import org.openrewrite.java.tree.J; |
| 23 | +import org.openrewrite.marker.SearchResult; |
| 24 | +import org.openrewrite.test.RecipeSpec; |
| 25 | +import org.openrewrite.test.RewriteTest; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 29 | +import static org.openrewrite.java.Assertions.java; |
| 30 | + |
| 31 | +class ScopedVariableTest implements RewriteTest { |
| 32 | + |
| 33 | + @Override |
| 34 | + public void defaults(RecipeSpec spec) { |
| 35 | + spec.recipe(RewriteTest.toRecipe(() -> |
| 36 | + new ScopedVariable.Matcher().asVisitor(a -> SearchResult.found(a.getTree(), |
| 37 | + String.format("Identified variable %s within scope %s", a.getIdentifier(), a.getScope().getValue().getClass().getSimpleName()) |
| 38 | + ) |
| 39 | + ) |
| 40 | + )); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + @DocumentExample |
| 45 | + void markAllFieldsInClass() { |
| 46 | + rewriteRun( |
| 47 | + //language=java |
| 48 | + java( |
| 49 | + """ |
| 50 | + class Test { |
| 51 | + private String field; |
| 52 | +
|
| 53 | + void doSome() { |
| 54 | + String anotherField; |
| 55 | + } |
| 56 | + } |
| 57 | + """, |
| 58 | + """ |
| 59 | + class Test { |
| 60 | + private String /*~~(Identified variable field with scope Block)~~>*/field; |
| 61 | +
|
| 62 | + void doSome() { |
| 63 | + String /*~~(Identified variable anotherField with scope Block)~~>*/anotherField; |
| 64 | + } |
| 65 | + } |
| 66 | + """ |
| 67 | + ) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void markAllFieldsInMethod() { |
| 73 | + rewriteRun( |
| 74 | + spec -> spec.recipe(Recipe.noop()), |
| 75 | + //language=java |
| 76 | + java( |
| 77 | + """ |
| 78 | + class Test { |
| 79 | + private String field; |
| 80 | +
|
| 81 | + void doSome() { |
| 82 | + String anotherField; |
| 83 | + } |
| 84 | + } |
| 85 | + """, |
| 86 | + spec -> spec.beforeRecipe((source) -> { |
| 87 | + J.MethodDeclaration md = (J.MethodDeclaration) source.getClasses().get(0).getBody().getStatements().get(1); |
| 88 | + new ScopedVariable.Matcher().asVisitor(var -> { |
| 89 | + assertThat(((J.Block)var.getScope().getValue())).isEqualTo(md.getBody()); |
| 90 | + return var.getTree(); |
| 91 | + }).visit(md, new InMemoryExecutionContext()); |
| 92 | + }) |
| 93 | + ) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments