Skip to content

Added support for summaries on the nested classes #1452

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

Merged
merged 3 commits into from
Dec 15, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.utbot.examples.nested;

public class DeepNested {
public class Nested1 {
public class Nested2 {
public int f(int i) {
if (i > 0) {
return 10;
}
return 0;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package examples.nested

import examples.CustomJavaDocTagsEnabler
import examples.SummaryTestCaseGeneratorTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.utbot.examples.nested.DeepNested
import org.utbot.examples.recursion.Recursion
import org.utbot.framework.plugin.api.MockStrategyApi
import org.utbot.testing.DoNotCalculate

@ExtendWith(CustomJavaDocTagsEnabler::class)
class SummaryNestedTest : SummaryTestCaseGeneratorTest(
DeepNested.Nested1.Nested2::class
) {
@Test
fun testNested() {
val summary1 = "@utbot.classUnderTest {@link DeepNested.Nested1.Nested2}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.nested.DeepNested.Nested1.Nested2#f(int)}\n" +
"@utbot.executesCondition {@code (i > 0): False}\n" +
"@utbot.returnsFrom {@code return 0;}\n"

val summary2 = "@utbot.classUnderTest {@link DeepNested.Nested1.Nested2}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.nested.DeepNested.Nested1.Nested2#f(int)}\n" +
"@utbot.executesCondition {@code (i > 0): True}\n" +
"@utbot.returnsFrom {@code return 10;}"

val methodName1 = "testF_ILessOrEqualZero"
val methodName2 = "testF_IGreaterThanZero"

val displayName1 = "i > 0 : False -> return 0"
val displayName2 = "i > 0 : True -> return 10"

val summaryKeys = listOf(
summary1,
summary2
)

val displayNames = listOf(
displayName1,
displayName2
)

val methodNames = listOf(
methodName1,
methodName2
)

val method = DeepNested.Nested1.Nested2::f
val mockStrategy = MockStrategyApi.NO_MOCKS
val coverage = DoNotCalculate

summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SourceCodeParser {
val clazz = it.types.firstOrNull { clazz ->
clazz.name.identifier == className
} ?: traverseInnerClassDeclarations(
it.types.flatMap { declaration -> declaration.childNodes },
it.types.flatMap { declaration -> declaration.childNodes }.filterIsInstance<ClassOrInterfaceDeclaration>(),
className
)

Expand Down Expand Up @@ -84,9 +84,23 @@ class SourceCodeParser {
* Identifier is ClassOrInterfaceDeclaration
*/
private fun traverseInnerClassDeclarations(
nodes: List<Node>, className: String
): TypeDeclaration<*>? = nodes.filterIsInstance<ClassOrInterfaceDeclaration>()
.firstOrNull { it.name.identifier == className }
nodes: List<ClassOrInterfaceDeclaration>, className: String
): TypeDeclaration<*>? {
var result = nodes.firstOrNull { it.name.identifier == className }

if (result != null) return result

run childrenSearch@ {
nodes.forEach {
val childNodes = it.childNodes.filterIsInstance<ClassOrInterfaceDeclaration>()
if (childNodes.isNotEmpty()) {
result = traverseInnerClassDeclarations(childNodes, className) as ClassOrInterfaceDeclaration?
if (result!= null) return@childrenSearch
}
}
}
return result
}
}

/**
Expand Down