Code insight: "Generate..." actions for test framework support methods

#KT-9355 Fixed
This commit is contained in:
Alexey Sedunov
2015-09-30 17:26:59 +03:00
parent 5a325aeec0
commit 730cc7b551
45 changed files with 941 additions and 2 deletions
+1
View File
@@ -5,6 +5,7 @@
</ANNOTATIONS>
<CLASSES>
<root url="jar://$PROJECT_DIR$/ideaSDK/plugins/junit/lib/idea-junit.jar!/" />
<root url="jar://$PROJECT_DIR$/ideaSDK/plugins/junit/lib/resources_en.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
+1
View File
@@ -7,6 +7,7 @@
<root url="jar://$PROJECT_DIR$/ideaSDK/plugins/testng/lib/testng-plugin.jar!/" />
<root url="jar://$PROJECT_DIR$/ideaSDK/plugins/testng/lib/testng.jar!/" />
<root url="jar://$PROJECT_DIR$/ideaSDK/plugins/testng/lib/jcommander.jar!/" />
<root url="jar://$PROJECT_DIR$/ideaSDK/plugins/testng/lib/resources_en.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
@@ -83,7 +83,7 @@ public fun <K> Iterable<K>.mapToIndex(): Map<K, Int> {
return map
}
public fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this
public inline fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this
public fun <T: Any> emptyOrSingletonList(item: T?): List<T> = if (item == null) listOf() else listOf(item)
@@ -42,6 +42,7 @@ import org.jetbrains.kotlin.generators.tests.reservedWords.generateTestDataForRe
import org.jetbrains.kotlin.idea.AbstractExpressionSelectionTest
import org.jetbrains.kotlin.idea.AbstractSmartSelectionTest
import org.jetbrains.kotlin.idea.codeInsight.*
import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateActionTest
import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractCodeMoverTest
import org.jetbrains.kotlin.idea.codeInsight.surroundWith.AbstractSurroundWithTest
import org.jetbrains.kotlin.idea.codeInsight.unwrap.AbstractUnwrapRemoveTest
@@ -723,6 +724,10 @@ fun main(args: Array<String>) {
testClass<AbstractKDocTypingTest>() {
model("kdoc/typing")
}
testClass<AbstractGenerateActionTest>() {
model("codeInsight/generate")
}
}
testGroup("idea/tests", "compiler/testData") {
@@ -138,7 +138,7 @@ private fun generateFunction(project: Project, descriptor: FunctionDescriptor, b
return JetPsiFactory(project).createFunction(OVERRIDE_RENDERER.render(newDescriptor) + body)
}
private fun generateUnsupportedOrSuperCall(descriptor: CallableMemberDescriptor, bodyType: OverrideMemberChooserObject.BodyType): String {
public fun generateUnsupportedOrSuperCall(descriptor: CallableMemberDescriptor, bodyType: OverrideMemberChooserObject.BodyType): String {
if (bodyType == OverrideMemberChooserObject.BodyType.EMPTY) {
return "throw UnsupportedOperationException()"
}
+17
View File
@@ -161,6 +161,23 @@
description="Execute Kotlin code in console">
<keyboard-shortcut first-keystroke="control ENTER" keymap="$default"/>
</action>
<group id="KotlinGenerateGroup">
<action id="KotlinGenerateSetUpMethod"
class="org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp"
text="SetUp Function" />
<action id="KotlinGenerateTestMethod"
class="org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Test"
text="Test Function" />
<action id="KotlinGenerateDataMethod"
class="org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Data"
text="Parameters Function" />
<action id="KotlinGenerateTearDownMethod"
class="org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown"
text="TearDown Function" />
<add-to-group group-id="GenerateGroup" anchor="first"/>
</group>
</actions>
<extensions defaultExtensionNs="com.intellij">
@@ -0,0 +1,61 @@
/*
* 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.actions.generate
import com.intellij.codeInsight.CodeInsightActionHandler
import com.intellij.codeInsight.actions.CodeInsightAction
import com.intellij.lang.ContextAwareActionHandler
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.actionSystem.Presentation
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.idea.core.refactoring.canRefactor
import org.jetbrains.kotlin.psi.JetClassOrObject
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
abstract class KotlinGenerateActionBase() : CodeInsightAction() {
override fun update(
presentation: Presentation,
project: Project,
editor: Editor,
file: PsiFile,
dataContext: DataContext,
actionPlace: String?
) {
super.update(presentation, project, editor, file, dataContext, actionPlace)
val actionHandler = handler
if (actionHandler is ContextAwareActionHandler && presentation.isEnabled) {
presentation.isEnabled = actionHandler.isAvailableForQuickList(editor, file, dataContext)
}
}
override fun isValidForFile(project: Project, editor: Editor, file: PsiFile): Boolean {
if (file !is JetFile || file.isCompiled) return false
val targetClass = getTargetClass(editor, file) ?: return false
return targetClass.canRefactor() && isValidForClass(targetClass)
}
protected open fun getTargetClass(editor: Editor, file: PsiFile): JetClassOrObject? {
return file.findElementAt(editor.caretModel.offset)?.getNonStrictParentOfType<JetClassOrObject>()
}
protected abstract fun isValidForClass(targetClass: JetClassOrObject): Boolean
}
@@ -0,0 +1,227 @@
/*
* 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.actions.generate
import com.intellij.codeInsight.CodeInsightActionHandler
import com.intellij.codeInsight.generation.actions.GenerateActionPopupTemplateInjector
import com.intellij.codeInsight.hint.HintManager
import com.intellij.ide.fileTemplates.FileTemplateManager
import com.intellij.ide.fileTemplates.impl.AllFileTemplatesConfigurable
import com.intellij.lang.java.JavaLanguage
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.extensions.Extensions
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.InputValidator
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.ui.popup.PopupChooserBuilder
import com.intellij.openapi.util.io.FileUtil
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElementFactory
import com.intellij.psi.PsiFile
import com.intellij.testIntegration.JavaTestFramework
import com.intellij.testIntegration.TestFramework
import com.intellij.testIntegration.TestIntegrationUtils.MethodKind
import com.intellij.ui.components.JBList
import com.intellij.util.IncorrectOperationException
import com.intellij.util.SmartList
import org.jetbrains.kotlin.asJava.toLightClass
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.overrideImplement.OverrideMemberChooserObject.BodyType
import org.jetbrains.kotlin.idea.core.overrideImplement.generateUnsupportedOrSuperCall
import org.jetbrains.kotlin.idea.core.refactoring.j2k
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.setupEditorSelection
import org.jetbrains.kotlin.idea.quickfix.generateMember
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetClassOrObject
import org.jetbrains.kotlin.psi.JetNamedFunction
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.ifEmpty
abstract class KotlinGenerateTestSupportActionBase(
private val methodKind : MethodKind
) : KotlinGenerateActionBase(), GenerateActionPopupTemplateInjector {
companion object {
private fun findTargetClass(editor: Editor, file: PsiFile): JetClassOrObject? {
val elementAtCaret = file.findElementAt(editor.caretModel.offset) ?: return null
return elementAtCaret.parentsWithSelf.filterIsInstance<JetClassOrObject>().firstOrNull { !it.isLocal() }
}
private fun findSuitableFrameworks(klass: JetClassOrObject): List<TestFramework> {
val lightClass = klass.toLightClass() ?: return emptyList()
val frameworks = Extensions.getExtensions(TestFramework.EXTENSION_NAME).filter { it.language == JavaLanguage.INSTANCE }
return frameworks.firstOrNull { it.isTestClass(lightClass) }?.let { listOf(it) }
?: frameworks.filterTo(SmartList<TestFramework>()) { it.isPotentialTestClass(lightClass) }
}
private fun chooseAndPerform(editor: Editor, frameworks: List<TestFramework>, consumer: (TestFramework) -> Unit) {
frameworks.ifEmpty { return }
frameworks.singleOrNull()?.let { return consumer(it) }
if (ApplicationManager.getApplication().isUnitTestMode) return consumer(frameworks.first())
val list = JBList(*frameworks.toTypedArray())
list.cellRenderer = TestFrameworkListCellRenderer()
PopupChooserBuilder(list)
.setFilteringEnabled { (it as TestFramework).name }
.setTitle("Choose Framework")
.setItemChoosenCallback { consumer(list.selectedValue as TestFramework) }
.setMovable(true)
.createPopup()
.showInBestPositionFor(editor)
}
private val BODY_VAR = "\${BODY}"
private val NAME_VAR = "\${NAME}"
private val NAME_VALIDATOR = object : InputValidator {
override fun checkInput(inputString: String) = KotlinNameSuggester.isIdentifier(inputString)
override fun canClose(inputString: String) = true
}
}
public class SetUp : KotlinGenerateTestSupportActionBase(MethodKind.SET_UP) {
override fun isApplicableTo(framework: TestFramework, targetClass: JetClassOrObject): Boolean {
return framework.findSetUpMethod(targetClass.toLightClass()!!) == null
}
}
public class Test : KotlinGenerateTestSupportActionBase(MethodKind.TEST) {
override fun isApplicableTo(framework: TestFramework, targetClass: JetClassOrObject) = true
}
public class Data : KotlinGenerateTestSupportActionBase(MethodKind.DATA) {
override fun isApplicableTo(framework: TestFramework, targetClass: JetClassOrObject): Boolean {
if (framework !is JavaTestFramework) return false
return framework.findParametersMethod(targetClass.toLightClass()) == null
}
}
public class TearDown : KotlinGenerateTestSupportActionBase(MethodKind.TEAR_DOWN) {
override fun isApplicableTo(framework: TestFramework, targetClass: JetClassOrObject): Boolean {
return framework.findTearDownMethod(targetClass.toLightClass()!!) == null
}
}
private inner class HandlerImpl : CodeInsightActionHandler {
override fun startInWriteAction() = false
override fun invoke(project: Project, editor: Editor, file: PsiFile) {
val klass = findTargetClass(editor, file) ?: return
val frameworks = findSuitableFrameworks(klass)
.filter { methodKind.getFileTemplateDescriptor(it) != null && isApplicableTo(it, klass) }
chooseAndPerform(editor, frameworks) { doGenerate(editor, file, klass, it) }
}
private fun doGenerate(editor: Editor, file: PsiFile, klass: JetClassOrObject, framework: TestFramework) {
val project = file.project
val commandName = "Generate test function"
project.executeWriteCommand(commandName) {
try {
PsiDocumentManager.getInstance(project).commitAllDocuments()
val fileTemplateDescriptor = methodKind.getFileTemplateDescriptor(framework)
val fileTemplate = FileTemplateManager.getInstance(project).getCodeTemplate(fileTemplateDescriptor.fileName)
var templateText = fileTemplate.text.replace(BODY_VAR, "")
if (templateText.contains(NAME_VAR)) {
var name = "Name"
if (!ApplicationManager.getApplication().isUnitTestMode) {
name = Messages.showInputDialog("Choose test name: ", commandName, null, name, NAME_VALIDATOR)
?: return@executeWriteCommand
}
templateText = fileTemplate.text.replace(NAME_VAR, name)
}
val factory = PsiElementFactory.SERVICE.getInstance(project)
val psiMethod = factory.createMethodFromText(templateText, null)
psiMethod.throwsList.referenceElements.forEach { it.delete() }
val function = psiMethod.j2k() as? JetNamedFunction
if (function == null) {
HintManager.getInstance().showErrorHint(editor, "Couldn't convert Java template to Kotlin")
return@executeWriteCommand
}
val functionInPlace = generateMember(editor, klass, function)
val functionDescriptor = functionInPlace.resolveToDescriptor() as FunctionDescriptor
val overriddenDescriptors = functionDescriptor.overriddenDescriptors
val bodyText = when (overriddenDescriptors.size()) {
0 -> generateUnsupportedOrSuperCall(functionDescriptor, BodyType.EMPTY)
1 -> generateUnsupportedOrSuperCall(overriddenDescriptors.single(), BodyType.SUPER)
else -> generateUnsupportedOrSuperCall(overriddenDescriptors.first(), BodyType.QUALIFIED_SUPER)
}
functionInPlace.bodyExpression?.delete()
functionInPlace.add(JetPsiFactory(project).createBlock(bodyText))
if (overriddenDescriptors.isNotEmpty()) {
functionInPlace.addModifier(JetTokens.OVERRIDE_KEYWORD)
}
setupEditorSelection(editor, functionInPlace)
}
catch (e: IncorrectOperationException) {
HintManager.getInstance().showErrorHint(editor, "Cannot generate method: " + e.getMessage())
}
}
}
}
private val handler = HandlerImpl()
override fun getHandler() = handler
override fun getTargetClass(editor: Editor, file: PsiFile): JetClassOrObject? {
return findTargetClass(editor, file)
}
override fun isValidForClass(targetClass: JetClassOrObject): Boolean {
return findSuitableFrameworks(targetClass).any { isApplicableTo(it, targetClass) }
}
protected abstract fun isApplicableTo(framework: TestFramework, targetClass: JetClassOrObject): Boolean
override fun createEditTemplateAction(dataContext: DataContext): AnAction? {
val project = CommonDataKeys.PROJECT.getData(dataContext) ?: return null
val editor = CommonDataKeys.EDITOR.getData(dataContext) ?: return null
val file = CommonDataKeys.PSI_FILE.getData(dataContext) ?: return null
val targetClass = getTargetClass(editor, file) ?: return null
val frameworks = findSuitableFrameworks(targetClass).ifEmpty { return null }
return object : AnAction("Edit Template") {
override fun actionPerformed(e: AnActionEvent) {
chooseAndPerform(editor, frameworks) {
val descriptor = methodKind.getFileTemplateDescriptor(it)
if (descriptor == null) {
HintManager.getInstance().showErrorHint(editor, "No template found for ${it.name}:${templatePresentation.text}")
return@chooseAndPerform
}
AllFileTemplatesConfigurable.editCodeTemplate(FileUtil.getNameWithoutExtension(descriptor.fileName), project)
}
}
}
}
}
@@ -0,0 +1,35 @@
/*
* 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.actions.generate;
import com.intellij.testIntegration.TestFramework;
import javax.swing.*;
import java.awt.*;
public class TestFrameworkListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component result = super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus);
if (value == null) return result;
TestFramework framework = (TestFramework) value;
setIcon(framework.getIcon());
setText(framework.getName());
return result;
}
}
@@ -0,0 +1,5 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Data
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
class A {<caret>
}
@@ -0,0 +1,10 @@
import org.junit.runners.Parameterized
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Data
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
class A {
@Parameterized.Parameters
fun data(): Collection<Array<Any>> {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,5 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
class A {<caret>
}
@@ -0,0 +1,10 @@
import org.junit.Before
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
class A {
@Before
fun setUp() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,10 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import org.junit.Before
class A {<caret>
@Before
fun setUp() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,15 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import org.junit.Before
class A {
@org.testng.annotations.BeforeMethod
fun setUp() {
throw UnsupportedOperationException()
}
@Before
fun setUp() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,11 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
open class A {
open fun setUp() {
}
}
class B : A() {<caret>
}
@@ -0,0 +1,16 @@
import org.junit.Before
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
open class A {
open fun setUp() {
}
}
class B : A() {
@Before
override fun setUp() {
super.setUp()
}
}
@@ -0,0 +1,5 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
class A {<caret>
}
@@ -0,0 +1,10 @@
import org.junit.After
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
class A {
@After
fun tearDown() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,10 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import org.junit.After
class A {<caret>
@After
fun tearDown() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,15 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import org.junit.After
class A {
@org.testng.annotations.AfterMethod
fun tearDown() {
throw UnsupportedOperationException()
}
@After
fun tearDown() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,5 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Test
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
class A {<caret>
}
@@ -0,0 +1,10 @@
import org.junit.Test
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Test
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
class A {
@Test
fun testName() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,7 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import junit.framework.TestCase
class A : TestCase() {<caret>
}
@@ -0,0 +1,9 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import junit.framework.TestCase
class A : TestCase() {
override fun setUp() {
super.setUp()
}
}
@@ -0,0 +1,10 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// NOT_APPLICABLE
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import junit.framework.TestCase
class A : TestCase() {<caret>
override fun setUp() {
super.setUp()
}
}
@@ -0,0 +1,7 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import junit.framework.TestCase
class A : TestCase() {<caret>
}
@@ -0,0 +1,9 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import junit.framework.TestCase
class A : TestCase() {
override fun tearDown() {
super.tearDown()
}
}
@@ -0,0 +1,10 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// NOT_APPLICABLE
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import junit.framework.TestCase
class A : TestCase() {<caret>
override fun tearDown() {
super.tearDown()
}
}
@@ -0,0 +1,7 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Test
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import junit.framework.TestCase
class A : TestCase() {<caret>
}
@@ -0,0 +1,9 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Test
// CONFIGURE_LIBRARY: JUnit@lib/junit-4.12.jar
import junit.framework.TestCase
class A : TestCase() {
fun testName() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,7 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Data
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.Test
@Test class A {<caret>
}
@@ -0,0 +1,11 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Data
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.DataProvider
import org.testng.annotations.Test
@Test class A {
@DataProvider(name = "Name")
fun Name(): Array<Array<Any>> {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,7 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.Test
@Test class A {<caret>
}
@@ -0,0 +1,11 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test
@Test class A {
@BeforeMethod
fun setUp() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,12 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// NOT_APPLICABLE
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test
@Test class A {<caret>
@BeforeMethod
fun setUp() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,13 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.Test
open class A {
open fun setUp() {
}
}
@Test class B : A() {<caret>
}
@@ -0,0 +1,17 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$SetUp
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test
open class A {
open fun setUp() {
}
}
@Test class B : A() {
@BeforeMethod
override fun setUp() {
super.setUp()
}
}
@@ -0,0 +1,7 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.Test
@Test class A {<caret>
}
@@ -0,0 +1,11 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.AfterMethod
import org.testng.annotations.Test
@Test class A {
@AfterMethod
fun tearDown() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,12 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$TearDown
// NOT_APPLICABLE
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.AfterMethod
import org.testng.annotations.Test
@Test class A {<caret>
@AfterMethod
fun tearDown() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,7 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Test
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.Test
@Test class A {<caret>
}
@@ -0,0 +1,10 @@
// ACTION_CLASS: org.jetbrains.kotlin.idea.actions.generate.KotlinGenerateTestSupportActionBase$Test
// CONFIGURE_LIBRARY: TestNG@plugins/testng/lib/testng.jar
import org.testng.annotations.Test
@Test class A {
@Test
fun testName() {
throw UnsupportedOperationException()
}
}
@@ -0,0 +1,75 @@
/*
* 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.generate
import com.intellij.codeInsight.actions.CodeInsightAction
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.util.io.FileUtil
import com.intellij.testFramework.PlatformTestUtil
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil
import org.jetbrains.kotlin.idea.test.JetLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.JetWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import java.io.File
abstract class AbstractGenerateActionTest : JetLightCodeInsightFixtureTestCase() {
private fun setUpTestSourceRoot() {
val module = myModule
val model = ModuleRootManager.getInstance(module).modifiableModel
val entry = model.contentEntries.single()
val sourceFolderFile = entry.sourceFolderFiles.single()
entry.removeSourceFolder(entry.sourceFolders.single())
entry.addSourceFolder(sourceFolderFile, true)
runWriteAction {
model.commit()
module.project.save()
}
}
protected fun doTest(path: String) {
setUpTestSourceRoot()
val fileText = FileUtil.loadFile(File(path), true)
try {
ConfigLibraryUtil.configureLibrariesByDirective(myModule, PlatformTestUtil.getCommunityPath(), fileText)
myFixture.configureByFile(path)
val actionClassName = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// ACTION_CLASS: ")
val action = Class.forName(actionClassName).newInstance() as CodeInsightAction
val isApplicableExpected = !InTextDirectivesUtils.isDirectiveDefined(fileText, "// NOT_APPLICABLE")
val presentation = myFixture.testAction(action)
TestCase.assertEquals(isApplicableExpected, presentation.isEnabled)
if (isApplicableExpected) {
val afterFile = File(path + ".after")
TestCase.assertTrue(afterFile.exists())
myFixture.checkResult(FileUtil.loadFile(afterFile, true))
}
}
finally {
ConfigLibraryUtil.unconfigureLibrariesByDirective(myModule, fileText)
}
}
override fun getProjectDescriptor() = JetWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
}
@@ -0,0 +1,187 @@
/*
* 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.generate;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.JetTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("idea/testData/codeInsight/generate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class GenerateActionTestGenerated extends AbstractGenerateActionTest {
public void testAllFilesPresentInGenerate() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/generate"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("idea/testData/codeInsight/generate/testFrameworkSupport")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class TestFrameworkSupport extends AbstractGenerateActionTest {
public void testAllFilesPresentInTestFrameworkSupport() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/generate/testFrameworkSupport"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class JUnit4 extends AbstractGenerateActionTest {
public void testAllFilesPresentInJUnit4() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("dataMethod.kt")
public void testDataMethod() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4/dataMethod.kt");
doTest(fileName);
}
@TestMetadata("setUp.kt")
public void testSetUp() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4/setUp.kt");
doTest(fileName);
}
@TestMetadata("setUpExists.kt")
public void testSetUpExists() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4/setUpExists.kt");
doTest(fileName);
}
@TestMetadata("setUpOverrides.kt")
public void testSetUpOverrides() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4/setUpOverrides.kt");
doTest(fileName);
}
@TestMetadata("tearDown.kt")
public void testTearDown() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4/tearDown.kt");
doTest(fileName);
}
@TestMetadata("tearDownExists.kt")
public void testTearDownExists() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4/tearDownExists.kt");
doTest(fileName);
}
@TestMetadata("testMethod.kt")
public void testTestMethod() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/jUnit4/testMethod.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/junit3")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Junit3 extends AbstractGenerateActionTest {
public void testAllFilesPresentInJunit3() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/generate/testFrameworkSupport/junit3"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("setUp.kt")
public void testSetUp() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/junit3/setUp.kt");
doTest(fileName);
}
@TestMetadata("setUpExists.kt")
public void testSetUpExists() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/junit3/setUpExists.kt");
doTest(fileName);
}
@TestMetadata("tearDown.kt")
public void testTearDown() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/junit3/tearDown.kt");
doTest(fileName);
}
@TestMetadata("tearDownExists.kt")
public void testTearDownExists() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/junit3/tearDownExists.kt");
doTest(fileName);
}
@TestMetadata("testMethod.kt")
public void testTestMethod() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/junit3/testMethod.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/testNG")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class TestNG extends AbstractGenerateActionTest {
public void testAllFilesPresentInTestNG() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/generate/testFrameworkSupport/testNG"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("dataMethod.kt")
public void testDataMethod() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/testNG/dataMethod.kt");
doTest(fileName);
}
@TestMetadata("setUp.kt")
public void testSetUp() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/testNG/setUp.kt");
doTest(fileName);
}
@TestMetadata("setUpExists.kt")
public void testSetUpExists() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/testNG/setUpExists.kt");
doTest(fileName);
}
@TestMetadata("setUpOverrides.kt")
public void testSetUpOverrides() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/testNG/setUpOverrides.kt");
doTest(fileName);
}
@TestMetadata("tearDown.kt")
public void testTearDown() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/testNG/tearDown.kt");
doTest(fileName);
}
@TestMetadata("tearDownExists.kt")
public void testTearDownExists() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/testNG/tearDownExists.kt");
doTest(fileName);
}
@TestMetadata("testMethod.kt")
public void testTestMethod() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/codeInsight/generate/testFrameworkSupport/testNG/testMethod.kt");
doTest(fileName);
}
}
}
}