From b06bdcef7546a4da9e333b840127b53dd4782049 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Wed, 31 Aug 2016 00:20:22 +0300 Subject: [PATCH] Now possible to use // WITH_RUNTIME in multi file tests --- .../AbstractQuickFixMultiFileTest.java | 169 ++++++++++-------- 1 file changed, 91 insertions(+), 78 deletions(-) diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java index c9eced5704f..ec472346aa4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/AbstractQuickFixMultiFileTest.java @@ -232,94 +232,107 @@ public abstract class AbstractQuickFixMultiFileTest extends KotlinDaemonAnalyzer protected void doMultiFileTest(final String beforeFileName) throws Exception { String multifileText = FileUtil.loadFile(new File(beforeFileName), true); - final List subFiles = KotlinTestUtils.createTestFiles( - "single.kt", - multifileText, - new KotlinTestUtils.TestFileFactoryNoModules() { - @NotNull - @Override - public TestFile create(@NotNull String fileName, @NotNull String text, @NotNull Map directives) { - if (text.startsWith("// FILE")) { - String firstLineDropped = StringUtil.substringAfter(text, "\n"); - assert firstLineDropped != null; - - text = firstLineDropped; - } - return new TestFile(fileName, text); - } - }); - - final TestFile afterFile = CollectionsKt.firstOrNull(subFiles, new Function1() { - @Override - public Boolean invoke(TestFile file) { - return file.path.contains(".after"); - } - }); - final TestFile beforeFile = CollectionsKt.firstOrNull(subFiles, new Function1() { - @Override - public Boolean invoke(TestFile file) { - return file.path.contains(".before"); - } - }); - - assert beforeFile != null; - - subFiles.remove(beforeFile); - if (afterFile != null) { - subFiles.remove(afterFile); + boolean withRuntime = InTextDirectivesUtils.isDirectiveDefined(multifileText, "// WITH_RUNTIME"); + if (withRuntime) { + ConfigLibraryUtil.configureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk()); } - configureMultiFileTest(subFiles, beforeFile); + try { + final List subFiles = KotlinTestUtils.createTestFiles( + "single.kt", + multifileText, + new KotlinTestUtils.TestFileFactoryNoModules() { + @NotNull + @Override + public TestFile create(@NotNull String fileName, @NotNull String text, @NotNull Map directives) { + if (text.startsWith("// FILE")) { + String firstLineDropped = StringUtil.substringAfter(text, "\n"); + assert firstLineDropped != null; - CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { - @Override - public void run() { - try { - PsiFile psiFile = getFile(); - - Pair pair = LightQuickFixTestCase.parseActionHint(psiFile, beforeFile.content); - String text = pair.getFirst(); - - boolean actionShouldBeAvailable = pair.getSecond(); - - if (psiFile instanceof KtFile) { - DirectiveBasedActionUtils.INSTANCE.checkForUnexpectedErrors((KtFile) psiFile); - } - - doAction(text, actionShouldBeAvailable, getTestName(false)); - - String actualText = getFile().getText(); - String afterText = new StringBuilder(actualText).insert(getEditor().getCaretModel().getOffset(), "").toString(); - - if (pair.second) { - assertNotNull(".after file should exist", afterFile); - if (!afterText.equals(afterFile.content)) { - StringBuilder actualTestFile = new StringBuilder(); - actualTestFile.append("// FILE: ").append(beforeFile.path).append("\n").append(beforeFile.content); - for (TestFile file : subFiles) { - actualTestFile.append("// FILE: ").append(file.path).append("\n").append(file.content); + text = firstLineDropped; } - actualTestFile.append("// FILE: ").append(afterFile.path).append("\n").append(afterText); + return new TestFile(fileName, text); + } + }); - KotlinTestUtils.assertEqualsToFile(new File(beforeFileName), actualTestFile.toString()); + final TestFile afterFile = CollectionsKt.firstOrNull(subFiles, new Function1() { + @Override + public Boolean invoke(TestFile file) { + return file.path.contains(".after"); + } + }); + final TestFile beforeFile = CollectionsKt.firstOrNull(subFiles, new Function1() { + @Override + public Boolean invoke(TestFile file) { + return file.path.contains(".before"); + } + }); + + assert beforeFile != null; + + subFiles.remove(beforeFile); + if (afterFile != null) { + subFiles.remove(afterFile); + } + + configureMultiFileTest(subFiles, beforeFile); + + CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { + @Override + public void run() { + try { + PsiFile psiFile = getFile(); + + Pair pair = LightQuickFixTestCase.parseActionHint(psiFile, beforeFile.content); + String text = pair.getFirst(); + + boolean actionShouldBeAvailable = pair.getSecond(); + + if (psiFile instanceof KtFile) { + DirectiveBasedActionUtils.INSTANCE.checkForUnexpectedErrors((KtFile) psiFile); + } + + doAction(text, actionShouldBeAvailable, getTestName(false)); + + String actualText = getFile().getText(); + String afterText = + new StringBuilder(actualText).insert(getEditor().getCaretModel().getOffset(), "").toString(); + + if (pair.second) { + assertNotNull(".after file should exist", afterFile); + if (!afterText.equals(afterFile.content)) { + StringBuilder actualTestFile = new StringBuilder(); + actualTestFile.append("// FILE: ").append(beforeFile.path).append("\n").append(beforeFile.content); + for (TestFile file : subFiles) { + actualTestFile.append("// FILE: ").append(file.path).append("\n").append(file.content); + } + actualTestFile.append("// FILE: ").append(afterFile.path).append("\n").append(afterText); + + KotlinTestUtils.assertEqualsToFile(new File(beforeFileName), actualTestFile.toString()); + } + } + else { + assertNull(".after file should not exist", afterFile); } } - else { - assertNull(".after file should not exist", afterFile); + catch (ComparisonFailure e) { + throw e; + } + catch (AssertionError e) { + throw e; + } + catch (Throwable e) { + e.printStackTrace(); + fail(getTestName(true)); } } - catch (ComparisonFailure e) { - throw e; - } - catch (AssertionError e) { - throw e; - } - catch (Throwable e) { - e.printStackTrace(); - fail(getTestName(true)); - } + }, "", ""); + } + finally { + if (withRuntime) { + ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(myModule, PluginTestCaseBase.mockJdk()); } - }, "", ""); + } } private void doTest(final String beforeFileName, boolean withExtraFile) throws Exception {