-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prove C# support for ExplicitInitialization recipe (#401)
* Improve tests and visitor for ExplicitInitialization recipe * Add C# test for ExplicitInitialization recipe * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Fix license header --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ea74635
commit 428347d
Showing
5 changed files
with
116 additions
and
23 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
75 changes: 75 additions & 0 deletions
75
...st/java/org/openrewrite/staticanalysis/charp/ExplicitInitializationVisitorCsharpTest.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,75 @@ | ||
/* | ||
* Copyright 2024 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.staticanalysis.charp; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.staticanalysis.ExplicitInitialization; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.staticanalysis.charp.JavaToCsharp.toCsRecipe; | ||
|
||
class ExplicitInitializationVisitorCsharpTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(toCsRecipe(new ExplicitInitialization())) | ||
.parser(JavaParser.fromJavaVersion() | ||
.dependsOn( | ||
// C# defines nullable primitive. These are actually in the CLR wrapper objects. | ||
//language=java | ||
""" | ||
class Nullable<T> { | ||
Integer a; | ||
Nullable(Integer a) { | ||
this.a = a; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void removeExplicitInitialization() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
class A { | ||
// C#: int? a | ||
Nullable<Integer> a = null; | ||
// C#: int? a = 0 | ||
Nullable<Integer> a = new Nullable<>(0); | ||
} | ||
""", | ||
""" | ||
class A { | ||
// C#: int? a | ||
Nullable<Integer> a; | ||
// C#: int? a = 0 | ||
Nullable<Integer> a = new Nullable<>(0); | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
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