Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to get method call arguments for tests in jarvis tutorial #29

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
}
Loading