From 9d7e978baf5fe055d13267b2490990a80d26111d Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Thu, 8 Sep 2016 15:43:30 +0300 Subject: [PATCH] Java inspection: Added lambda expression tests for "Move return to computation" inspection (IDEA-121153) --- .../afterIfInLambda.java | 17 +++++++ .../afterWhileInLambda.java | 24 ++++++++++ .../beforeIfInLambda.java | 17 +++++++ .../beforeWhileInLambda.java | 26 ++++++++++ ...eturnSeparatedFromComputationFix8Test.java | 47 +++++++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/afterIfInLambda.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/afterWhileInLambda.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/beforeIfInLambda.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/beforeWhileInLambda.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ReturnSeparatedFromComputationFix8Test.java diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/afterIfInLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/afterIfInLambda.java new file mode 100644 index 000000000000..a3000a616d11 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/afterIfInLambda.java @@ -0,0 +1,17 @@ +// "Move 'return' closer to computation of the value of 'n'" "true" +class T { + interface I { + int call(); + } + void f(boolean b) { + g(() -> { + int n = -1; + if (b) return 1; + return n; + }); + } + + void g(I i) { + i.call(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/afterWhileInLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/afterWhileInLambda.java new file mode 100644 index 000000000000..9de9ab16d54b --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/afterWhileInLambda.java @@ -0,0 +1,24 @@ +// "Move 'return' closer to computation of the value of 'n'" "true" +class T { + interface I { + int call(); + } + void f(boolean b) { + g(() -> { + int n = -1; + while (true) { + if (h()) { + return 1; + } + } + }); + } + + void g(I i) { + i.call(); + } + + boolean h() { + return true; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/beforeIfInLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/beforeIfInLambda.java new file mode 100644 index 000000000000..e78ffa19f3a6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/beforeIfInLambda.java @@ -0,0 +1,17 @@ +// "Move 'return' closer to computation of the value of 'n'" "true" +class T { + interface I { + int call(); + } + void f(boolean b) { + g(() -> { + int n = -1; + if (b) n = 1; + return n; + }); + } + + void g(I i) { + i.call(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/beforeWhileInLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/beforeWhileInLambda.java new file mode 100644 index 000000000000..279ff1b87762 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8/beforeWhileInLambda.java @@ -0,0 +1,26 @@ +// "Move 'return' closer to computation of the value of 'n'" "true" +class T { + interface I { + int call(); + } + void f(boolean b) { + g(() -> { + int n = -1; + while (true) { + if (h()) { + n = 1; + break; + } + } + return n; + }); + } + + void g(I i) { + i.call(); + } + + boolean h() { + return true; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ReturnSeparatedFromComputationFix8Test.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ReturnSeparatedFromComputationFix8Test.java new file mode 100644 index 000000000000..b18edfdc9043 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ReturnSeparatedFromComputationFix8Test.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 com.intellij.codeInsight.daemon.quickFix; + +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.intermediaryVariable.ReturnSeparatedFromComputationInspection; +import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.testFramework.IdeaTestUtil; +import org.jetbrains.annotations.NotNull; + +/** + * @author Pavel.Dolgov + */ +public class ReturnSeparatedFromComputationFix8Test extends LightQuickFixParameterizedTestCase { + @NotNull + @Override + protected LocalInspectionTool[] configureLocalInspectionTools() { + return new LocalInspectionTool[]{new ReturnSeparatedFromComputationInspection()}; + } + + public void test() throws Exception { + doAllTests(); + } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/returnSeparatedFromComputation8"; + } + + @Override + protected Sdk getProjectJDK() { + return IdeaTestUtil.getMockJdk18(); + } +}