generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from jaksonlin/unittest-context
add unittest management context
- Loading branch information
Showing
55 changed files
with
3,086 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ain/kotlin/com/github/jaksonlin/pitestintellij/actions/GenerateAnnotationCommandAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.github.jaksonlin.pitestintellij.actions | ||
|
||
import com.github.jaksonlin.pitestintellij.commands.unittestannotations.GenerateAnnotationCommand | ||
import com.github.jaksonlin.pitestintellij.context.CaseCheckContext | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.actionSystem.CommonDataKeys | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiDocumentManager | ||
import com.intellij.psi.PsiMethod | ||
import com.intellij.psi.util.PsiTreeUtil | ||
|
||
|
||
class GenerateAnnotationCommandAction : AnAction() { | ||
|
||
override fun actionPerformed(e: AnActionEvent) { | ||
val psiMethodInfo = findMethodAtCaret(e) ?: return | ||
val context = CaseCheckContext.create(psiMethodInfo.first, psiMethodInfo.second) | ||
GenerateAnnotationCommand(e.project!!, context).execute() | ||
} | ||
|
||
private fun findMethodAtCaret(e: AnActionEvent): Pair<PsiMethod, PsiClass>? { | ||
val project = e.project ?: return null | ||
val editor = e.dataContext.getData(CommonDataKeys.EDITOR) ?: return null | ||
val caret = e.dataContext.getData(CommonDataKeys.CARET) ?: return null | ||
val elementAtCaret = PsiDocumentManager.getInstance(project) | ||
.getPsiFile(editor.document)?.findElementAt(caret.offset) ?: return null | ||
val method = PsiTreeUtil.getParentOfType(elementAtCaret, PsiMethod::class.java) ?: return null | ||
val containingClass = PsiTreeUtil.getParentOfType(method, PsiClass::class.java) ?: return null | ||
return method to containingClass | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/com/github/jaksonlin/pitestintellij/actions/RunCaseAnnoationCheckAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.jaksonlin.pitestintellij.actions | ||
|
||
import com.github.jaksonlin.pitestintellij.commands.unittestannotations.CheckAnnotationCommand | ||
import com.github.jaksonlin.pitestintellij.context.CaseCheckContext | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.actionSystem.CommonDataKeys | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiDocumentManager | ||
import com.intellij.psi.PsiMethod | ||
import com.intellij.psi.util.PsiTreeUtil | ||
|
||
class RunCaseAnnoationCheckAction : AnAction() { | ||
|
||
override fun actionPerformed(e: AnActionEvent) { | ||
val psiMethodInfo = findMethodAtCaret(e) ?: return | ||
val context = CaseCheckContext.create(psiMethodInfo.first, psiMethodInfo.second) | ||
CheckAnnotationCommand(e.project!!, context).execute() | ||
} | ||
|
||
private fun findMethodAtCaret(e: AnActionEvent): Pair<PsiMethod, PsiClass>? { | ||
val project = e.project ?: return null | ||
val editor = e.dataContext.getData(CommonDataKeys.EDITOR) ?: return null | ||
val caret = e.dataContext.getData(CommonDataKeys.CARET) ?: return null | ||
val elementAtCaret = PsiDocumentManager.getInstance(project) | ||
.getPsiFile(editor.document)?.findElementAt(caret.offset) ?: return null | ||
val method = PsiTreeUtil.getParentOfType(elementAtCaret, PsiMethod::class.java) ?: return null | ||
val containingClass = PsiTreeUtil.getParentOfType(method, PsiClass::class.java) ?: return null | ||
return method to containingClass | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...ain/kotlin/com/github/jaksonlin/pitestintellij/actions/RunTestFileAnnoationCheckAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.github.jaksonlin.pitestintellij.actions | ||
|
||
import com.github.jaksonlin.pitestintellij.commands.unittestannotations.CheckAnnotationCommand | ||
import com.github.jaksonlin.pitestintellij.context.CaseCheckContext | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.actionSystem.CommonDataKeys | ||
import com.intellij.psi.JavaRecursiveElementVisitor | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiJavaFile | ||
import com.intellij.psi.PsiMethod | ||
import com.intellij.psi.util.PsiTreeUtil | ||
|
||
class RunTestFileAnnoationCheckAction: AnAction() { | ||
|
||
override fun actionPerformed(e: AnActionEvent) { | ||
batchCheckAnnotation(e) | ||
} | ||
|
||
private fun batchCheckAnnotation(e: AnActionEvent){ | ||
val psiFile = e.dataContext.getData(CommonDataKeys.PSI_FILE) | ||
val psiJavaFile = psiFile as PsiJavaFile | ||
|
||
psiJavaFile.accept(object : JavaRecursiveElementVisitor() { | ||
override fun visitMethod(method: PsiMethod) { | ||
super.visitMethod(method) | ||
// inspect the method annotations | ||
val annotations = method.annotations | ||
for (annotation in annotations) { | ||
// inspect the annotation | ||
val annotationName = annotation.qualifiedName | ||
if (annotationName!=null && // to support junit 4 & 5, do not use regexp, as it will also match some beforeTest/afterTest annotations | ||
(annotationName == "org.junit.Test" || annotationName == "org.junit.jupiter.api.Test" || annotationName == "Test")) { | ||
val psiClass = PsiTreeUtil.getParentOfType(method, PsiClass::class.java) ?: return | ||
val context = CaseCheckContext.create(method, psiClass) | ||
CheckAnnotationCommand(e.project!!, context).execute() | ||
break | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/github/jaksonlin/pitestintellij/annotations/AnnotationFieldConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.github.jaksonlin.pitestintellij.annotations | ||
|
||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.JsonClassDiscriminator | ||
|
||
@Serializable | ||
sealed class DefaultValue { | ||
@Serializable | ||
@SerialName("StringValue") | ||
data class StringValue(val value: String) : DefaultValue() | ||
|
||
@Serializable | ||
@SerialName("StringListValue") | ||
data class StringListValue(val value: List<String>) : DefaultValue() | ||
|
||
@Serializable | ||
@SerialName("NullValue") | ||
object NullValue : DefaultValue() | ||
} | ||
|
||
|
||
@Serializable | ||
data class AnnotationFieldConfig( | ||
val name: String, | ||
val type: AnnotationFieldType, | ||
val required: Boolean = false, | ||
val defaultValue: DefaultValue = DefaultValue.NullValue, | ||
val validation: FieldValidation? = null, | ||
val valueProvider: ValueProvider? = null | ||
) |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/github/jaksonlin/pitestintellij/annotations/AnnotationFieldType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.jaksonlin.pitestintellij.annotations | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
enum class AnnotationFieldType { | ||
STRING, | ||
STRING_LIST | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/com/github/jaksonlin/pitestintellij/annotations/AnnotationParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.github.jaksonlin.pitestintellij.annotations | ||
|
||
import com.github.jaksonlin.pitestintellij.context.UnittestCase | ||
|
||
|
||
class AnnotationParser(private val schema: AnnotationSchema) { | ||
private val validator = AnnotationValidator(schema) | ||
|
||
fun parseAnnotation(annotationValues: Map<String, Any>): UnittestCase { | ||
when (val result = validator.validate(annotationValues)) { | ||
is AnnotationValidator.ValidationResult.Valid -> { | ||
val parsedValues = schema.fields.associate { field -> | ||
if (field.required) { | ||
if (!annotationValues.containsKey(field.name)) { | ||
throw IllegalArgumentException("Missing required field: ${field.name}") | ||
} | ||
if (annotationValues[field.name] == null) { | ||
throw IllegalArgumentException("Required field cannot be null: ${field.name}") | ||
} | ||
} else { | ||
if (!annotationValues.containsKey(field.name) || annotationValues[field.name] == null) { | ||
return@associate field.name to field.defaultValue | ||
} | ||
} | ||
val rawValue = annotationValues[field.name] | ||
field.name to convertValue(rawValue, field) | ||
} | ||
return UnittestCase(parsedValues) | ||
} | ||
is AnnotationValidator.ValidationResult.Invalid -> { | ||
throw IllegalArgumentException( | ||
"Invalid annotation values:\n${result.errors.joinToString("\n")}" | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun convertValue(value: Any?, field: AnnotationFieldConfig): Any? { | ||
if (value == null) { | ||
return when (val defaultValue = field.defaultValue) { | ||
is DefaultValue.StringValue -> defaultValue.value | ||
is DefaultValue.StringListValue -> defaultValue.value | ||
DefaultValue.NullValue -> null | ||
} | ||
} | ||
|
||
return when (field.type) { | ||
AnnotationFieldType.STRING -> value as? String ?: field.defaultValue | ||
AnnotationFieldType.STRING_LIST -> (value as? List<*>)?.mapNotNull { it as? String } ?: emptyList<String>() | ||
} | ||
} | ||
} |
Oops, something went wrong.