|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license |
| 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 | +using OpenRewrite.CSharp; |
| 17 | +using OpenRewrite.Java; |
| 18 | +using OpenRewrite.Test; |
| 19 | +using ExecutionContext = OpenRewrite.Core.ExecutionContext; |
| 20 | +using Recipe = OpenRewrite.Core.Recipe; |
| 21 | + |
| 22 | +namespace OpenRewrite.Tests.Core; |
| 23 | + |
| 24 | +public class PreconditionsTest : RewriteTest |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// When the precondition Recipe matches, the inner visitor should run. |
| 28 | + /// </summary> |
| 29 | + [Fact] |
| 30 | + public void RecipePreconditionMatches() |
| 31 | + { |
| 32 | + RewriteRun( |
| 33 | + spec => spec.SetRecipe(new PreconditionedRenameRecipe |
| 34 | + { |
| 35 | + PreconditionClassName = "Foo", |
| 36 | + From = "Foo", |
| 37 | + To = "Bar" |
| 38 | + }), |
| 39 | + CSharp( |
| 40 | + "class Foo { }", |
| 41 | + "class Bar { }" |
| 42 | + ) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// When the precondition Recipe does NOT match, the inner visitor should not run. |
| 48 | + /// </summary> |
| 49 | + [Fact] |
| 50 | + public void RecipePreconditionDoesNotMatch() |
| 51 | + { |
| 52 | + RewriteRun( |
| 53 | + spec => spec.SetRecipe(new PreconditionedRenameRecipe |
| 54 | + { |
| 55 | + PreconditionClassName = "Missing", |
| 56 | + From = "Foo", |
| 57 | + To = "Bar" |
| 58 | + }), |
| 59 | + CSharp("class Foo { }") |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// <summary> |
| 65 | +/// A recipe that renames a class, but only if a precondition recipe (FindClass) matches. |
| 66 | +/// Uses Preconditions.Check(Recipe, visitor) overload. |
| 67 | +/// </summary> |
| 68 | +file class PreconditionedRenameRecipe : OpenRewrite.Core.Recipe |
| 69 | +{ |
| 70 | + public required string PreconditionClassName { get; init; } |
| 71 | + public required string From { get; init; } |
| 72 | + public required string To { get; init; } |
| 73 | + |
| 74 | + public override string DisplayName => "Preconditioned rename class"; |
| 75 | + public override string Description => "Renames a class only if the precondition recipe matches."; |
| 76 | + |
| 77 | + public override OpenRewrite.Core.ITreeVisitor<ExecutionContext> GetVisitor() |
| 78 | + { |
| 79 | + return OpenRewrite.Core.Preconditions.Check( |
| 80 | + new FindClassRecipe { ClassName = PreconditionClassName }, |
| 81 | + new RenameClassVisitor(From, To)); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// <summary> |
| 86 | +/// A simple search recipe that marks files containing a specific class name. |
| 87 | +/// Used as a precondition. |
| 88 | +/// </summary> |
| 89 | +file class FindClassRecipe : OpenRewrite.Core.Recipe |
| 90 | +{ |
| 91 | + public required string ClassName { get; init; } |
| 92 | + |
| 93 | + public override string DisplayName => "Find class"; |
| 94 | + public override string Description => "Finds files containing a specific class."; |
| 95 | + |
| 96 | + public override OpenRewrite.Core.ITreeVisitor<ExecutionContext> GetVisitor() => new FindClassVisitor(ClassName); |
| 97 | +} |
| 98 | + |
| 99 | +file class FindClassVisitor(string className) : CSharpVisitor<ExecutionContext> |
| 100 | +{ |
| 101 | + public override J VisitClassDeclaration(ClassDeclaration cd, ExecutionContext ctx) |
| 102 | + { |
| 103 | + cd = (ClassDeclaration)base.VisitClassDeclaration(cd, ctx); |
| 104 | + if (cd.Name.SimpleName == className) |
| 105 | + { |
| 106 | + return OpenRewrite.Core.SearchResult.Found(cd); |
| 107 | + } |
| 108 | + return cd; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +file class RenameClassVisitor(string from, string to) : CSharpVisitor<ExecutionContext> |
| 113 | +{ |
| 114 | + public override J VisitClassDeclaration(ClassDeclaration cd, ExecutionContext ctx) |
| 115 | + { |
| 116 | + cd = (ClassDeclaration)base.VisitClassDeclaration(cd, ctx); |
| 117 | + if (cd.Name.SimpleName == from) |
| 118 | + { |
| 119 | + return cd.WithName(cd.Name.WithSimpleName(to)); |
| 120 | + } |
| 121 | + return cd; |
| 122 | + } |
| 123 | +} |
0 commit comments