diff --git a/build.gradle.kts b/build.gradle.kts index 03ca216..e536713 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "org.jetbrains.academy.test.system" -version = "2.1.2" +version = "2.1.3" allprojects { apply { diff --git a/ij/java-psi/src/main/kotlin/org/jetbrains/academy/test/system/java/ij/analyzer/PsiFileAnalyzerUtil.kt b/ij/java-psi/src/main/kotlin/org/jetbrains/academy/test/system/java/ij/analyzer/PsiFileAnalyzerUtil.kt index d579380..f1401ed 100644 --- a/ij/java-psi/src/main/kotlin/org/jetbrains/academy/test/system/java/ij/analyzer/PsiFileAnalyzerUtil.kt +++ b/ij/java-psi/src/main/kotlin/org/jetbrains/academy/test/system/java/ij/analyzer/PsiFileAnalyzerUtil.kt @@ -4,12 +4,7 @@ import com.intellij.ide.highlighter.JavaFileType import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.project.Project -import com.intellij.psi.PsiFile -import com.intellij.psi.PsiField -import com.intellij.psi.PsiMethod -import com.intellij.psi.PsiFileFactory -import com.intellij.psi.PsiLiteralExpression -import com.intellij.psi.PsiCodeBlock +import com.intellij.psi.* import org.jetbrains.academy.test.system.ij.analyzer.extractElementsOfTypes import org.jetbrains.academy.test.system.ij.analyzer.getBlockBody import org.jetbrains.academy.test.system.ij.analyzer.getConstValue @@ -56,3 +51,17 @@ fun PsiFile.findMethodsWithContent(content: String): List = val methods = extractElementsOfTypes(PsiMethod::class.java) methods.filter { it.getBlockBody(PsiCodeBlock::class.java) == formattingContent }.mapNotNull { it.name }.toList() } + +/** + * Retrieves the arguments of a method call with the given method name. + * + * @param methodName The name of the method to retrieve the arguments for. + * @return A list of strings representing the arguments of the method call, + * or null if no method call with the given name is found. + */ +fun PsiFile.getMethodCallArguments(methodName: String): List? = + ApplicationManager.getApplication().runReadAction?> { + extractElementsOfTypes(PsiMethodCallExpression::class.java) + .firstOrNull { it.methodExpression.referenceName == methodName } + ?.argumentList?.expressions?.mapNotNull { it.text } + } diff --git a/ij/java-psi/src/main/kotlin/org/jetbrains/academy/test/system/java/test/BaseIjTestClass.kt b/ij/java-psi/src/main/kotlin/org/jetbrains/academy/test/system/java/test/BaseIjTestClass.kt index 9aa1f39..60a2af9 100644 --- a/ij/java-psi/src/main/kotlin/org/jetbrains/academy/test/system/java/test/BaseIjTestClass.kt +++ b/ij/java-psi/src/main/kotlin/org/jetbrains/academy/test/system/java/test/BaseIjTestClass.kt @@ -14,6 +14,7 @@ import org.jetbrains.academy.test.system.ij.analyzer.findMethodUsages import org.jetbrains.academy.test.system.ij.analyzer.hasElementOfTypeWithName import org.jetbrains.academy.test.system.ij.analyzer.hasExpressionWithParent import org.jetbrains.academy.test.system.java.ij.analyzer.findMethodsWithContent +import org.jetbrains.academy.test.system.java.ij.analyzer.getMethodCallArguments import org.jetbrains.academy.test.system.java.ij.analyzer.hasConstantWithGivenValue /** @@ -49,4 +50,7 @@ open class BaseIjTestClass : BasePlatformTestCase() { expression, parent, isParentTypeFunction, PsiMethod::class.java, PsiNewExpression::class.java, PsiReferenceExpression::class.java, PsiMethodCallExpression::class.java ) + + fun getMethodCallArguments(methodName: String): List? = + myFixture.file.getMethodCallArguments(methodName) } diff --git a/ij/java-psi/src/test/kotlin/org/jetbrains/academy/test/system/java/ij/BaseIjTestClassTests.kt b/ij/java-psi/src/test/kotlin/org/jetbrains/academy/test/system/java/ij/BaseIjTestClassTests.kt index acca103..11a38c8 100644 --- a/ij/java-psi/src/test/kotlin/org/jetbrains/academy/test/system/java/ij/BaseIjTestClassTests.kt +++ b/ij/java-psi/src/test/kotlin/org/jetbrains/academy/test/system/java/ij/BaseIjTestClassTests.kt @@ -348,4 +348,26 @@ class BaseIjTestClassTests : BaseIjTestClass() { "There must exist an expression $expression with parent $parent" } } + + fun testGetMethodCallArguments() { + val example = """ + public class ExampleClass { + public static void calculateDogAgeInDogYears() { + description(""${'"'} + text + ""${'"'}.trimIndent()) + } + } + """.trimIndent() + myFixture.configureByText("Task.java", example) + val methodName = "description" + val argument = """ + ""${'"'} + text + ""${'"'}.trimIndent() + """.trimIndent() + Assertions.assertEquals(argument, getMethodCallArguments("description")?.firstOrNull()?.trimIndent()) { + "There must exist an method call $methodName with arguments $argument" + } + } } diff --git a/ij/kotlin-psi/src/main/kotlin/org/jetbrains/academy/test/system/kotlin/ij/analyzer/PsiFileAnalyzerUtil.kt b/ij/kotlin-psi/src/main/kotlin/org/jetbrains/academy/test/system/kotlin/ij/analyzer/PsiFileAnalyzerUtil.kt index f8af3dc..f594a29 100644 --- a/ij/kotlin-psi/src/main/kotlin/org/jetbrains/academy/test/system/kotlin/ij/analyzer/PsiFileAnalyzerUtil.kt +++ b/ij/kotlin-psi/src/main/kotlin/org/jetbrains/academy/test/system/kotlin/ij/analyzer/PsiFileAnalyzerUtil.kt @@ -7,11 +7,7 @@ import com.intellij.psi.PsiFileFactory import org.jetbrains.academy.test.system.ij.analyzer.* import org.jetbrains.academy.test.system.ij.formatting.formatting import org.jetbrains.kotlin.idea.KotlinFileType -import org.jetbrains.kotlin.psi.KtProperty -import org.jetbrains.kotlin.psi.KtConstantExpression -import org.jetbrains.kotlin.psi.KtStringTemplateExpression -import org.jetbrains.kotlin.psi.KtNamedFunction -import org.jetbrains.kotlin.psi.KtBlockExpression +import org.jetbrains.kotlin.psi.* /** * Checks if PsiFile contains a constant property with the given element value. @@ -43,3 +39,15 @@ fun PsiFile.findMethodsWithContent(content: String): List = } methods.filter { it.getBlockBody(KtBlockExpression::class.java) == formattingContent }.mapNotNull { it.name }.toList() } + +/** + * Retrieves the method call arguments of the specified method from the given PsiFile. + * + * @param methodName The name of the method to retrieve the arguments for. + * @return A list of strings representing the arguments of the method call, or null if the method is not found or has no arguments. + */ +fun PsiFile.getMethodCallArguments(methodName: String): List? = + ApplicationManager.getApplication().runReadAction?> { + extractElementsOfTypes(KtCallExpression::class.java).firstOrNull { it.calleeExpression?.text == methodName } + ?.valueArguments?.mapNotNull { it.text } + } diff --git a/ij/kotlin-psi/src/main/kotlin/org/jetbrains/academy/test/system/kotlin/test/BaseIjTestClass.kt b/ij/kotlin-psi/src/main/kotlin/org/jetbrains/academy/test/system/kotlin/test/BaseIjTestClass.kt index da2cf00..2c98d6a 100644 --- a/ij/kotlin-psi/src/main/kotlin/org/jetbrains/academy/test/system/kotlin/test/BaseIjTestClass.kt +++ b/ij/kotlin-psi/src/main/kotlin/org/jetbrains/academy/test/system/kotlin/test/BaseIjTestClass.kt @@ -5,6 +5,7 @@ import org.jetbrains.academy.test.system.ij.analyzer.findMethodUsages import org.jetbrains.academy.test.system.ij.analyzer.hasElementOfTypeWithName import org.jetbrains.academy.test.system.ij.analyzer.hasExpressionWithParent import org.jetbrains.academy.test.system.kotlin.ij.analyzer.findMethodsWithContent +import org.jetbrains.academy.test.system.kotlin.ij.analyzer.getMethodCallArguments import org.jetbrains.academy.test.system.kotlin.ij.analyzer.hasConstantWithGivenValue import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtNamedFunction @@ -43,4 +44,7 @@ open class BaseIjTestClass : BasePlatformTestCase() { expression, parent, isParentTypeFunction, KtNamedFunction::class.java, KtDotQualifiedExpression::class.java, KtCallExpression::class.java ) + + fun getMethodCallArguments(methodName: String): List? = + myFixture.file.getMethodCallArguments(methodName) } diff --git a/ij/kotlin-psi/src/test/kotlin/org/jetbrains/academy/test/system/kotlin/ij/BaseIjTestClassTests.kt b/ij/kotlin-psi/src/test/kotlin/org/jetbrains/academy/test/system/kotlin/ij/BaseIjTestClassTests.kt index 999d4b6..d475b77 100644 --- a/ij/kotlin-psi/src/test/kotlin/org/jetbrains/academy/test/system/kotlin/ij/BaseIjTestClassTests.kt +++ b/ij/kotlin-psi/src/test/kotlin/org/jetbrains/academy/test/system/kotlin/ij/BaseIjTestClassTests.kt @@ -314,4 +314,24 @@ class BaseIjTestClassTests : BaseIjTestClass() { "There must exist an expression $expression with parent $parent" ) } + + fun testGetMethodCallArguments() { + val example = """ + fun calculateDogAgeInDogYears() { + description(""${'"'} + text + ""${'"'}.trimIndent()) + } + """.trimIndent() + myFixture.configureByText("Task.kt", example) + val methodName = "description" + val argument = """ + ""${'"'} + text + ""${'"'}.trimIndent() + """.trimIndent() + Assertions.assertEquals(argument, getMethodCallArguments(methodName)?.firstOrNull()?.trimIndent()) { + "There must exist an method call $methodName with arguments $argument" + } + } }