Converted to Kotlin

This commit is contained in:
Valentin Kipyatkov
2015-04-08 18:12:31 +03:00
parent fe14107848
commit 11380358ee
2 changed files with 127 additions and 139 deletions
@@ -1,139 +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.codeInsight;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.checkers.DebugInfoUtil;
import org.jetbrains.kotlin.diagnostics.Diagnostic;
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils;
import org.jetbrains.kotlin.diagnostics.Errors;
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages;
import org.jetbrains.kotlin.idea.AbstractCopyPasteTest;
import org.jetbrains.kotlin.idea.PluginTestCaseBase;
import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.psi.JetReferenceExpression;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode;
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
import java.io.File;
import java.util.List;
public abstract class AbstractInsertImportOnPasteTest extends AbstractCopyPasteTest {
private static final String BASE_PATH = PluginTestCaseBase.getTestDataPathBase() + "/copyPaste/imports";
private static final String DEFAULT_TO_FILE_TEXT = "package to\n\n<caret>";
private static final String ALLOW_UNRESOLVED_DIRECTIVE = "// ALLOW_UNRESOLVED";
public void doTestCut(String path) throws Exception {
doTestAction(IdeActions.ACTION_CUT, path);
}
public void doTestCopy(String path) throws Exception {
doTestAction(IdeActions.ACTION_COPY, path);
}
private void doTestAction(@NotNull String cutOrCopy, String path) throws Exception {
myFixture.setTestDataPath(BASE_PATH);
File testFile = new File(path);
String testFileName = testFile.getName();
configureByDependencyIfExists(testFileName.replace(".kt", ".dependency.kt"));
configureByDependencyIfExists(testFileName.replace(".kt", ".dependency.java"));
myFixture.configureByFile(testFileName);
myFixture.performEditorAction(cutOrCopy);
String toFileName = testFileName.replace(".kt", ".to.kt");
JetFile toFile = configureToFile(toFileName);
performNotWriteEditorAction(IdeActions.ACTION_PASTE);
myFixture.checkResultByFile(testFileName.replace(".kt", ".expected.kt"));
if (!InTextDirectivesUtils.isDirectiveDefined(FileUtil.loadFile(testFile, true), ALLOW_UNRESOLVED_DIRECTIVE)) {
checkNoUnresolvedReferences(toFile);
}
}
@NotNull
private JetFile configureToFile(@NotNull String toFileName) {
if (new File(BASE_PATH + "/" + toFileName).exists()) {
return (JetFile) myFixture.configureByFile(toFileName);
}
else {
return (JetFile) myFixture.configureByText(toFileName, DEFAULT_TO_FILE_TEXT);
}
}
private static void checkNoUnresolvedReferences(@NotNull final JetFile file) {
BindingContext bindingContext = ResolvePackage.analyzeFully(file);
for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) {
List<TextRange> textRanges = diagnostic.getTextRanges();
String diagnosticText = DefaultErrorMessages.render(diagnostic);
if (diagnostic.getPsiFile() == file) {
fail(diagnostic.getFactory().getName() + ": " + diagnosticText + " "
+ DiagnosticUtils.atLocation(file, textRanges.get(0)));
}
}
}
DebugInfoUtil.markDebugAnnotations(file, bindingContext, new DebugInfoUtil.DebugInfoReporter() {
@Override
public void preProcessReference(@NotNull JetReferenceExpression expression) {
ResolvePackage.analyze(expression, BodyResolveMode.FULL);
}
@Override
public void reportElementWithErrorType(@NotNull JetReferenceExpression expression) {
//do nothing
}
@Override
public void reportMissingUnresolved(@NotNull JetReferenceExpression expression) {
// this may happen if incorrect psi transformations are done
fail(expression.getText() + " is unresolved but not marked " + DiagnosticUtils.atLocation(file, expression.getTextRange()));
}
@Override
public void reportUnresolvedWithTarget(
@NotNull JetReferenceExpression expression, @NotNull String target
) {
//do nothing
}
});
}
private void configureByDependencyIfExists(@NotNull String dependencyFileName) throws Exception {
File file = new File(BASE_PATH + "/" + dependencyFileName);
if (file.exists()) {
if (dependencyFileName.endsWith(".java")) {
//allow test framework to put it under right directory
myFixture.addClass(FileUtil.loadFile(file, true));
}
else {
myFixture.configureByFile(dependencyFileName);
}
}
}
@Override
protected String getTestDataPath() {
return BASE_PATH;
}
}
@@ -0,0 +1,127 @@
/*
* 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.codeInsight
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.util.io.FileUtil
import org.jetbrains.kotlin.checkers.DebugInfoUtil
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
import org.jetbrains.kotlin.idea.AbstractCopyPasteTest
import org.jetbrains.kotlin.idea.PluginTestCaseBase
import org.jetbrains.kotlin.idea.caches.resolve.*
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetReferenceExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import java.io.File
import kotlin.test.fail
public abstract class AbstractInsertImportOnPasteTest : AbstractCopyPasteTest() {
private val BASE_PATH = PluginTestCaseBase.getTestDataPathBase() + "/copyPaste/imports"
private val DEFAULT_TO_FILE_TEXT = "package to\n\n<caret>"
private val ALLOW_UNRESOLVED_DIRECTIVE = "// ALLOW_UNRESOLVED"
override fun getTestDataPath() = BASE_PATH
protected fun doTestCut(path: String) {
doTestAction(IdeActions.ACTION_CUT, path)
}
protected fun doTestCopy(path: String) {
doTestAction(IdeActions.ACTION_COPY, path)
}
private fun doTestAction(cutOrCopy: String, path: String) {
myFixture.setTestDataPath(BASE_PATH)
val testFile = File(path)
val testFileName = testFile.getName()
configureByDependencyIfExists(testFileName.replace(".kt", ".dependency.kt"))
configureByDependencyIfExists(testFileName.replace(".kt", ".dependency.java"))
myFixture.configureByFile(testFileName)
myFixture.performEditorAction(cutOrCopy)
val toFileName = testFileName.replace(".kt", ".to.kt")
val toFile = configureToFile(toFileName)
performNotWriteEditorAction(IdeActions.ACTION_PASTE)
myFixture.checkResultByFile(testFileName.replace(".kt", ".expected.kt"))
if (!InTextDirectivesUtils.isDirectiveDefined(FileUtil.loadFile(testFile, true), ALLOW_UNRESOLVED_DIRECTIVE)) {
checkNoUnresolvedReferences(toFile)
}
}
private fun configureToFile(toFileName: String): JetFile {
if (File(BASE_PATH + "/" + toFileName).exists()) {
return myFixture.configureByFile(toFileName) as JetFile
}
else {
return myFixture.configureByText(toFileName, DEFAULT_TO_FILE_TEXT) as JetFile
}
}
private fun configureByDependencyIfExists(dependencyFileName: String) {
val file = File(BASE_PATH + "/" + dependencyFileName)
if (file.exists()) {
if (dependencyFileName.endsWith(".java")) {
//allow test framework to put it under right directory
myFixture.addClass(FileUtil.loadFile(file, true))
}
else {
myFixture.configureByFile(dependencyFileName)
}
}
}
private fun checkNoUnresolvedReferences(file: JetFile) {
val bindingContext = file.analyzeFully()
for (diagnostic in bindingContext.getDiagnostics()) {
if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) {
val textRanges = diagnostic.getTextRanges()
val diagnosticText = DefaultErrorMessages.render(diagnostic)
if (diagnostic.getPsiFile() == file) {
fail(diagnostic.getFactory().getName() + ": " + diagnosticText + " " + DiagnosticUtils.atLocation(file, textRanges.get(0)))
}
}
}
DebugInfoUtil.markDebugAnnotations(file, bindingContext, object : DebugInfoUtil.DebugInfoReporter() {
override fun preProcessReference(expression: JetReferenceExpression) {
expression.analyze(BodyResolveMode.FULL)
}
override fun reportElementWithErrorType(expression: JetReferenceExpression) {
//do nothing
}
override fun reportMissingUnresolved(expression: JetReferenceExpression) {
// this may happen if incorrect psi transformations are done
fail(expression.getText() + " is unresolved but not marked " + DiagnosticUtils.atLocation(file, expression.getTextRange()))
}
override fun reportUnresolvedWithTarget(expression: JetReferenceExpression, target: String) {
//do nothing
}
})
}
}