Skip to content

Commit a2dcd6e

Browse files
authored
Fix switch summary content & add extra summary test #756 (#1779)
1 parent 1e5b593 commit a2dcd6e

File tree

6 files changed

+126
-14
lines changed

6 files changed

+126
-14
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ public int enumSwitch(RoundingMode m) {
4646
return -1;
4747
}
4848

49+
public int charToIntSwitch(char c) {
50+
switch (c) {
51+
case 'I': return 1;
52+
case 'V': return 5;
53+
case 'X': return 10;
54+
case 'L': return 50;
55+
case 'C': return 100;
56+
case 'D': return 500;
57+
case 'M': return 1000;
58+
default: throw new IllegalArgumentException("Unrecognized symbol: " + c);
59+
}
60+
}
61+
4962
//TODO: String switch
5063
// public int stringSwitch(String s) {
5164
// switch (s) {

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
1313
Switch::class
1414
) {
1515
@Test
16-
fun testDifferentExceptions() {
16+
fun testSimpleSwitch() {
1717
val summary1 = "@utbot.classUnderTest {@link Switch}\n" +
1818
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
1919
"@utbot.activatesSwitch {@code case 10}\n" +
@@ -68,4 +68,101 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
6868

6969
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
7070
}
71+
72+
@Test
73+
fun testCharToIntSwitch() {
74+
val summary1 = "@utbot.classUnderTest {@link Switch}\n" +
75+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
76+
"@utbot.activatesSwitch {@code case 'C'}\n" +
77+
"@utbot.returnsFrom {@code return 100;}\n"
78+
val summary2 = "@utbot.classUnderTest {@link Switch}\n" +
79+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
80+
"@utbot.activatesSwitch {@code case 'V'}\n" +
81+
"@utbot.returnsFrom {@code return 5;}\n"
82+
val summary3 = "@utbot.classUnderTest {@link Switch}\n" +
83+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
84+
"@utbot.activatesSwitch {@code case 'I'}\n" +
85+
"@utbot.returnsFrom {@code return 1;}\n"
86+
val summary4 = "@utbot.classUnderTest {@link Switch}\n" +
87+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
88+
"@utbot.activatesSwitch {@code case 'X'}\n" +
89+
"@utbot.returnsFrom {@code return 10;}\n"
90+
val summary5 = "@utbot.classUnderTest {@link Switch}\n" +
91+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
92+
"@utbot.activatesSwitch {@code case 'M'}\n" +
93+
"@utbot.returnsFrom {@code return 1000;}\n"
94+
val summary6 = "@utbot.classUnderTest {@link Switch}\n" +
95+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
96+
"@utbot.activatesSwitch {@code case 'D'}\n" +
97+
"@utbot.returnsFrom {@code return 500;}\n"
98+
val summary7 = "@utbot.classUnderTest {@link Switch}\n" +
99+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
100+
"@utbot.activatesSwitch {@code case 'L'}\n" +
101+
"@utbot.returnsFrom {@code return 50;}\n"
102+
val summary8 = "@utbot.classUnderTest {@link Switch}\n" +
103+
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
104+
"@utbot.invokes {@link java.lang.StringBuilder#append(java.lang.String)}\n" +
105+
"@utbot.invokes {@link java.lang.StringBuilder#append(char)}\n" +
106+
"@utbot.invokes {@link java.lang.StringBuilder#toString()}\n" +
107+
"@utbot.activatesSwitch {@code case default}\n" +
108+
"@utbot.throwsException {@link java.lang.IllegalArgumentException} in: default:\n" +
109+
" throw new IllegalArgumentException(\"Unrecognized symbol: \" + c);\n"
110+
111+
val methodName1 = "testCharToIntSwitch_Return100"
112+
val methodName2 = "testCharToIntSwitch_Return5"
113+
val methodName3 = "testCharToIntSwitch_Return1"
114+
val methodName4 = "testCharToIntSwitch_Return10"
115+
val methodName5 = "testCharToIntSwitch_Return1000"
116+
val methodName6 = "testCharToIntSwitch_Return500"
117+
val methodName7 = "testCharToIntSwitch_Return50"
118+
val methodName8 = "testCharToIntSwitch_StringBuilderToString"
119+
120+
val displayName1 = "switch(c) case: 'C' -> return 100"
121+
val displayName2 = "switch(c) case: 'V' -> return 5"
122+
val displayName3 = "switch(c) case: 'I' -> return 1"
123+
val displayName4 = "switch(c) case: 'X' -> return 10"
124+
val displayName5 = "switch(c) case: 'M' -> return 1000"
125+
val displayName6 = "switch(c) case: 'D' -> return 500"
126+
val displayName7 = "switch(c) case: 'L' -> return 50"
127+
val displayName8 = """default: throw new IllegalArgumentException("Unrecognized symbol: " + c) -> ThrowIllegalArgumentException"""
128+
129+
val summaryKeys = listOf(
130+
summary1,
131+
summary2,
132+
summary3,
133+
summary4,
134+
summary5,
135+
summary6,
136+
summary7,
137+
summary8,
138+
)
139+
140+
val displayNames = listOf(
141+
displayName1,
142+
displayName2,
143+
displayName3,
144+
displayName4,
145+
displayName5,
146+
displayName6,
147+
displayName7,
148+
displayName8,
149+
)
150+
151+
val methodNames = listOf(
152+
methodName1,
153+
methodName2,
154+
methodName3,
155+
methodName4,
156+
methodName5,
157+
methodName6,
158+
methodName7,
159+
methodName8,
160+
)
161+
162+
val method = Switch::charToIntSwitch
163+
val mockStrategy = MockStrategyApi.NO_MOCKS
164+
val coverage = DoNotCalculate
165+
166+
summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
167+
}
71168
}

utbot-summary-tests/src/test/kotlin/math/SummaryIntMathPowTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SummaryIntMathPowTest : SummaryTestCaseGeneratorTest(
1111
) {
1212
@Test
1313
fun testPow() {
14-
val summary1 = "Test activates switch case: 2, returns from: return 1;\n"
14+
val summary1 = "Test activates switch case: 1, returns from: return 1;\n"
1515
val summary2 = "Test executes conditions:\n" +
1616
" (k < Integer.SIZE): False\n" +
1717
"returns from: return 0;\n"
@@ -69,7 +69,7 @@ class SummaryIntMathPowTest : SummaryTestCaseGeneratorTest(
6969
val methodName13 = "testPow_KBitwiseAnd1NotEqualsZero_2"
7070
val methodName14 = "testPow_KBitwiseAnd1EqualsZero_2"
7171

72-
val displayName1 = "switch(b) case: 2 -> return 1"
72+
val displayName1 = "switch(b) case: 1 -> return 1"
7373
val displayName2 = "k < Integer.SIZE : False -> return 0"
7474
val displayName3 = "k < Integer.SIZE : False -> return (k < Integer.SIZE) ? (1 << k) : 0"
7575
val displayName4 = "-> return b * accum" // TODO: weird display name with missed part before ->

utbot-summary/src/main/kotlin/org/utbot/summary/AbstractTextBuilder.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,14 @@ abstract class AbstractTextBuilder(
106106
return JimpleToASTMap.getSwitchCaseLabel(astNode, case)
107107
}
108108
}
109-
//needed more tests to cover these cases
110109
if (stmt is JTableSwitchStmt && astNode is SwitchStmt) {
111-
val switchCase = JimpleToASTMap.mapSwitchCase(astNode, step.decision)
110+
val switchCase = JimpleToASTMap.mapSwitchCase(astNode, step)
112111
if (switchCase is SwitchEntry) {
113112
val case = switchCase.labels.first
114-
if (case.isPresent) {
115-
return "${case.get()}"
113+
return if (case.isPresent) {
114+
"${case.get()}"
116115
} else {
117-
return "default"
116+
"default"
118117
}
119118
}
120119
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.github.javaparser.ast.stmt.Statement
2525
import com.github.javaparser.ast.stmt.SwitchEntry
2626
import com.github.javaparser.ast.stmt.SwitchStmt
2727
import com.github.javaparser.ast.stmt.WhileStmt
28+
import org.utbot.framework.plugin.api.Step
2829
import org.utbot.summary.comment.isLoopStatement
2930
import java.util.LinkedList
3031
import java.util.Queue
@@ -339,12 +340,14 @@ class JimpleToASTMap(stmts: Iterable<Unit>, methodDeclaration: MethodDeclaration
339340
}
340341

341342
/**
342-
* @return SwitchEntry №decision
343+
* @return SwitchEntry
343344
*/
344-
fun mapSwitchCase(switchStmt: SwitchStmt, decision: Int): SwitchEntry? {
345-
val switchStmts = switchStmt.childNodes.filterIsInstance<SwitchEntry>()
346-
return if (decision < switchStmts.size) switchStmts[decision]
347-
else null
345+
fun mapSwitchCase(switchStmt: SwitchStmt, step: Step): SwitchEntry? {
346+
val neededLine = step.stmt.unitBoxes[step.decision].unit.javaSourceStartLineNumber
347+
return switchStmt
348+
.childNodes
349+
.filterIsInstance<SwitchEntry>()
350+
.find { neededLine in (it.range.get().begin.line..it.range.get().end.line) }
348351
}
349352

350353
/**

utbot-summary/src/main/kotlin/org/utbot/summary/name/NodeConvertor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class NodeConvertor {
274274
res += JimpleToASTMap.getSwitchCaseLabel(switchNode, case).capitalize()
275275
}
276276
if (stmt is JTableSwitchStmt) {
277-
val switchCase = JimpleToASTMap.mapSwitchCase(switchNode, step.decision)
277+
val switchCase = JimpleToASTMap.mapSwitchCase(switchNode, step)
278278
if (switchCase is SwitchEntry) {
279279
val case = switchCase.labels.first
280280
res += if (case.isPresent) {

0 commit comments

Comments
 (0)