Skip to content

Commit

Permalink
Merge pull request #26 from jetbrains-academy/fixes-for-java-psi-module
Browse files Browse the repository at this point in the history
Added fixes for java-psi module
  • Loading branch information
nbirillo authored Dec 15, 2023
2 parents 282faa4 + f3c4f78 commit 0bc874d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.academy.test.system.java.ij.analyzer

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
Expand Down Expand Up @@ -48,7 +49,7 @@ private fun formattingContent(content: String, project: Project): String {
* @return A list of method names whose bodies match the provided content.
*/
fun PsiFile.findMethodsWithContent(content: String): List<String> =
ApplicationManager.getApplication().runReadAction<List<String>> {
WriteCommandAction.runWriteCommandAction<List<String>>(project) {
formatting()
val formattingContent = formattingContent(content, project)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.psi.PsiClass
import com.intellij.psi.PsiField
import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiCallExpression
import com.intellij.psi.PsiLocalVariable
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import org.jetbrains.academy.test.system.ij.analyzer.findMethodUsages
import org.jetbrains.academy.test.system.ij.analyzer.hasElementOfTypeWithName
Expand All @@ -29,8 +30,11 @@ open class BaseIjTestClass : BasePlatformTestCase() {
fun findMethodUsages(content: String): List<String> =
myFixture.file.findMethodUsages(content, PsiCallExpression::class.java, PsiMethod::class.java)

fun hasProperty(propertyName: String): Boolean =
myFixture.file.hasElementOfTypeWithName(PsiField::class.java, propertyName)
fun hasField(fieldName: String): Boolean =
myFixture.file.hasElementOfTypeWithName(PsiField::class.java, fieldName)

fun hasLocalVariable(localVariableName: String): Boolean =
myFixture.file.hasElementOfTypeWithName(PsiLocalVariable::class.java, localVariableName)

fun hasMethod(methodName: String): Boolean =
myFixture.file.hasElementOfTypeWithName(PsiMethod::class.java, methodName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,49 @@ class BaseIjTestClassTests : BaseIjTestClass() {
}
}

fun testHasProperty() {
fun testHasField() {
val example = """
public class ExampleClass {
private static final String CONSTANT = "some text";
private final double value = 0.5;
private int number = 2;
public void method() {
double waveLength = 0.5e-6;
System.out.println("Content");
}
}
""".trimIndent()
myFixture.configureByText("Task.java", example)
var value = "CONSTANT"
assert(hasProperty(value)) { "There must exist a property with name $value" }
assert(hasField(value)) { "There must exist a property with name $value" }
value = "value"
assert(hasProperty(value)) { "There must exist a property with name $value" }
assert(hasField(value)) { "There must exist a property with name $value" }
value = "number"
assert(hasProperty(value)) { "There must exist a property with name $value" }
assertFalse(hasProperty("method"))
assertFalse(hasProperty("Content"))
assert(hasField(value)) { "There must exist a property with name $value" }
assertFalse(hasField("method"))
assertFalse(hasField("Content"))
}

fun testHasLocalVariable() {
val example = """
public class ExampleClass {
private static final String CONSTANT = "some text";
public void method() {
double value = 0.5;
int number = 2;
System.out.println("Content");
}
}
""".trimIndent()
myFixture.configureByText("Task.java", example)
var value = "value"
assert(hasLocalVariable(value)) { "There must exist a local variable with name $value" }
value = "number"
assert(hasLocalVariable(value)) { "There must exist a local variable with name $value" }
assertFalse(hasLocalVariable("CONSTANT"))
assertFalse(hasLocalVariable("Content"))
}

fun testHasMethod() {
Expand Down

0 comments on commit 0bc874d

Please sign in to comment.