From c0062b1199b23d93d05243b827effbec9fdef051 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 11 Jun 2015 13:45:30 +0300 Subject: [PATCH] Code simplifications --- .../idea/test/DirectiveBasedActionUtils.kt | 75 +++++++------------ 1 file changed, 25 insertions(+), 50 deletions(-) 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 index aab61c9e663..fca6b8c4d84 100644 --- 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 @@ -16,77 +16,52 @@ 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.caches.resolve.analyzeFully 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()) { + if (InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// DISABLE-ERRORS").isNotEmpty()) { 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 expectedErrors = InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// ERROR:").sort() - val expectedErrorStrings = InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// ERROR:") - Collections.sort(expectedErrorStrings) + val actualErrors = file.analyzeFully().getDiagnostics() + .filter { it.getSeverity() == Severity.ERROR } + .map { IdeErrorMessages.render(it) } + .sort() - 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) + UsefulTestCase.assertOrderedEquals("All actual errors should be mentioned in test data with // ERROR: directive. But no unnecessary errors should be me mentioned", + actualErrors, + expectedErrors) } public fun checkAvailableActionsAreExpected(file: JetFile, availableActions: Collection) { - val validActions = Ordering.natural().sortedCopy(Lists.newArrayList(InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// ACTION:"))) + val expectedActions = InTextDirectivesUtils.findLinesWithPrefixesRemoved(file.getText(), "// ACTION:").sort() - val actualActions = Ordering.natural().sortedCopy(Lists.newArrayList(Collections2.transform(availableActions, object : Function { - override fun apply(input: IntentionAction?): String? { - assert(input != null) - return input!!.getText() - } - }))) + val actualActions = availableActions.map { it.getText() }.sort() - UsefulTestCase.assertOrderedEquals("Some unexpected actions available at current position. Use // ACTION: directive", filterOutIrrelevantActions(actualActions), filterOutIrrelevantActions(validActions)) + UsefulTestCase.assertOrderedEquals("Some unexpected actions available at current position. Use // ACTION: directive", + filterOutIrrelevantActions(actualActions), + filterOutIrrelevantActions(expectedActions)) } - 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 - } - }) + //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 fun filterOutIrrelevantActions(actions: Collection): Collection { + return actions.filter { action -> IRRELEVANT_ACTION_PREFIXES.none { action.startsWith(it) } } } - private val IRRELEVANT_ACTION_PREFIXES = Arrays.asList("Disable ", "Edit intention settings", "Edit inspection profile setting", "Inject language or reference") + private val IRRELEVANT_ACTION_PREFIXES = listOf( + "Disable ", + "Edit intention settings", + "Edit inspection profile setting", + "Inject language or reference" + ) }