From 596e92645dbb9dcc32ab89b52676efb8a73556c1 Mon Sep 17 00:00:00 2001 From: Eve Matthaey Date: Fri, 8 Mar 2024 11:59:11 -0600 Subject: [PATCH] [kotlin] J2K: Add more test cases for ReplaceGuardClauseWithFunctionCallInspection postprocessing step KTIJ-28575 GitOrigin-RevId: ec918e359953249a6f248d21a4cba38b90104cdd --- .../newJ2k/postProcessing/GuardClause.java | 65 ++++++++++++++++++- .../newJ2k/postProcessing/GuardClause.kt | 51 ++++++++++++++- 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/plugins/kotlin/j2k/shared/tests/testData/newJ2k/postProcessing/GuardClause.java b/plugins/kotlin/j2k/shared/tests/testData/newJ2k/postProcessing/GuardClause.java index eb40cd30cb5a..515a725a9bbb 100644 --- a/plugins/kotlin/j2k/shared/tests/testData/newJ2k/postProcessing/GuardClause.java +++ b/plugins/kotlin/j2k/shared/tests/testData/newJ2k/postProcessing/GuardClause.java @@ -1,8 +1,69 @@ // IGNORE_K2 public class Test { - void test(String s) { - if (s == null) { + void testRequire(String s1, boolean b1, boolean b2) { + if (s1 == null) { throw new IllegalArgumentException("s should not be null"); } + + if (!b1) { + throw new IllegalArgumentException(); + } else { + System.out.println("never mind"); + } + // comment above b2 + if (b2) { + throw new IllegalArgumentException(); + } + + if (!b1 && b2) throw new IllegalArgumentException(); + else { + System.out.println(1); + System.out.println(2); + } + + if (s1.length() < 3) { + throw new IllegalArgumentException(); + } else if (s1.length() == 4) { + System.out.println(1); + } else { + System.out.println(2); + } + } + + void testCheck(boolean b, String notNullString) { + if (b) throw new IllegalStateException(); + + // comment above notNullString + if (notNullString == null) { + throw new IllegalStateException() + } + } + + void testDoubles(double x, double y) { + if (!(x < y)) { + throw new IllegalStateException() + } + if (y < 2*x) { + throw new IllegalStateException() + } + } + + void doNotTouch(boolean b, String s1) { + try { + System.out.println("hello!"); + } catch (Exception e) { + if (e instanceof RuntimeException) { + throw new IllegalStateException(e); + } + } + + if (b) { + throw new IndexOutOfBoundsException(); + } + + if (s1.length() < 5) { + System.out.println("Some other side effect"); + throw new IllegalStateException("oops"); + } } } \ No newline at end of file diff --git a/plugins/kotlin/j2k/shared/tests/testData/newJ2k/postProcessing/GuardClause.kt b/plugins/kotlin/j2k/shared/tests/testData/newJ2k/postProcessing/GuardClause.kt index 17b53481a433..f7d77e8cc797 100644 --- a/plugins/kotlin/j2k/shared/tests/testData/newJ2k/postProcessing/GuardClause.kt +++ b/plugins/kotlin/j2k/shared/tests/testData/newJ2k/postProcessing/GuardClause.kt @@ -1,5 +1,52 @@ class Test { - fun test(s: String?) { - requireNotNull(s) { "s should not be null" } + fun testRequire(s1: String?, b1: Boolean, b2: Boolean) { + requireNotNull(s1) { "s should not be null" } + + require(b1) + println("never mind") + // comment above b2 + require(!b2) + + require(!(!b1 && b2)) + println(1) + println(2) + + require(s1.length >= 3) + if (s1.length == 4) { + println(1) + } else { + println(2) + } + } + + fun testCheck(b: Boolean, notNullString: String?) { + check(!b) + + // comment above notNullString + checkNotNull(notNullString) + } + + fun testDoubles(x: Double, y: Double) { + check((x < y)) + check(!(y < 2 * x)) + } + + fun doNotTouch(b: Boolean, s1: String) { + try { + println("hello!") + } catch (e: Exception) { + if (e is RuntimeException) { + throw IllegalStateException(e) + } + } + + if (b) { + throw IndexOutOfBoundsException() + } + + if (s1.length < 5) { + println("Some other side effect") + throw IllegalStateException("oops") + } } }