Skip to content

Commit

Permalink
Added function to get method call arguments for tests in jarvis tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
anchouls committed Jul 10, 2024
1 parent 24a48f7 commit 0a233e5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "org.jetbrains.academy.test.system"
version = "2.1.2"
version = "2.1.3"

allprojects {
apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,3 +51,17 @@ fun PsiFile.findMethodsWithContent(content: String): List<String> =
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<String>? =
ApplicationManager.getApplication().runReadAction<List<String>?> {
extractElementsOfTypes(PsiMethodCallExpression::class.java)
.firstOrNull { it.methodExpression.referenceName == methodName }
?.argumentList?.expressions?.mapNotNull { it.text }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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<String>? =
myFixture.file.getMethodCallArguments(methodName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -43,3 +39,15 @@ fun PsiFile.findMethodsWithContent(content: String): List<String> =
}
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<String>? =
ApplicationManager.getApplication().runReadAction<List<String>?> {
extractElementsOfTypes(KtCallExpression::class.java).firstOrNull { it.calleeExpression?.text == methodName }
?.valueArguments?.mapNotNull { it.text }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String>? =
myFixture.file.getMethodCallArguments(methodName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}

0 comments on commit 0a233e5

Please sign in to comment.