Now possible to use // WITH_RUNTIME in multi file tests

This commit is contained in:
Simon Ogorodnik
2016-08-31 00:20:22 +03:00
committed by Dmitry Jemerov
parent da2ebb1394
commit b06bdcef75
@@ -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<TestFile> subFiles = KotlinTestUtils.createTestFiles(
"single.kt",
multifileText,
new KotlinTestUtils.TestFileFactoryNoModules<TestFile>() {
@NotNull
@Override
public TestFile create(@NotNull String fileName, @NotNull String text, @NotNull Map<String, String> 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<TestFile, Boolean>() {
@Override
public Boolean invoke(TestFile file) {
return file.path.contains(".after");
}
});
final TestFile beforeFile = CollectionsKt.firstOrNull(subFiles, new Function1<TestFile, Boolean>() {
@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<TestFile> subFiles = KotlinTestUtils.createTestFiles(
"single.kt",
multifileText,
new KotlinTestUtils.TestFileFactoryNoModules<TestFile>() {
@NotNull
@Override
public TestFile create(@NotNull String fileName, @NotNull String text, @NotNull Map<String, String> 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<String, Boolean> 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(), "<caret>").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<TestFile, Boolean>() {
@Override
public Boolean invoke(TestFile file) {
return file.path.contains(".after");
}
});
final TestFile beforeFile = CollectionsKt.firstOrNull(subFiles, new Function1<TestFile, Boolean>() {
@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<String, Boolean> 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(), "<caret>").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 {