idea: cleanup code

This commit is contained in:
Dmitry Gridin
2019-12-13 22:11:25 +07:00
parent e77d8657f4
commit 8dbbd64beb
897 changed files with 13161 additions and 18514 deletions
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.test
@@ -39,15 +28,15 @@ object AstAccessControl {
}
fun testWithControlledAccessToAst(
shouldFail: Boolean, allowedFile: VirtualFile,
project: Project, disposable: Disposable, testBody: () -> Unit
shouldFail: Boolean, allowedFile: VirtualFile,
project: Project, disposable: Disposable, testBody: () -> Unit
) {
testWithControlledAccessToAst(shouldFail, listOf(allowedFile), project, disposable, testBody)
}
fun testWithControlledAccessToAst(
shouldFail: Boolean, allowedFiles: List<VirtualFile>,
project: Project, disposable: Disposable, testBody: () -> Unit
shouldFail: Boolean, allowedFiles: List<VirtualFile>,
project: Project, disposable: Disposable, testBody: () -> Unit
) {
val filter = wrapWithDirectiveAllow { file ->
file.fileType != KotlinFileType.INSTANCE || file in allowedFiles
@@ -56,15 +45,12 @@ object AstAccessControl {
execute(shouldFail, project, disposable, filter, testBody)
}
fun wrapWithDirectiveAllow(allowedFiles: (VirtualFile) -> Boolean): (VirtualFile) -> Boolean {
return { file ->
if (allowedFiles(file)) {
false
}
else {
val text = VfsUtilCore.loadText(file)
!InTextDirectivesUtils.isDirectiveDefined(text, ALLOW_AST_ACCESS_DIRECTIVE)
}
fun wrapWithDirectiveAllow(allowedFiles: (VirtualFile) -> Boolean): (VirtualFile) -> Boolean = { file ->
if (allowedFiles(file)) {
false
} else {
val text = VfsUtilCore.loadText(file)
!InTextDirectivesUtils.isDirectiveDefined(text, ALLOW_AST_ACCESS_DIRECTIVE)
}
}
@@ -72,8 +58,10 @@ object AstAccessControl {
return execute(shouldFail, fixture.project, disposable, { file -> file != fixture.file.virtualFile }, testBody)
}
private fun <T : Any> execute(shouldFail: Boolean, project: Project, disposable: Disposable,
forbidAstAccessFilter: (VirtualFile) -> Boolean, testBody: () -> T): T? {
private fun <T : Any> execute(
shouldFail: Boolean, project: Project, disposable: Disposable,
forbidAstAccessFilter: (VirtualFile) -> Boolean, testBody: () -> T
): T? {
val manager = (PsiManager.getInstance(project) as PsiManagerImpl)
manager.setAssertOnFileLoadingFilter(VirtualFileFilter { file -> forbidAstAccessFilter(file) }, disposable)
@@ -81,20 +69,20 @@ object AstAccessControl {
try {
val result = testBody()
if (shouldFail) {
fail("This failure means that that a test that should fail (by triggering ast switch) in fact did not.\n" +
"This could happen for the following reasons:\n" +
"1. This kind of operation no longer trigger ast switch, choose better indicator test case." +
"2. Test is now misconfigured and no longer checks for ast switch, reconfigure the test.")
fail(
"This failure means that that a test that should fail (by triggering ast switch) in fact did not.\n" +
"This could happen for the following reasons:\n" +
"1. This kind of operation no longer trigger ast switch, choose better indicator test case." +
"2. Test is now misconfigured and no longer checks for ast switch, reconfigure the test."
)
}
return result
}
catch (e: Throwable) {
} catch (e: Throwable) {
if (!shouldFail) {
throw e
}
}
finally {
} finally {
manager.setAssertOnFileLoadingFilter(VirtualFileFilter.NONE, disposable)
}
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.test
@@ -43,10 +32,10 @@ import kotlin.test.assertNotNull
* Helper for configuring kotlin runtime in tested project.
*/
object ConfigLibraryUtil {
private val DEFAULT_JAVA_RUNTIME_LIB_NAME = "JAVA_RUNTIME_LIB_NAME"
private val DEFAULT_KOTLIN_TEST_LIB_NAME = "KOTLIN_TEST_LIB_NAME"
private val DEFAULT_KOTLIN_JS_STDLIB_NAME = "KOTLIN_JS_STDLIB_NAME"
private val DEFAULT_KOTLIN_COMMON_STDLIB_NAME = "KOTLIN_COMMON_STDLIB_NAME"
private const val DEFAULT_JAVA_RUNTIME_LIB_NAME = "JAVA_RUNTIME_LIB_NAME"
private const val DEFAULT_KOTLIN_TEST_LIB_NAME = "KOTLIN_TEST_LIB_NAME"
private const val DEFAULT_KOTLIN_JS_STDLIB_NAME = "KOTLIN_JS_STDLIB_NAME"
private const val DEFAULT_KOTLIN_COMMON_STDLIB_NAME = "KOTLIN_COMMON_STDLIB_NAME"
private fun getKotlinRuntimeLibEditor(libName: String, library: File): NewLibraryEditor {
val editor = NewLibraryEditor()
@@ -63,9 +52,13 @@ object ConfigLibraryUtil {
fun configureKotlinJsRuntimeAndSdk(module: Module, sdk: Sdk) {
configureSdk(module, sdk)
addLibrary(getKotlinRuntimeLibEditor(DEFAULT_KOTLIN_JS_STDLIB_NAME,
PathUtil.kotlinPathsForDistDirectory.jsStdLibJarPath), module,
JSLibraryKind)
addLibrary(
getKotlinRuntimeLibEditor(
DEFAULT_KOTLIN_JS_STDLIB_NAME,
PathUtil.kotlinPathsForDistDirectory.jsStdLibJarPath
), module,
JSLibraryKind
)
}
fun configureKotlinCommonRuntime(module: Module) {
@@ -222,7 +215,7 @@ object ConfigLibraryUtil {
}
}
if (!libraryNames.isEmpty()) throw AssertionError("Couldn't find the following libraries: " + libraryNames)
if (libraryNames.isNotEmpty()) throw AssertionError("Couldn't find the following libraries: " + libraryNames)
}
fun configureLibrariesByDirective(module: Module, rootPath: String, fileText: String) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2000-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -48,9 +48,11 @@ object DirectiveBasedActionUtils {
val actualActions = availableActions.map { it.text }.sorted()
UsefulTestCase.assertOrderedEquals("Some unexpected actions available at current position. Use // ACTION: directive",
filterOutIrrelevantActions(actualActions),
expectedActions)
UsefulTestCase.assertOrderedEquals(
"Some unexpected actions available at current position. Use // ACTION: directive",
filterOutIrrelevantActions(actualActions),
expectedActions
)
}
//TODO: hack, implemented because irrelevant actions behave in different ways on build server and locally
@@ -62,26 +64,26 @@ object DirectiveBasedActionUtils {
private fun isIrrelevantAction(action: String) = action.isEmpty() || IRRELEVANT_ACTION_PREFIXES.any { action.startsWith(it) }
private val IRRELEVANT_ACTION_PREFIXES = listOf(
"Disable ",
"Edit intention settings",
"Edit inspection profile setting",
"Inject language or reference",
"Suppress '",
"Run inspection on",
"Inspection '",
"Suppress for ",
"Suppress all ",
"Edit cleanup profile settings",
"Fix all '",
"Cleanup code",
"Go to ",
"Show local variable type hints",
"Show function return type hints",
"Show property type hints",
"Show parameter type hints",
"Show argument name hints",
"Show hints for suspending calls",
"Add 'JUnit",
"Add 'testng"
"Disable ",
"Edit intention settings",
"Edit inspection profile setting",
"Inject language or reference",
"Suppress '",
"Run inspection on",
"Inspection '",
"Suppress for ",
"Suppress all ",
"Edit cleanup profile settings",
"Fix all '",
"Cleanup code",
"Go to ",
"Show local variable type hints",
"Show function return type hints",
"Show property type hints",
"Show parameter type hints",
"Show argument name hints",
"Show hints for suspending calls",
"Add 'JUnit",
"Add 'testng"
)
}
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.test
@@ -32,19 +21,21 @@ abstract class KotlinLightJava9ModulesCodeInsightFixtureTestCase : KotlinLightCo
}
protected fun addFile(path: String, text: String, module: ModuleDescriptor = ModuleDescriptor.MAIN): VirtualFile =
VfsTestUtil.createFile(module.root(), path, text)
VfsTestUtil.createFile(module.root(), path, text)
protected fun addKotlinFile(path: String, @Language("kotlin") text: String, module: ModuleDescriptor = ModuleDescriptor.MAIN): VirtualFile =
addFile(path, text.toTestData(), module)
protected fun addKotlinFile(
path: String,
@Language("kotlin") text: String,
module: ModuleDescriptor = ModuleDescriptor.MAIN
): VirtualFile = addFile(path, text.toTestData(), module)
protected fun addJavaFile(path: String, @Language("java") text: String, module: ModuleDescriptor = ModuleDescriptor.MAIN): VirtualFile =
addFile(path, text.toTestData(), module)
addFile(path, text.toTestData(), module)
protected fun moduleInfo(@Language("JAVA") text: String, module: ModuleDescriptor = ModuleDescriptor.MAIN) =
addFile("module-info.java", text.toTestData(), module)
addFile("module-info.java", text.toTestData(), module)
protected fun checkModuleInfo(@Language("JAVA") text: String) =
myFixture.checkResult("module-info.java", text.toTestData(), false)
protected fun checkModuleInfo(@Language("JAVA") text: String) = myFixture.checkResult("module-info.java", text.toTestData(), false)
}
private const val IDENTIFIER_CARET = "CARET"
@@ -53,4 +44,4 @@ private const val COMMENT_CARET = "/*CARET*/"
private val ADDITIONAL_CARET_MARKERS = arrayOf(IDENTIFIER_CARET, COMMENT_CARET_CHAR, COMMENT_CARET)
private fun String.toTestData(): String =
ADDITIONAL_CARET_MARKERS.fold(trimIndent()) { result, marker -> result.replace(marker, EditorTestUtil.CARET_TAG, ignoreCase = true) }
ADDITIONAL_CARET_MARKERS.fold(trimIndent()) { result, marker -> result.replace(marker, EditorTestUtil.CARET_TAG, ignoreCase = true) }
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.test
@@ -25,7 +14,7 @@ import org.jetbrains.kotlin.test.TestMetadata
import java.io.File
import kotlin.reflect.full.findAnnotation
abstract class KotlinLightPlatformCodeInsightFixtureTestCase: LightPlatformCodeInsightFixtureTestCase() {
abstract class KotlinLightPlatformCodeInsightFixtureTestCase : LightPlatformCodeInsightFixtureTestCase() {
override fun setUp() {
super.setUp()
(StartupManager.getInstance(project) as StartupManagerImpl).runPostStartupActivities()
@@ -1,17 +1,6 @@
/*
* 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.test
@@ -43,20 +32,22 @@ abstract class KotlinMultiFileTestCase : MultiFileTestCase() {
}
}
protected fun getTestDirName(lowercaseFirstLetter : Boolean) : String {
protected fun getTestDirName(lowercaseFirstLetter: Boolean): String {
val testName = getTestName(lowercaseFirstLetter)
val endIndex = testName.lastIndexOf('_')
if (endIndex < 0) return testName
return testName.substring(0, endIndex).replace('_', '/')
}
protected fun doTestCommittingDocuments(action : (VirtualFile, VirtualFile?) -> Unit) {
super.doTest({ rootDir, rootAfter ->
action(rootDir, rootAfter)
protected fun doTestCommittingDocuments(action: (VirtualFile, VirtualFile?) -> Unit) {
super.doTest(
{ rootDir, rootAfter ->
action(rootDir, rootAfter)
PsiDocumentManager.getInstance(project!!).commitAllDocuments()
FileDocumentManager.getInstance().saveAllDocuments()
}, getTestDirName(true))
PsiDocumentManager.getInstance(project!!).commitAllDocuments()
FileDocumentManager.getInstance().saveAllDocuments()
}, getTestDirName(true)
)
}
override fun prepareProject(rootDir: VirtualFile) {
@@ -64,22 +55,21 @@ abstract class KotlinMultiFileTestCase : MultiFileTestCase() {
val model = ModuleManager.getInstance(project).modifiableModel
VfsUtilCore.visitChildrenRecursively(
rootDir,
object : VirtualFileVisitor<Any>() {
override fun visitFile(file: VirtualFile): Boolean {
if (!file.isDirectory && file.name.endsWith(ModuleFileType.DOT_DEFAULT_EXTENSION)) {
model.loadModule(file.path)
return false
}
return true
rootDir,
object : VirtualFileVisitor<Any>() {
override fun visitFile(file: VirtualFile): Boolean {
if (!file.isDirectory && file.name.endsWith(ModuleFileType.DOT_DEFAULT_EXTENSION)) {
model.loadModule(file.path)
return false
}
return true
}
}
)
runWriteAction { model.commit() }
}
else {
} else {
PsiTestUtil.addSourceContentToRoots(myModule, rootDir)
}
}
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.test
@@ -58,10 +47,9 @@ object KotlinMultiModuleJava9ProjectDescriptor : DefaultLightProjectDescriptor()
M7("${TEST_MODULE_NAME}_m7", "src_m7");
fun root(): VirtualFile =
if (this == MAIN) LightPlatformTestCase.getSourceRoot() else TempFileSystem.getInstance().findFileByPath("/$rootName")!!
if (this == MAIN) LightPlatformTestCase.getSourceRoot() else TempFileSystem.getInstance().findFileByPath("/$rootName")!!
fun testRoot(): VirtualFile? =
if (this == MAIN) TempFileSystem.getInstance().findFileByPath("/test_src")!! else null
fun testRoot(): VirtualFile? = if (this == MAIN) TempFileSystem.getInstance().findFileByPath("/test_src")!! else null
}
override fun getSdk(): Sdk = PluginTestCaseBase.jdk(TestJdkKind.FULL_JDK_9)
@@ -110,9 +98,9 @@ object KotlinMultiModuleJava9ProjectDescriptor : DefaultLightProjectDescriptor()
fun cleanupSourceRoots() = runWriteAction {
ModuleDescriptor.values().asSequence()
.filter { it != ModuleDescriptor.MAIN }
.flatMap { it.root().children.asSequence() }
.plus(ModuleDescriptor.MAIN.testRoot()!!.children.asSequence())
.forEach { it.delete(this) }
.filter { it != ModuleDescriptor.MAIN }
.flatMap { it.root().children.asSequence() }
.plus(ModuleDescriptor.MAIN.testRoot()!!.children.asSequence())
.forEach { it.delete(this) }
}
}