From 300aaab2f0ec3c74af4e34a32338a1d1ea06e277 Mon Sep 17 00:00:00 2001 From: amandelpie Date: Fri, 2 Dec 2022 18:46:50 +0300 Subject: [PATCH] Added support for summaries on the nested classes --- .../org/utbot/examples/nested/DeepNested.java | 14 +++++ .../examples/nested/SummaryNestedTest.kt | 55 +++++++++++++++++++ .../org/utbot/summary/ast/SourceCodeParser.kt | 22 ++++++-- 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 utbot-sample/src/main/java/org/utbot/examples/nested/DeepNested.java create mode 100644 utbot-summary-tests/src/test/kotlin/examples/nested/SummaryNestedTest.kt diff --git a/utbot-sample/src/main/java/org/utbot/examples/nested/DeepNested.java b/utbot-sample/src/main/java/org/utbot/examples/nested/DeepNested.java new file mode 100644 index 0000000000..0c9ba5e61d --- /dev/null +++ b/utbot-sample/src/main/java/org/utbot/examples/nested/DeepNested.java @@ -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; + } + } + } +} \ No newline at end of file diff --git a/utbot-summary-tests/src/test/kotlin/examples/nested/SummaryNestedTest.kt b/utbot-summary-tests/src/test/kotlin/examples/nested/SummaryNestedTest.kt new file mode 100644 index 0000000000..0252e3d970 --- /dev/null +++ b/utbot-summary-tests/src/test/kotlin/examples/nested/SummaryNestedTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/utbot-summary/src/main/kotlin/org/utbot/summary/ast/SourceCodeParser.kt b/utbot-summary/src/main/kotlin/org/utbot/summary/ast/SourceCodeParser.kt index a6e226fca5..8fa3f041e5 100644 --- a/utbot-summary/src/main/kotlin/org/utbot/summary/ast/SourceCodeParser.kt +++ b/utbot-summary/src/main/kotlin/org/utbot/summary/ast/SourceCodeParser.kt @@ -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(), className ) @@ -84,9 +84,23 @@ class SourceCodeParser { * Identifier is ClassOrInterfaceDeclaration */ private fun traverseInnerClassDeclarations( - nodes: List, className: String - ): TypeDeclaration<*>? = nodes.filterIsInstance() - .firstOrNull { it.name.identifier == className } + nodes: List, 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() + if (childNodes.isNotEmpty()) { + result = traverseInnerClassDeclarations(childNodes, className) as ClassOrInterfaceDeclaration? + if (result!= null) return@childrenSearch + } + } + } + return result + } } /**