Skip to content

Commit 13e85bb

Browse files
committed
Add unit tests for summaries with custom JavaDoc tags #565
1 parent 483a56d commit 13e85bb

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

utbot-summary-tests/src/test/kotlin/examples/SummaryTestCaseGeneratorTest.kt

+44
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.utbot.framework.UtSettings.checkSolverTimeoutMillis
1111
import org.utbot.framework.codegen.TestExecution
1212
import org.utbot.framework.plugin.api.*
1313
import org.utbot.framework.plugin.api.util.UtContext
14+
import org.utbot.summary.UtSummarySettings
1415
import org.utbot.summary.comment.nextSynonyms
1516
import org.utbot.summary.summarize
1617
import kotlin.reflect.KClass
@@ -56,6 +57,7 @@ open class SummaryTestCaseGeneratorTest(
5657
checkSolverTimeoutMillis = 0
5758
checkNpeInNestedMethods = true
5859
checkNpeInNestedNotPrivateMethods = true
60+
UtSummarySettings.USE_CUSTOM_JAVADOC_TAGS = false
5961
}
6062
val utMethod = UtMethod.from(method)
6163
val testSet = executionsModel(utMethod, mockStrategy)
@@ -67,6 +69,34 @@ open class SummaryTestCaseGeneratorTest(
6769
testSetWithSummarization.checkClusterInfo(clusterInfo)
6870
}
6971

72+
/**
73+
* Checks summaries containing custom JavaDoc tags.
74+
*/
75+
inline fun <reified R> checkSummariesWithCustomTags(
76+
method: KFunction<R>,
77+
mockStrategy: MockStrategyApi,
78+
coverageMatcher: CoverageMatcher,
79+
summaryKeys: List<String>,
80+
methodNames: List<String>,
81+
displayNames: List<String>
82+
) {
83+
workaround(WorkaroundReason.HACK) {
84+
// @todo change to the constructor parameter
85+
checkSolverTimeoutMillis = 0
86+
checkNpeInNestedMethods = true
87+
checkNpeInNestedNotPrivateMethods = true
88+
UtSummarySettings.USE_CUSTOM_JAVADOC_TAGS = true
89+
}
90+
val utMethod = UtMethod.from(method)
91+
val testSet = executionsModel(utMethod, mockStrategy)
92+
testSet.summarize(searchDirectory)
93+
testSet.clustersInfo
94+
95+
testSet.executions.checkMatchersWithCustomTagsInSummary(summaryKeys)
96+
testSet.executions.checkMatchersWithMethodNames(methodNames)
97+
testSet.executions.checkMatchersWithDisplayNames(displayNames)
98+
}
99+
70100
/**
71101
* It removes from the String all whitespaces, tabs etc.
72102
*
@@ -160,6 +190,20 @@ open class SummaryTestCaseGeneratorTest(
160190
}
161191
}
162192

193+
fun List<UtExecution>.checkMatchersWithCustomTagsInSummary(
194+
summaryTextKeys: List<String>,
195+
) {
196+
if (summaryTextKeys.isEmpty()) {
197+
return
198+
}
199+
val notMatchedExecutions = this.filter { execution ->
200+
summaryTextKeys.none { summaryKey ->
201+
execution.summary?.toString()?.contains(summaryKey) == true
202+
}
203+
}
204+
Assertions.assertTrue(notMatchedExecutions.isEmpty()) { "Not matched comments ${summaries(notMatchedExecutions)}" }
205+
}
206+
163207
fun List<UtExecution>.checkMatchersWithMethodNames(
164208
methodNames: List<String>,
165209
) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package examples.controlflow
2+
3+
import examples.SummaryTestCaseGeneratorTest
4+
import org.junit.jupiter.api.Test
5+
import org.utbot.examples.DoNotCalculate
6+
import org.utbot.examples.controlflow.Conditions
7+
import org.utbot.framework.plugin.api.MockStrategyApi
8+
9+
class SummaryConditionsTest : SummaryTestCaseGeneratorTest(
10+
Conditions::class
11+
) {
12+
@Test
13+
fun testSimpleCondition() {
14+
val summary1 = "@utbot.classUnderTest {@link Conditions}\n" +
15+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#simpleCondition(boolean)}\n" +
16+
"@utbot.executesCondition {@code (condition): False}\n" +
17+
"@utbot.returnsFrom {@code return 0;}"
18+
19+
val summary2 = "@utbot.classUnderTest {@link Conditions}\n" +
20+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#simpleCondition(boolean)}\n" +
21+
"@utbot.executesCondition {@code (condition): True}\n" +
22+
"@utbot.returnsFrom {@code return 1;}"
23+
24+
val methodName1 = "testSimpleCondition_NotCondition"
25+
val methodName2 = "testSimpleCondition_Condition"
26+
27+
val displayName1 = "condition : False -> return 0"
28+
val displayName2 = "condition : True -> return 1"
29+
30+
val summaryKeys = listOf(
31+
summary1,
32+
summary2
33+
)
34+
35+
val displayNames = listOf(
36+
displayName1,
37+
displayName2
38+
)
39+
40+
val methodNames = listOf(
41+
methodName1,
42+
methodName2
43+
)
44+
45+
val method = Conditions::simpleCondition
46+
val mockStrategy = MockStrategyApi.NO_MOCKS
47+
val coverage = DoNotCalculate
48+
49+
checkSummariesWithCustomTags(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package examples.exceptions
2+
3+
import examples.SummaryTestCaseGeneratorTest
4+
import org.junit.jupiter.api.Test
5+
import org.utbot.examples.DoNotCalculate
6+
import org.utbot.examples.exceptions.ExceptionClusteringExamples
7+
import org.utbot.framework.plugin.api.MockStrategyApi
8+
9+
class SummaryExceptionClusteringExamplesTest : SummaryTestCaseGeneratorTest(
10+
ExceptionClusteringExamples::class
11+
) {
12+
@Test
13+
fun testDifferentExceptions() {
14+
val summary1 = "@utbot.classUnderTest {@link ExceptionClusteringExamples}\n" +
15+
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
16+
"@utbot.executesCondition {@code (i == 0): True}\n" +
17+
"@utbot.throwsException {@link ArithmeticException} in: return 100 / i;"
18+
19+
val summary2 = "@utbot.classUnderTest {@link ExceptionClusteringExamples}\n" +
20+
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
21+
"@utbot.executesCondition {@code (i == 0): False},\n" +
22+
"{@code (i == 1): True}\n" +
23+
"@utbot.throwsException {@link MyCheckedException} after condition: {@code i == 1}"
24+
val summary3 = "@utbot.classUnderTest {@link ExceptionClusteringExamples}\n" +
25+
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
26+
"@utbot.executesCondition {@code (i == 0): False},\n" +
27+
"{@code (i == 1): False},\n" +
28+
"{@code (i == 2): True}\n" +
29+
"@utbot.throwsException {@link IllegalArgumentException} after condition: {@code i == 2}"
30+
val summary4 = "@utbot.classUnderTest {@link ExceptionClusteringExamples}\n" +
31+
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
32+
"@utbot.executesCondition {@code (i == 0): False},\n" +
33+
"{@code (i == 1): False},\n" +
34+
"{@code (i == 2): False}\n" +
35+
"@utbot.returnsFrom {@code return i * 2;}\n"
36+
37+
val methodName1 = "testDifferentExceptions_IEqualsZero"
38+
val methodName2 = "testDifferentExceptions_IEquals1"
39+
val methodName3 = "testDifferentExceptions_IEquals2"
40+
val methodName4 = "testDifferentExceptions_INotEquals2"
41+
42+
val displayName1 = "return 100 / i : True -> ThrowArithmeticException"
43+
val displayName2 = "i == 1 -> ThrowMyCheckedException"
44+
val displayName3 = "i == 2 -> ThrowIllegalArgumentException"
45+
val displayName4 = "i == 0 : False -> return i * 2"
46+
47+
val summaryKeys = listOf(
48+
summary1,
49+
summary2,
50+
summary3,
51+
summary4
52+
)
53+
54+
val displayNames = listOf(
55+
displayName1,
56+
displayName2,
57+
displayName3,
58+
displayName4
59+
)
60+
61+
val methodNames = listOf(
62+
methodName1,
63+
methodName2,
64+
methodName3,
65+
methodName4
66+
)
67+
68+
val method = ExceptionClusteringExamples::differentExceptions
69+
val mockStrategy = MockStrategyApi.NO_MOCKS
70+
val coverage = DoNotCalculate
71+
72+
checkSummariesWithCustomTags(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
73+
}
74+
}

0 commit comments

Comments
 (0)