AbstractQuickFixTest: J2K
This commit is contained in:
@@ -14,349 +14,315 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.quickfix;
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
|
||||
import com.intellij.codeInsight.daemon.quickFix.ActionHint;
|
||||
import com.intellij.codeInsight.daemon.quickFix.QuickFixTestCase;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.codeInspection.InspectionProfileEntry;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.SuppressIntentionAction;
|
||||
import com.intellij.codeInspection.SuppressableProblemGroup;
|
||||
import com.intellij.ide.startup.impl.StartupManagerImpl;
|
||||
import com.intellij.lang.annotation.ProblemGroup;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.projectRoots.JavaSdk;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.startup.StartupManager;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.CharsetToolkit;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.stubs.StubUpdatingIndex;
|
||||
import com.intellij.rt.execution.junit.FileComparisonFailure;
|
||||
import com.intellij.testFramework.InspectionTestUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.indexing.FileBasedIndex;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import kotlin.io.FilesKt;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import kotlin.text.StringsKt;
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.idea.KotlinLightQuickFixTestCase;
|
||||
import org.jetbrains.kotlin.idea.quickfix.utils.QuickfixTestUtilsKt;
|
||||
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil;
|
||||
import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils;
|
||||
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase;
|
||||
import org.jetbrains.kotlin.idea.test.TestFixtureExtension;
|
||||
import org.jetbrains.kotlin.psi.KtFile;
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.junit.Assert;
|
||||
import com.intellij.codeInsight.daemon.quickFix.ActionHint
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase
|
||||
import com.intellij.codeInsight.daemon.quickFix.QuickFixTestCase
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.codeInspection.InspectionProfileEntry
|
||||
import com.intellij.codeInspection.LocalInspectionTool
|
||||
import com.intellij.codeInspection.SuppressableProblemGroup
|
||||
import com.intellij.ide.startup.impl.StartupManagerImpl
|
||||
import com.intellij.openapi.command.CommandProcessor
|
||||
import com.intellij.openapi.projectRoots.JavaSdk
|
||||
import com.intellij.openapi.projectRoots.Sdk
|
||||
import com.intellij.openapi.startup.StartupManager
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import com.intellij.openapi.vfs.CharsetToolkit
|
||||
import com.intellij.psi.stubs.StubUpdatingIndex
|
||||
import com.intellij.rt.execution.junit.FileComparisonFailure
|
||||
import com.intellij.testFramework.InspectionTestUtil
|
||||
import com.intellij.testFramework.LightPlatformCodeInsightTestCase
|
||||
import com.intellij.testFramework.LightPlatformTestCase
|
||||
import com.intellij.testFramework.UsefulTestCase
|
||||
import com.intellij.util.ArrayUtil
|
||||
import com.intellij.util.ObjectUtils.notNull
|
||||
import com.intellij.util.indexing.FileBasedIndex
|
||||
import junit.framework.TestCase
|
||||
import org.apache.commons.lang.SystemUtils
|
||||
import org.jetbrains.kotlin.idea.KotlinLightQuickFixTestCase
|
||||
import org.jetbrains.kotlin.idea.quickfix.utils.findInspectionFile
|
||||
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil
|
||||
import org.jetbrains.kotlin.idea.test.DirectiveBasedActionUtils
|
||||
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
|
||||
import org.jetbrains.kotlin.idea.test.TestFixtureExtension
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.junit.Assert
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.nio.charset.Charset
|
||||
import java.util.*
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.util.ObjectUtils.notNull;
|
||||
|
||||
public abstract class AbstractQuickFixTest extends KotlinLightQuickFixTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
((StartupManagerImpl) StartupManager.getInstance(getProject())).runPostStartupActivities();
|
||||
abstract class AbstractQuickFixTest : KotlinLightQuickFixTestCase() {
|
||||
@Throws(Exception::class)
|
||||
override fun setUp() {
|
||||
super.setUp()
|
||||
(StartupManager.getInstance(LightPlatformTestCase.getProject()) as StartupManagerImpl).runPostStartupActivities()
|
||||
}
|
||||
|
||||
protected void doTest(@NotNull String beforeFileName) throws Exception {
|
||||
@Throws(Exception::class)
|
||||
protected fun doTest(beforeFileName: String) {
|
||||
try {
|
||||
configureRuntimeIfNeeded(beforeFileName);
|
||||
configureRuntimeIfNeeded(beforeFileName)
|
||||
|
||||
enableInspections(beforeFileName);
|
||||
enableInspections(beforeFileName)
|
||||
|
||||
doSingleTest(getTestName(false) + ".kt");
|
||||
checkForUnexpectedErrors();
|
||||
doSingleTest(getTestName(false) + ".kt")
|
||||
checkForUnexpectedErrors()
|
||||
}
|
||||
finally {
|
||||
unConfigureRuntimeIfNeeded(beforeFileName);
|
||||
unConfigureRuntimeIfNeeded(beforeFileName)
|
||||
}
|
||||
}
|
||||
|
||||
//region Severe hack - lot of code copied from LightQuickFixTestCase to workaround stupid format of test data with before/after prefixes
|
||||
@Override
|
||||
protected void doSingleTest(String fileSuffix) {
|
||||
doKotlinQuickFixTest(fileSuffix, createWrapper());
|
||||
override fun doSingleTest(fileSuffix: String) {
|
||||
doKotlinQuickFixTest(fileSuffix, createWrapper())
|
||||
}
|
||||
|
||||
private static QuickFixTestCase myWrapper;
|
||||
|
||||
@Override
|
||||
protected boolean shouldBeAvailableAfterExecution() {
|
||||
return InTextDirectivesUtils.isDirectiveDefined(myWrapper.getFile().getText(), "// SHOULD_BE_AVAILABLE_AFTER_EXECUTION");
|
||||
override fun shouldBeAvailableAfterExecution(): Boolean {
|
||||
return InTextDirectivesUtils.isDirectiveDefined(myWrapper!!.file.text, "// SHOULD_BE_AVAILABLE_AFTER_EXECUTION")
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected LocalInspectionTool[] configureLocalInspectionTools() {
|
||||
if (KotlinTestUtils.isAllFilesPresentTest(getTestName(false))) return super.configureLocalInspectionTools();
|
||||
override fun configureLocalInspectionTools(): Array<LocalInspectionTool> {
|
||||
if (KotlinTestUtils.isAllFilesPresentTest(getTestName(false))) return super.configureLocalInspectionTools()
|
||||
|
||||
String testRoot = KotlinTestUtils.getTestsRoot(this.getClass());
|
||||
String configFileText = FilesKt.readText(new File(testRoot, getTestName(true) + ".kt"), Charset.defaultCharset());
|
||||
List<String> toolsStrings = InTextDirectivesUtils.findListWithPrefixes(configFileText, "TOOL:");
|
||||
val testRoot = KotlinTestUtils.getTestsRoot(this.javaClass)
|
||||
val configFileText = File(testRoot, getTestName(true) + ".kt").readText(Charset.defaultCharset())
|
||||
val toolsStrings = InTextDirectivesUtils.findListWithPrefixes(configFileText, "TOOL:")
|
||||
|
||||
if (toolsStrings.isEmpty()) return super.configureLocalInspectionTools();
|
||||
if (toolsStrings.isEmpty()) return super.configureLocalInspectionTools()
|
||||
|
||||
return ArrayUtil.toObjectArray(CollectionsKt.map(toolsStrings, new Function1<String, LocalInspectionTool>() {
|
||||
@Override
|
||||
public LocalInspectionTool invoke(String toolFqName) {
|
||||
try {
|
||||
Class<?> aClass = Class.forName(toolFqName);
|
||||
return (LocalInspectionTool) aClass.newInstance();
|
||||
return ArrayUtil.toObjectArray(toolsStrings.map { toolFqName ->
|
||||
try {
|
||||
val aClass = Class.forName(toolFqName)
|
||||
return@map aClass.newInstance() as LocalInspectionTool
|
||||
}
|
||||
catch (e: Exception) {
|
||||
throw IllegalArgumentException("Failed to create inspection for key '$toolFqName'", e)
|
||||
}
|
||||
}, LocalInspectionTool::class.java)
|
||||
}
|
||||
|
||||
protected open fun configExtra(options: String) {
|
||||
|
||||
}
|
||||
|
||||
private fun doKotlinQuickFixTest(testName: String, quickFixTestCase: QuickFixTestCase) {
|
||||
val relativePath = notNull(quickFixTestCase.basePath, "") + "/" + testName.decapitalize()
|
||||
val testFullPath = quickFixTestCase.testDataPath.replace(File.separatorChar, '/') + relativePath
|
||||
val testFile = File(testFullPath)
|
||||
CommandProcessor.getInstance().executeCommand(quickFixTestCase.project, {
|
||||
var fileText = ""
|
||||
var expectedErrorMessage: String? = ""
|
||||
var fixtureClasses = emptyList<String>()
|
||||
try {
|
||||
fileText = FileUtil.loadFile(testFile, CharsetToolkit.UTF8_CHARSET)
|
||||
TestCase.assertTrue("\"<caret>\" is missing in file \"$testName\"", fileText.contains("<caret>"))
|
||||
|
||||
fixtureClasses = InTextDirectivesUtils.findListWithPrefixes(fileText, "// FIXTURE_CLASS: ")
|
||||
for (fixtureClass in fixtureClasses) {
|
||||
TestFixtureExtension.loadFixture(fixtureClass, LightPlatformTestCase.getModule())
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalArgumentException("Failed to create inspection for key '" + toolFqName + "'", e);
|
||||
|
||||
expectedErrorMessage = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// SHOULD_FAIL_WITH: ")
|
||||
val contents = StringUtil.convertLineSeparators(fileText)
|
||||
quickFixTestCase.configureFromFileText(testFile.name, contents)
|
||||
quickFixTestCase.bringRealEditorBack()
|
||||
|
||||
checkForUnexpectedActions()
|
||||
|
||||
configExtra(fileText)
|
||||
|
||||
applyAction(contents, quickFixTestCase, testName, testFullPath)
|
||||
|
||||
UsefulTestCase.assertEmpty(expectedErrorMessage)
|
||||
}
|
||||
catch (e: FileComparisonFailure) {
|
||||
throw e
|
||||
}
|
||||
catch (e: AssertionError) {
|
||||
throw e
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
if (expectedErrorMessage == null || expectedErrorMessage != e.message) {
|
||||
e.printStackTrace()
|
||||
TestCase.fail(testName)
|
||||
}
|
||||
}
|
||||
}), LocalInspectionTool.class);
|
||||
}
|
||||
|
||||
protected void configExtra(String options) {
|
||||
|
||||
}
|
||||
|
||||
private void doKotlinQuickFixTest(final String testName, final QuickFixTestCase quickFixTestCase) {
|
||||
String relativePath = notNull(quickFixTestCase.getBasePath(), "") + "/" + StringsKt.decapitalize(testName);
|
||||
final String testFullPath = quickFixTestCase.getTestDataPath().replace(File.separatorChar, '/') + relativePath;
|
||||
final File testFile = new File(testFullPath);
|
||||
CommandProcessor.getInstance().executeCommand(quickFixTestCase.getProject(), new Runnable() {
|
||||
@SuppressWarnings({"AssignmentToStaticFieldFromInstanceMethod", "CallToPrintStackTrace"})
|
||||
@Override
|
||||
public void run() {
|
||||
String fileText = "";
|
||||
String expectedErrorMessage = "";
|
||||
List<String> fixtureClasses = Collections.emptyList();
|
||||
try {
|
||||
fileText = FileUtil.loadFile(testFile, CharsetToolkit.UTF8_CHARSET);
|
||||
assertTrue("\"<caret>\" is missing in file \"" + testName + "\"", fileText.contains("<caret>"));
|
||||
|
||||
fixtureClasses = InTextDirectivesUtils.findListWithPrefixes(fileText, "// FIXTURE_CLASS: ");
|
||||
for (String fixtureClass : fixtureClasses) {
|
||||
TestFixtureExtension.Companion.loadFixture(fixtureClass, getModule());
|
||||
}
|
||||
|
||||
expectedErrorMessage = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// SHOULD_FAIL_WITH: ");
|
||||
String contents = StringUtil.convertLineSeparators(fileText);
|
||||
quickFixTestCase.configureFromFileText(testFile.getName(), contents);
|
||||
quickFixTestCase.bringRealEditorBack();
|
||||
|
||||
checkForUnexpectedActions();
|
||||
|
||||
configExtra(fileText);
|
||||
|
||||
applyAction(contents, quickFixTestCase, testName, testFullPath);
|
||||
|
||||
assertEmpty(expectedErrorMessage);
|
||||
}
|
||||
catch (FileComparisonFailure e) {
|
||||
throw e;
|
||||
}
|
||||
catch (AssertionError e) {
|
||||
throw e;
|
||||
}
|
||||
catch (Throwable e) {
|
||||
if (expectedErrorMessage == null || !expectedErrorMessage.equals(e.getMessage())) {
|
||||
e.printStackTrace();
|
||||
fail(testName);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
for (String fixtureClass : fixtureClasses) {
|
||||
TestFixtureExtension.Companion.unloadFixture(fixtureClass);
|
||||
}
|
||||
ConfigLibraryUtil.unconfigureLibrariesByDirective(getModule(), fileText);
|
||||
finally {
|
||||
for (fixtureClass in fixtureClasses) {
|
||||
TestFixtureExtension.unloadFixture(fixtureClass)
|
||||
}
|
||||
ConfigLibraryUtil.unconfigureLibrariesByDirective(LightPlatformTestCase.getModule(), fileText)
|
||||
}
|
||||
}, "", "");
|
||||
}, "", "")
|
||||
}
|
||||
|
||||
private static void applyAction(String contents, QuickFixTestCase quickFixTestCase, String testName, String testFullPath)
|
||||
throws Exception {
|
||||
String fileName = StringsKt.substringAfterLast(testFullPath, "/", "");
|
||||
ActionHint actionHint = ActionHint.parse(quickFixTestCase.getFile(), contents.replace("${file}", fileName));
|
||||
|
||||
quickFixTestCase.beforeActionStarted(testName, contents);
|
||||
|
||||
try {
|
||||
myWrapper = quickFixTestCase;
|
||||
quickFixTestCase.doAction(actionHint, testFullPath, testName);
|
||||
}
|
||||
finally {
|
||||
myWrapper = null;
|
||||
quickFixTestCase.afterActionCompleted(testName, contents);
|
||||
}
|
||||
@Throws(Exception::class)
|
||||
override fun doAction(actionHint: ActionHint, testFullPath: String, testName: String) {
|
||||
LightQuickFixTestCase.doAction(actionHint, testFullPath, testName, myWrapper!!)
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAction(ActionHint actionHint, String testFullPath, String testName) throws Exception {
|
||||
doAction(actionHint, testFullPath, testName, myWrapper);
|
||||
override fun checkResultByFile(message: String?, filePath: String, ignoreTrailingSpaces: Boolean) {
|
||||
val file = File(filePath)
|
||||
val afterFileName = file.name
|
||||
assert(afterFileName.startsWith(LightQuickFixTestCase.AFTER_PREFIX))
|
||||
val newAfterFileName = afterFileName.substring(LightQuickFixTestCase.AFTER_PREFIX.length).decapitalize() + ".after"
|
||||
|
||||
super.checkResultByFile(message, File(file.parent, newAfterFileName).path, ignoreTrailingSpaces)
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkResultByFile(@Nullable String message, @NotNull String filePath, boolean ignoreTrailingSpaces) {
|
||||
File file = new File(filePath);
|
||||
String afterFileName = file.getName();
|
||||
assert afterFileName.startsWith(AFTER_PREFIX);
|
||||
String newAfterFileName = StringsKt.decapitalize(afterFileName.substring(AFTER_PREFIX.length())) + ".after";
|
||||
|
||||
super.checkResultByFile(message, new File(file.getParent(), newAfterFileName).getPath(), ignoreTrailingSpaces);
|
||||
}
|
||||
//endregion
|
||||
|
||||
private static void configureRuntimeIfNeeded(@NotNull String beforeFileName) throws IOException {
|
||||
@Throws(IOException::class)
|
||||
private fun unConfigureRuntimeIfNeeded(beforeFileName: String) {
|
||||
if (beforeFileName.endsWith("JsRuntime.kt")) {
|
||||
// Without the following line of code subsequent tests with js-runtime will be prone to failure due "outdated stub in index" error.
|
||||
FileBasedIndex.getInstance().requestRebuild(StubUpdatingIndex.INDEX_ID);
|
||||
|
||||
ConfigLibraryUtil.configureKotlinJsRuntimeAndSdk(getModule(), getFullJavaJDK());
|
||||
ConfigLibraryUtil.unConfigureKotlinJsRuntimeAndSdk(LightPlatformTestCase.getModule(), projectJDK)
|
||||
}
|
||||
else if (isRuntimeNeeded(beforeFileName)) {
|
||||
ConfigLibraryUtil.configureKotlinRuntimeAndSdk(getModule(), getFullJavaJDK());
|
||||
}
|
||||
else if (beforeFileName.contains("Runtime") || beforeFileName.contains("JsRuntime")) {
|
||||
Assert.fail("Runtime marker is used in test name, but not in test file end. " +
|
||||
"This can lead to false-positive absent of actions");
|
||||
ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(LightPlatformTestCase.getModule(), projectJDK)
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isRuntimeNeeded(@NotNull String beforeFileName) throws IOException {
|
||||
return beforeFileName.endsWith("Runtime.kt") ||
|
||||
beforeFileName.toLowerCase().contains("createfromusage") ||
|
||||
InTextDirectivesUtils.isDirectiveDefined(FileUtil.loadFile(new File(beforeFileName)), "WITH_RUNTIME");
|
||||
}
|
||||
|
||||
private void unConfigureRuntimeIfNeeded(@NotNull String beforeFileName) throws IOException {
|
||||
if (beforeFileName.endsWith("JsRuntime.kt")) {
|
||||
ConfigLibraryUtil.unConfigureKotlinJsRuntimeAndSdk(getModule(), getProjectJDK());
|
||||
}
|
||||
else if (isRuntimeNeeded(beforeFileName)) {
|
||||
ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(getModule(), getProjectJDK());
|
||||
}
|
||||
}
|
||||
|
||||
private void enableInspections(String beforeFileName) throws IOException, ClassNotFoundException {
|
||||
File inspectionFile = QuickfixTestUtilsKt.findInspectionFile(new File(beforeFileName).getParentFile());
|
||||
@Throws(IOException::class, ClassNotFoundException::class)
|
||||
private fun enableInspections(beforeFileName: String) {
|
||||
val inspectionFile = findInspectionFile(File(beforeFileName).parentFile)
|
||||
if (inspectionFile != null) {
|
||||
String className = FileUtil.loadFile(inspectionFile).trim();
|
||||
Class<InspectionProfileEntry> inspectionClass = (Class<InspectionProfileEntry>) Class.forName(className);
|
||||
List<InspectionProfileEntry> tools = InspectionTestUtil.instantiateTools(
|
||||
Collections.<Class<? extends InspectionProfileEntry>>singletonList(inspectionClass));
|
||||
enableInspectionTools(tools.get(0));
|
||||
val className = FileUtil.loadFile(inspectionFile).trim { it <= ' ' }
|
||||
val inspectionClass = Class.forName(className) as Class<InspectionProfileEntry>
|
||||
val tools = InspectionTestUtil.instantiateTools(
|
||||
listOf<Class<out InspectionProfileEntry>>(inspectionClass))
|
||||
enableInspectionTools(tools[0])
|
||||
}
|
||||
}
|
||||
|
||||
private void checkForUnexpectedActions() throws ClassNotFoundException {
|
||||
String text = getEditor().getDocument().getText();
|
||||
ActionHint actionHint = ActionHint.parse(getFile(), text);
|
||||
@Throws(ClassNotFoundException::class)
|
||||
private fun checkForUnexpectedActions() {
|
||||
val text = LightPlatformCodeInsightTestCase.getEditor().getDocument().getText()
|
||||
val actionHint = ActionHint.parse(LightPlatformCodeInsightTestCase.getFile(), text)
|
||||
if (!actionHint.shouldPresent()) {
|
||||
List<IntentionAction> actions = getAvailableActions();
|
||||
val actions = availableActions
|
||||
|
||||
String prefix = "class ";
|
||||
if (actionHint.getExpectedText().startsWith(prefix)) {
|
||||
String className = actionHint.getExpectedText().substring(prefix.length());
|
||||
final Class<?> aClass = Class.forName(className);
|
||||
assert IntentionAction.class.isAssignableFrom(aClass) : className + " should be inheritor of IntentionAction";
|
||||
val prefix = "class "
|
||||
if (actionHint.expectedText.startsWith(prefix)) {
|
||||
val className = actionHint.expectedText.substring(prefix.length)
|
||||
val aClass = Class.forName(className)
|
||||
assert(IntentionAction::class.java.isAssignableFrom(aClass)) { className + " should be inheritor of IntentionAction" }
|
||||
|
||||
final Set<String> validActions =
|
||||
new HashSet<String>(InTextDirectivesUtils.findLinesWithPrefixesRemoved(text, "// ACTION:"));
|
||||
val validActions = HashSet(InTextDirectivesUtils.findLinesWithPrefixesRemoved(text, "// ACTION:"))
|
||||
|
||||
CollectionsKt.removeAll(actions, new Function1<IntentionAction, Boolean>() {
|
||||
@Override
|
||||
public Boolean invoke(IntentionAction action) {
|
||||
return !aClass.isAssignableFrom(action.getClass()) || validActions.contains(action.getText());
|
||||
}
|
||||
});
|
||||
actions.removeAll { action -> !aClass.isAssignableFrom(action.javaClass) || validActions.contains(action.text) }
|
||||
|
||||
if (!actions.isEmpty()) {
|
||||
Assert.fail("Unexpected intention actions present\n " +
|
||||
CollectionsKt.map(actions, new Function1<IntentionAction, String>() {
|
||||
@Override
|
||||
public String invoke(IntentionAction action) {
|
||||
return action.getClass().toString() + " " + action.toString() + "\n";
|
||||
}
|
||||
})
|
||||
);
|
||||
Assert.fail("Unexpected intention actions present\n " + actions.map { action -> action.javaClass.toString() + " " + action.toString() + "\n" }
|
||||
)
|
||||
}
|
||||
|
||||
for (IntentionAction action : actions) {
|
||||
if (aClass.isAssignableFrom(action.getClass()) && !validActions.contains(action.getText())) {
|
||||
Assert.fail("Unexpected intention action " + action.getClass() + " found");
|
||||
for (action in actions) {
|
||||
if (aClass.isAssignableFrom(action.javaClass) && !validActions.contains(action.text)) {
|
||||
Assert.fail("Unexpected intention action " + action.javaClass + " found")
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Action shouldn't be found. Check that other actions are expected and thus tested action isn't there under another name.
|
||||
DirectiveBasedActionUtils.INSTANCE.checkAvailableActionsAreExpected(getFile(), actions);
|
||||
DirectiveBasedActionUtils.checkAvailableActionsAreExpected(LightPlatformCodeInsightTestCase.getFile(), actions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkForUnexpectedErrors() {
|
||||
DirectiveBasedActionUtils.INSTANCE.checkForUnexpectedErrors((KtFile) getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IntentionAction findActionWithText(String text) {
|
||||
IntentionAction intention = super.findActionWithText(text);
|
||||
if (intention != null) return intention;
|
||||
override fun findActionWithText(text: String): IntentionAction? {
|
||||
val intention = super.findActionWithText(text)
|
||||
if (intention != null) return intention
|
||||
|
||||
// Support warning suppression
|
||||
int caretOffset = myEditor.getCaretModel().getOffset();
|
||||
for (HighlightInfo highlight : doHighlighting()) {
|
||||
val caretOffset = LightPlatformCodeInsightTestCase.myEditor.getCaretModel().getOffset()
|
||||
for (highlight in doHighlighting()) {
|
||||
if (highlight.startOffset <= caretOffset && caretOffset <= highlight.endOffset) {
|
||||
ProblemGroup group = highlight.getProblemGroup();
|
||||
if (group instanceof SuppressableProblemGroup) {
|
||||
SuppressableProblemGroup problemGroup = (SuppressableProblemGroup) group;
|
||||
PsiElement at = getFile().findElementAt(highlight.getActualStartOffset());
|
||||
SuppressIntentionAction[] actions = problemGroup.getSuppressActions(at);
|
||||
for (SuppressIntentionAction action : actions) {
|
||||
if (action.getText().equals(text)) {
|
||||
return action;
|
||||
val group = highlight.problemGroup
|
||||
if (group is SuppressableProblemGroup) {
|
||||
val at = LightPlatformCodeInsightTestCase.getFile().findElementAt(highlight.actualStartOffset)
|
||||
val actions = group.getSuppressActions(at)
|
||||
for (action in actions) {
|
||||
if (action.text == text) {
|
||||
return action
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkResultByText(String message, @NotNull String fileText, boolean ignoreTrailingSpaces, String filePath) {
|
||||
super.checkResultByText(message, fileText, ignoreTrailingSpaces, new File(filePath).getAbsolutePath());
|
||||
override fun checkResultByText(message: String?, fileText: String, ignoreTrailingSpaces: Boolean, filePath: String?) {
|
||||
super.checkResultByText(message, fileText, ignoreTrailingSpaces, File(filePath!!).absolutePath)
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return KotlinTestUtils.getTestsRoot(getClass());
|
||||
override fun getBasePath(): String {
|
||||
return KotlinTestUtils.getTestsRoot(javaClass)
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return "./";
|
||||
override fun getTestDataPath(): String {
|
||||
return "./"
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Sdk getProjectJDK() {
|
||||
return PluginTestCaseBase.mockJdk();
|
||||
override fun getProjectJDK(): Sdk {
|
||||
return PluginTestCaseBase.mockJdk()
|
||||
}
|
||||
|
||||
protected static Sdk getFullJavaJDK() {
|
||||
return JavaSdk.getInstance().createJdk("JDK", SystemUtils.getJavaHome().getAbsolutePath());
|
||||
companion object {
|
||||
|
||||
private var myWrapper: QuickFixTestCase? = null
|
||||
|
||||
@Throws(Exception::class)
|
||||
private fun applyAction(contents: String, quickFixTestCase: QuickFixTestCase, testName: String, testFullPath: String) {
|
||||
val fileName = testFullPath.substringAfterLast("/", "")
|
||||
val actionHint = ActionHint.parse(quickFixTestCase.file, contents.replace("\${file}", fileName))
|
||||
|
||||
quickFixTestCase.beforeActionStarted(testName, contents)
|
||||
|
||||
try {
|
||||
myWrapper = quickFixTestCase
|
||||
quickFixTestCase.doAction(actionHint, testFullPath, testName)
|
||||
}
|
||||
finally {
|
||||
myWrapper = null
|
||||
quickFixTestCase.afterActionCompleted(testName, contents)
|
||||
}
|
||||
}
|
||||
//endregion
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun configureRuntimeIfNeeded(beforeFileName: String) {
|
||||
if (beforeFileName.endsWith("JsRuntime.kt")) {
|
||||
// Without the following line of code subsequent tests with js-runtime will be prone to failure due "outdated stub in index" error.
|
||||
FileBasedIndex.getInstance().requestRebuild(StubUpdatingIndex.INDEX_ID)
|
||||
|
||||
ConfigLibraryUtil.configureKotlinJsRuntimeAndSdk(LightPlatformTestCase.getModule(), fullJavaJDK)
|
||||
}
|
||||
else if (isRuntimeNeeded(beforeFileName)) {
|
||||
ConfigLibraryUtil.configureKotlinRuntimeAndSdk(LightPlatformTestCase.getModule(), fullJavaJDK)
|
||||
}
|
||||
else if (beforeFileName.contains("Runtime") || beforeFileName.contains("JsRuntime")) {
|
||||
Assert.fail("Runtime marker is used in test name, but not in test file end. " + "This can lead to false-positive absent of actions")
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun isRuntimeNeeded(beforeFileName: String): Boolean {
|
||||
return beforeFileName.endsWith("Runtime.kt") ||
|
||||
beforeFileName.toLowerCase().contains("createfromusage") ||
|
||||
InTextDirectivesUtils.isDirectiveDefined(FileUtil.loadFile(File(beforeFileName)), "WITH_RUNTIME")
|
||||
}
|
||||
|
||||
fun checkForUnexpectedErrors() {
|
||||
DirectiveBasedActionUtils.checkForUnexpectedErrors(LightPlatformCodeInsightTestCase.getFile() as KtFile)
|
||||
}
|
||||
|
||||
protected val fullJavaJDK: Sdk
|
||||
get() = JavaSdk.getInstance().createJdk("JDK", SystemUtils.getJavaHome().absolutePath)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user