From 09723b9991b58996de82d683a6234e561fd5102e Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 11 Jun 2015 13:38:25 +0300 Subject: [PATCH] Converted DirectiveBasedActionUtils to Kotlin --- .../idea/test/DirectiveBasedActionUtils.java | 109 ------------------ .../idea/test/DirectiveBasedActionUtils.kt | 92 +++++++++++++++ .../intentions/AbstractIntentionTest.java | 2 +- .../AbstractQuickFixMultiFileTest.java | 4 +- .../idea/quickfix/AbstractQuickFixTest.java | 4 +- .../JetChangeSignatureTest.java | 2 +- 6 files changed, 98 insertions(+), 115 deletions(-) delete mode 100644 idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/DirectiveBasedActionUtils.java create mode 100644 idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/DirectiveBasedActionUtils.kt diff --git a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/DirectiveBasedActionUtils.java b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/DirectiveBasedActionUtils.java deleted file mode 100644 index 5782a7f13bf..00000000000 --- a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/DirectiveBasedActionUtils.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2010-2015 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 org.jetbrains.kotlin.idea.test; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.collect.Collections2; -import com.google.common.collect.Lists; -import com.google.common.collect.Ordering; -import com.intellij.codeInsight.intention.IntentionAction; -import com.intellij.testFramework.UsefulTestCase; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.diagnostics.Diagnostic; -import org.jetbrains.kotlin.diagnostics.Severity; -import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage; -import org.jetbrains.kotlin.idea.highlighter.IdeErrorMessages; -import org.jetbrains.kotlin.psi.JetFile; -import org.jetbrains.kotlin.test.InTextDirectivesUtils; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -public class DirectiveBasedActionUtils { - private DirectiveBasedActionUtils() { - } - - public static void checkForUnexpectedErrors(JetFile file) { - if (!InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// DISABLE-ERRORS").isEmpty()) { - return; - } - - Collection diagnostics = ResolvePackage.analyzeFully(file).getDiagnostics().all(); - Collection errorDiagnostics = Collections2.filter(diagnostics, new Predicate() { - @Override - public boolean apply(@Nullable Diagnostic diagnostic) { - assert (diagnostic != null); - return diagnostic.getSeverity() == Severity.ERROR; - } - }); - Collection actualErrorStrings = Collections2.transform(errorDiagnostics, new Function() { - @Override - public String apply(@Nullable Diagnostic diagnostic) { - assert (diagnostic != null); - return IdeErrorMessages.render(diagnostic); - } - }); - - List expectedErrorStrings = InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// ERROR:"); - Collections.sort(expectedErrorStrings); - - UsefulTestCase.assertOrderedEquals( - "All actual errors should be mentioned in test data with // ERROR: directive. But no unnecessary errors should be me mentioned", - Ordering.natural().sortedCopy(actualErrorStrings), expectedErrorStrings); - } - - public static void checkAvailableActionsAreExpected(JetFile file, Collection availableActions) { - List validActions = Ordering.natural().sortedCopy( - Lists.newArrayList(InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// ACTION:"))); - - Collection actualActions = Ordering.natural().sortedCopy( - Lists.newArrayList(Collections2.transform(availableActions, new Function() { - @Override - public String apply(@Nullable IntentionAction input) { - assert input != null; - return input.getText(); - } - }))); - - UsefulTestCase.assertOrderedEquals("Some unexpected actions available at current position. Use // ACTION: directive", - filterOutIrrelevantActions(actualActions), filterOutIrrelevantActions(validActions)); - } - - @NotNull - //TODO: hack, implemented because irrelevant actions behave in different ways on build server and locally - // this behaviour should be investigated and hack can be removed - private static Collection filterOutIrrelevantActions(@NotNull Collection actions) { - return Collections2.filter(actions, new Predicate() { - @Override - public boolean apply(String input) { - for (String prefix : IRRELEVANT_ACTION_PREFIXES) { - if (input.startsWith(prefix) || input.isEmpty()) { - return false; - } - } - return true; - } - }); - } - - private static final Collection IRRELEVANT_ACTION_PREFIXES = - Arrays.asList("Disable ", "Edit intention settings", "Edit inspection profile setting", "Inject language or reference", "Suppress "); -} diff --git a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/DirectiveBasedActionUtils.kt b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/DirectiveBasedActionUtils.kt new file mode 100644 index 00000000000..aab61c9e663 --- /dev/null +++ b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/DirectiveBasedActionUtils.kt @@ -0,0 +1,92 @@ +/* + * Copyright 2010-2015 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 org.jetbrains.kotlin.idea.test + +import com.google.common.base.Function +import com.google.common.base.Predicate +import com.google.common.collect.Collections2 +import com.google.common.collect.Lists +import com.google.common.collect.Ordering +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.testFramework.UsefulTestCase +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.diagnostics.Severity +import org.jetbrains.kotlin.idea.caches.resolve.* +import org.jetbrains.kotlin.idea.highlighter.IdeErrorMessages +import org.jetbrains.kotlin.psi.JetFile +import org.jetbrains.kotlin.test.InTextDirectivesUtils + +import java.util.Arrays +import java.util.Collections + +public object DirectiveBasedActionUtils { + + public fun checkForUnexpectedErrors(file: JetFile) { + if (!InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// DISABLE-ERRORS").isEmpty()) { + return + } + + val diagnostics = file.analyzeFully().getDiagnostics().all() + val errorDiagnostics = Collections2.filter(diagnostics, object : Predicate { + override fun apply(diagnostic: Diagnostic?): Boolean { + assert((diagnostic != null)) + return diagnostic!!.getSeverity() == Severity.ERROR + } + }) + val actualErrorStrings = Collections2.transform(errorDiagnostics, object : Function { + override fun apply(diagnostic: Diagnostic?): String? { + assert((diagnostic != null)) + return IdeErrorMessages.render(diagnostic) + } + }) + + val expectedErrorStrings = InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// ERROR:") + Collections.sort(expectedErrorStrings) + + UsefulTestCase.assertOrderedEquals("All actual errors should be mentioned in test data with // ERROR: directive. But no unnecessary errors should be me mentioned", Ordering.natural().sortedCopy(actualErrorStrings), expectedErrorStrings) + } + + public fun checkAvailableActionsAreExpected(file: JetFile, availableActions: Collection) { + val validActions = Ordering.natural().sortedCopy(Lists.newArrayList(InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// ACTION:"))) + + val actualActions = Ordering.natural().sortedCopy(Lists.newArrayList(Collections2.transform(availableActions, object : Function { + override fun apply(input: IntentionAction?): String? { + assert(input != null) + return input!!.getText() + } + }))) + + UsefulTestCase.assertOrderedEquals("Some unexpected actions available at current position. Use // ACTION: directive", filterOutIrrelevantActions(actualActions), filterOutIrrelevantActions(validActions)) + } + + private //TODO: hack, implemented because irrelevant actions behave in different ways on build server and locally + // this behaviour should be investigated and hack can be removed + fun filterOutIrrelevantActions(actions: Collection): Collection { + return Collections2.filter(actions, object : Predicate { + override fun apply(input: String?): Boolean { + for (prefix in IRRELEVANT_ACTION_PREFIXES) { + if (input!!.startsWith(prefix)) { + return false + } + } + return true + } + }) + } + + private val IRRELEVANT_ACTION_PREFIXES = Arrays.asList("Disable ", "Edit intention settings", "Edit inspection profile setting", "Inject language or reference") +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractIntentionTest.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractIntentionTest.java index b4e7cbe63b7..194d964c8a1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractIntentionTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/AbstractIntentionTest.java @@ -120,7 +120,7 @@ public abstract class AbstractIntentionTest extends KotlinCodeInsightTestCase { ConfigLibraryUtil.configureKotlinRuntimeAndSdk(getModule(), PluginTestCaseBase.mockJdk()); } - DirectiveBasedActionUtils.checkForUnexpectedErrors((JetFile) getFile()); + DirectiveBasedActionUtils.INSTANCE$.checkForUnexpectedErrors((JetFile) getFile()); doTestFor(pathToFile, intentionAction, fileText); } diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java index 497bc76969c..694977bce05 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java @@ -125,7 +125,7 @@ public abstract class AbstractQuickFixMultiFileTest extends KotlinDaemonAnalyzer boolean actionShouldBeAvailable = pair.getSecond(); if (psiFile instanceof JetFile) { - DirectiveBasedActionUtils.checkForUnexpectedErrors((JetFile) psiFile); + DirectiveBasedActionUtils.INSTANCE$.checkForUnexpectedErrors((JetFile) psiFile); } doAction(text, actionShouldBeAvailable, beforeFileName); @@ -163,7 +163,7 @@ public abstract class AbstractQuickFixMultiFileTest extends KotlinDaemonAnalyzer "Infos:" + infos); } else { - DirectiveBasedActionUtils.checkAvailableActionsAreExpected((JetFile) getFile(), availableActions); + DirectiveBasedActionUtils.INSTANCE$.checkAvailableActionsAreExpected((JetFile) getFile(), availableActions); } } else { diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.java index 520945f8ea2..2e4758793c5 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixTest.java @@ -219,13 +219,13 @@ public abstract class AbstractQuickFixTest extends KotlinLightQuickFixTestCase { } else { // Action shouldn't be found. Check that other actions are expected and thus tested action isn't there under another name. - DirectiveBasedActionUtils.checkAvailableActionsAreExpected((JetFile) getFile(), actions); + DirectiveBasedActionUtils.INSTANCE$.checkAvailableActionsAreExpected((JetFile) getFile(), actions); } } } public static void checkForUnexpectedErrors() { - DirectiveBasedActionUtils.checkForUnexpectedErrors((JetFile) getFile()); + DirectiveBasedActionUtils.INSTANCE$.checkForUnexpectedErrors((JetFile) getFile()); } @Override diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/changeSignature/JetChangeSignatureTest.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/changeSignature/JetChangeSignatureTest.java index 854d45cda5c..ee46b50d103 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/changeSignature/JetChangeSignatureTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/changeSignature/JetChangeSignatureTest.java @@ -1110,7 +1110,7 @@ public class JetChangeSignatureTest extends KotlinCodeInsightTestCase { PsiFile currentFile = getFile(); checkResultByFile(currentFile.getName().replace("Before.", "After.")); if (checkErrorsAfter && currentFile instanceof JetFile) { - DirectiveBasedActionUtils.checkForUnexpectedErrors((JetFile) currentFile); + DirectiveBasedActionUtils.INSTANCE$.checkForUnexpectedErrors((JetFile) currentFile); } } }