Skip to content

Commit 8b1b5f7

Browse files
authored
Fix broken summaries JavaDoc rendering #1605 (#1767)
Fix ternary return line alignment
1 parent ee394c0 commit 8b1b5f7

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

utbot-sample/src/main/java/org/utbot/examples/controlflow/Conditions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
package org.utbot.examples.controlflow;
22

33
public class Conditions {
4+
5+
/**
6+
* This Doc is here in order to check whether the summaries and display names are rendered correctly.
7+
* Had not in hours of peace,
8+
* It learned to lightly look on life.
9+
*
10+
* @param a some long value
11+
* @param b some int value
12+
* @return the result you won't expect.
13+
*/
14+
public int returnCastFromTernaryOperator(long a, int b) {
15+
a = a % b;
16+
return (int) (a < 0 ? a + b : a);
17+
}
18+
419
public int simpleCondition(boolean condition) {
520
if (condition) {
621
return 1;

utbot-summary-tests/src/test/kotlin/examples/controlflow/SummaryConditionsTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,49 @@ class SummaryConditionsTest : SummaryTestCaseGeneratorTest(
5151

5252
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
5353
}
54+
55+
@Test
56+
fun testReturnCastFromTernaryOperator() {
57+
val summary1 = "@utbot.classUnderTest {@link Conditions}\n" +
58+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#returnCastFromTernaryOperator(long,int)}\n" +
59+
"@utbot.returnsFrom {@code return (int) (a < 0 ? a + b : a);}\n"
60+
val summary2 = "@utbot.classUnderTest {@link Conditions}\n" +
61+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#returnCastFromTernaryOperator(long,int)}\n" +
62+
"@utbot.returnsFrom {@code return (int) (a < 0 ? a + b : a);}\n"
63+
val summary3 = "@utbot.classUnderTest {@link Conditions}\n" +
64+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#returnCastFromTernaryOperator(long,int)}\n" +
65+
"@utbot.throwsException {@link java.lang.ArithmeticException} in: a = a % b;\n"
66+
67+
val methodName1 = "testReturnCastFromTernaryOperator_A0aba"
68+
val methodName2 = "testReturnCastFromTernaryOperator_A0aba_1"
69+
val methodName3 = "testReturnCastFromTernaryOperator_ThrowArithmeticException"
70+
71+
val displayName1 = "return (int) (a < 0 ? a + b : a) : False -> return (int) (a < 0 ? a + b : a)"
72+
val displayName2 = "return (int) (a < 0 ? a + b : a) : True -> return (int) (a < 0 ? a + b : a)"
73+
val displayName3 = "a = a % b -> ThrowArithmeticException"
74+
75+
val summaryKeys = listOf(
76+
summary1,
77+
summary2,
78+
summary3
79+
)
80+
81+
val displayNames = listOf(
82+
displayName1,
83+
displayName2,
84+
displayName3
85+
)
86+
87+
val methodNames = listOf(
88+
methodName1,
89+
methodName2,
90+
methodName3
91+
)
92+
93+
val method = Conditions::returnCastFromTernaryOperator
94+
val mockStrategy = MockStrategyApi.NO_MOCKS
95+
val coverage = DoNotCalculate
96+
97+
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
98+
}
5499
}

utbot-summary/src/main/kotlin/org/utbot/summary/ast/JimpleToASTMap.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ class JimpleToASTMap(stmts: Iterable<Unit>, methodDeclaration: MethodDeclaration
188188
val stmts = stmtToASTNode.keys.toTypedArray()
189189
for (nonAlignedReturn in nonAlignedReturns) {
190190
val index = stmts.indexOf(nonAlignedReturn)
191-
if (index - 1 >= 0 && stmts[index - 1] is JIfStmt) {
192-
stmtToASTNode[nonAlignedReturn] = stmtToASTNode[stmts[index - 1]]
193-
}
191+
val ternaryIfStmtIndex = stmts.indexOfLast { it is JIfStmt && stmts.indexOf(it) <= index }
192+
193+
stmtToASTNode[nonAlignedReturn] = stmtToASTNode[stmts[ternaryIfStmtIndex]]
194194
}
195195
}
196196

0 commit comments

Comments
 (0)