Skip to content

Commit

Permalink
Refactoring in Summary Module #2322 #2367 (#2425)
Browse files Browse the repository at this point in the history
  • Loading branch information
sofurihafe authored Jul 20, 2023
1 parent 4833af7 commit 231d9e0
Show file tree
Hide file tree
Showing 27 changed files with 357 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class UtBotSymbolicEngine(
applicationContext.getBeansAssignableTo(methodUnderTest.classId).isEmpty()) {
val fullConfigDisplayName = (applicationContext.springSettings as? SpringSettings.PresentSpringSettings)
?.configuration?.fullDisplayName
val errorDescription = "No beans of type ${methodUnderTest.classId.name} are found. " +
val errorDescription = "No beans of type ${methodUnderTest.classId.name} were found. " +
"Try choosing different Spring configuration or adding beans to $fullConfigDisplayName"
emit(UtError(
errorDescription,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ public void emptyBranches(boolean condition) {
// do nothing
}
}

public int elseIf(int id) throws RuntimeException {
if (id > 0) return 0;
else if (id == 0) throw new RuntimeException("Exception message");
else return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public int charToIntSwitch(char c) {
}
}

public int throwExceptionInSwitchArgument() {
switch (getChar()) {
case 'I':
return 1;
default:
return 100;
}
}

private char getChar() throws RuntimeException {
throw new RuntimeException("Exception message");
}

//TODO: String switch
// public int stringSwitch(String s) {
// switch (s) {
Expand All @@ -72,4 +85,3 @@ public int charToIntSwitch(char c) {
// }
// }
}

Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@ private int nestedWithThrow(int i) {
}
return i;
}
}

public int throwExceptionInMethodUnderTest() throws RuntimeException {
throw new RuntimeException("Exception message");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ class SummaryBinarySearchTest : SummaryTestCaseGeneratorTest(
"returns from: return right + 1;\n"
val summary5 = "Test invokes:\n" +
" org.utbot.examples.algorithms.BinarySearch#isUnsorted(long[]) once\n" +
"throws NullPointerException in: isUnsorted(array)\n"
"throws NullPointerException when: isUnsorted(array)\n"
val summary6 = "Test invokes:\n" +
" org.utbot.examples.algorithms.BinarySearch#isUnsorted(long[]) once\n" +
"executes conditions:\n" +
" (isUnsorted(array)): True\n" +
"throws IllegalArgumentException after condition: isUnsorted(array)\n"
"throws IllegalArgumentException when: isUnsorted(array)\n"

val methodName1 = "testLeftBinSearch_NotFound"
val methodName2 = "testLeftBinSearch_MiddleOfArrayLessThanKey"
val methodName3 = "testLeftBinSearch_Found"
val methodName4 = "testLeftBinSearch_Found_1"
val methodName5 = "testLeftBinSearch_BinarySearchIsUnsorted"
val methodName6 = "testLeftBinSearch_IsUnsorted"
val methodName5 = "testLeftBinSearch_ThrowNullPointerException"
val methodName6 = "testLeftBinSearch_ThrowIllegalArgumentException"

val displayName1 = "found : False -> return -1"
val displayName2 = "array[middle] == key : False -> return -1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ class SummarySortTest : SummaryTestCaseGeneratorTest(
@Test
fun testDefaultSort() {
val summary1 = "Test \n" +
"throws NullPointerException in: array.length < 4\n"
"throws NullPointerException when: array.length < 4\n"
val summary2 = "Test executes conditions:\n" +
" (array.length < 4): True\n" +
"throws IllegalArgumentException after condition: array.length < 4\n"
"throws IllegalArgumentException when: array.length < 4\n"
val summary3 = "Test executes conditions:\n" +
" (array.length < 4): False\n" +
"invokes:\n" +
" {@link java.util.Arrays#sort(int[])} once\n" +
"returns from: return array;\n"

val methodName1 = "testDefaultSort_ThrowNullPointerException"
val methodName2 = "testDefaultSort_ArrayLengthLessThan4"
val methodName2 = "testDefaultSort_ThrowIllegalArgumentException"
val methodName3 = "testDefaultSort_ArrayLengthGreaterOrEqual4"

val displayName1 = "array.length < 4 -> ThrowNullPointerException"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SummaryListWrapperReturnsVoidTest : SummaryTestCaseGeneratorTest(
val summary3 = "Test returns from: return i[0];"
val summary4 = "Test returns from: return i[0];"

val methodName1 = "testRunForEach_ListForEach"
val methodName1 = "testRunForEach_ThrowNullPointerException"
val methodName2 = "testRunForEach_Return0OfI"
val methodName3 = "testRunForEach_Return0OfI_1"
val methodName4 = "testRunForEach_Return0OfI_2"
Expand Down Expand Up @@ -88,7 +88,7 @@ class SummaryListWrapperReturnsVoidTest : SummaryTestCaseGeneratorTest(
"returns from: return sum[0];"

val methodName1 = "testSumPositiveForEach_ThrowNullPointerException"
val methodName2 = "testSumPositiveForEach_ListForEach"
val methodName2 = "testSumPositiveForEach_ThrowNullPointerException_1"
val methodName3 = "testSumPositiveForEach_0OfSumEqualsZero"
val methodName4 = "testSumPositiveForEach_0OfSumEqualsZero_1"
val methodName5 = "testSumPositiveForEach_0OfSumNotEqualsZero"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,56 @@ class SummaryConditionsTest : SummaryTestCaseGeneratorTest(

summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
}
}

@Test
fun testElseIf() {
val summary1 = "@utbot.classUnderTest {@link Conditions}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#elseIf(int)}\n" +
"@utbot.executesCondition {@code (id > 0): True}\n" +
"@utbot.returnsFrom {@code return 0;}"

val summary2 = "@utbot.classUnderTest {@link Conditions}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#elseIf(int)}\n" +
"@utbot.executesCondition {@code (id > 0): False}\n" +
"@utbot.executesCondition {@code (id == 0): False}\n" +
"@utbot.returnsFrom {@code return 1;}"

val summary3 = "@utbot.classUnderTest {@link Conditions}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Conditions#elseIf(int)}\n" +
"@utbot.executesCondition {@code (id > 0): False}\n" +
"@utbot.executesCondition {@code (id == 0): True}\n" +
"@utbot.throwsException {@link java.lang.RuntimeException} when: id == 0"

val methodName1 = "testElseIf_IdGreaterThanZero"
val methodName2 = "testElseIf_IdNotEqualsZero"
val methodName3 = "testElseIf_ThrowRuntimeException"

val displayName1 = "id > 0 : True -> id > 0"
val displayName2 = "id > 0 : False -> return 1"
val displayName3 = "id == 0 -> ThrowRuntimeException"

val summaryKeys = listOf(
summary1,
summary2,
summary3
)

val displayNames = listOf(
displayName1,
displayName2,
displayName3
)

val methodNames = listOf(
methodName1,
methodName2,
methodName3
)

val method = Conditions::elseIf
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 @@ -5,6 +5,7 @@ import examples.SummaryTestCaseGeneratorTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.utbot.examples.controlflow.Switch
import org.utbot.examples.exceptions.ExceptionExamples
import org.utbot.framework.plugin.api.MockStrategyApi
import org.utbot.testing.DoNotCalculate

Expand All @@ -16,19 +17,19 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
fun testSimpleSwitch() {
val summary1 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
"@utbot.activatesSwitch {@code case 10}\n" +
"@utbot.activatesSwitch {@code switch(x) case: 10}\n" +
"@utbot.returnsFrom {@code return 10;}"
val summary2 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
"@utbot.activatesSwitch {@code case default}\n" +
"@utbot.activatesSwitch {@code switch(x) case: default}\n" +
"@utbot.returnsFrom {@code return -1;}"
val summary3 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
"@utbot.activatesSwitch {@code case 12}\n" +
"@utbot.activatesSwitch {@code switch(x) case: 12}\n" +
"@utbot.returnsFrom {@code return 12;}"
val summary4 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#simpleSwitch(int)}\n" +
"@utbot.activatesSwitch {@code case 13}\n" +
"@utbot.activatesSwitch {@code switch(x) case: 13}\n" +
"@utbot.returnsFrom {@code return 13;}"

val methodName1 = "testSimpleSwitch_Return10"
Expand All @@ -37,7 +38,7 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
val methodName4 = "testSimpleSwitch_Return13"

val displayName1 = "switch(x) case: 10 -> return 10"
val displayName2 = "switch(x) case: Default -> return -1"
val displayName2 = "switch(x) case: default -> return -1"
val displayName3 = "switch(x) case: 12 -> return 12"
val displayName4 = "switch(x) case: 13 -> return 13"

Expand Down Expand Up @@ -73,40 +74,39 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
fun testCharToIntSwitch() {
val summary1 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
"@utbot.activatesSwitch {@code case 'C'}\n" +
"@utbot.activatesSwitch {@code switch(c) case: 'C'}\n" +
"@utbot.returnsFrom {@code return 100;}\n"
val summary2 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
"@utbot.activatesSwitch {@code case 'V'}\n" +
"@utbot.activatesSwitch {@code switch(c) case: 'V'}\n" +
"@utbot.returnsFrom {@code return 5;}\n"
val summary3 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
"@utbot.activatesSwitch {@code case 'I'}\n" +
"@utbot.activatesSwitch {@code switch(c) case: 'I'}\n" +
"@utbot.returnsFrom {@code return 1;}\n"
val summary4 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
"@utbot.activatesSwitch {@code case 'X'}\n" +
"@utbot.activatesSwitch {@code switch(c) case: 'X'}\n" +
"@utbot.returnsFrom {@code return 10;}\n"
val summary5 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
"@utbot.activatesSwitch {@code case 'M'}\n" +
"@utbot.activatesSwitch {@code switch(c) case: 'M'}\n" +
"@utbot.returnsFrom {@code return 1000;}\n"
val summary6 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
"@utbot.activatesSwitch {@code case 'D'}\n" +
"@utbot.activatesSwitch {@code switch(c) case: 'D'}\n" +
"@utbot.returnsFrom {@code return 500;}\n"
val summary7 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
"@utbot.activatesSwitch {@code case 'L'}\n" +
"@utbot.activatesSwitch {@code switch(c) case: 'L'}\n" +
"@utbot.returnsFrom {@code return 50;}\n"
val summary8 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#charToIntSwitch(char)}\n" +
"@utbot.invokes {@link java.lang.StringBuilder#append(java.lang.String)}\n" +
"@utbot.invokes {@link java.lang.StringBuilder#append(char)}\n" +
"@utbot.invokes {@link java.lang.StringBuilder#toString()}\n" +
"@utbot.activatesSwitch {@code case default}\n" +
"@utbot.throwsException {@link java.lang.IllegalArgumentException} in: default:\n" +
" throw new IllegalArgumentException(\"Unrecognized symbol: \" + c);\n"
"@utbot.activatesSwitch {@code switch(c) case: default}\n" +
"@utbot.throwsException {@link java.lang.IllegalArgumentException} when: switch(c) case: default\n"

val methodName1 = "testCharToIntSwitch_Return100"
val methodName2 = "testCharToIntSwitch_Return5"
Expand All @@ -115,7 +115,7 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
val methodName5 = "testCharToIntSwitch_Return1000"
val methodName6 = "testCharToIntSwitch_Return500"
val methodName7 = "testCharToIntSwitch_Return50"
val methodName8 = "testCharToIntSwitch_StringBuilderToString"
val methodName8 = "testCharToIntSwitch_ThrowIllegalArgumentException"

val displayName1 = "switch(c) case: 'C' -> return 100"
val displayName2 = "switch(c) case: 'V' -> return 5"
Expand All @@ -124,7 +124,7 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(
val displayName5 = "switch(c) case: 'M' -> return 1000"
val displayName6 = "switch(c) case: 'D' -> return 500"
val displayName7 = "switch(c) case: 'L' -> return 50"
val displayName8 = """default: throw new IllegalArgumentException("Unrecognized symbol: " + c) -> ThrowIllegalArgumentException"""
val displayName8 = "switch(c) case: default -> ThrowIllegalArgumentException"

val summaryKeys = listOf(
summary1,
Expand Down Expand Up @@ -165,4 +165,34 @@ class SummarySwitchTest : SummaryTestCaseGeneratorTest(

summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
}
}

@Test
fun testThrowExceptionInSwitchArgument() {
val summary1 = "@utbot.classUnderTest {@link Switch}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.controlflow.Switch#throwExceptionInSwitchArgument()}\n" +
"@utbot.invokes org.utbot.examples.controlflow.Switch#getChar()\n" +
"@utbot.throwsException {@link java.lang.RuntimeException} in: switch(getChar())\n"

val methodName1 = "testThrowExceptionInSwitchArgument_ThrowRuntimeException"

val displayName1 = "switch(getChar()) -> ThrowRuntimeException"

val summaryKeys = listOf(
summary1,
)

val displayNames = listOf(
displayName1,
)

val methodNames = listOf(
methodName1,
)

val method = Switch::throwExceptionInSwitchArgument
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 @@ -23,23 +23,23 @@ class SummaryExceptionClusteringExamplesTest : SummaryTestCaseGeneratorTest(
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
"@utbot.executesCondition {@code (i == 0): False}\n" +
"@utbot.executesCondition {@code (i == 1): True}\n" +
"@utbot.throwsException {@link org.utbot.examples.exceptions.MyCheckedException} after condition: i == 1"
"@utbot.throwsException {@link org.utbot.examples.exceptions.MyCheckedException} when: i == 1"
val summary3 = "@utbot.classUnderTest {@link ExceptionClusteringExamples}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
"@utbot.executesCondition {@code (i == 0): False}\n" +
"@utbot.executesCondition {@code (i == 1): False}\n" +
"@utbot.executesCondition {@code (i == 2): True}\n" +
"@utbot.throwsException {@link java.lang.IllegalArgumentException} after condition: i == 2"
"@utbot.throwsException {@link java.lang.IllegalArgumentException} when: i == 2"
val summary4 = "@utbot.classUnderTest {@link ExceptionClusteringExamples}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionClusteringExamples#differentExceptions(int)}\n" +
"@utbot.executesCondition {@code (i == 0): False}\n" +
"@utbot.executesCondition {@code (i == 1): False}\n" +
"@utbot.executesCondition {@code (i == 2): False}\n" +
"@utbot.returnsFrom {@code return i * 2;}\n"

val methodName1 = "testDifferentExceptions_IEqualsZero"
val methodName2 = "testDifferentExceptions_IEquals1"
val methodName3 = "testDifferentExceptions_IEquals2"
val methodName1 = "testDifferentExceptions_ThrowArithmeticException"
val methodName2 = "testDifferentExceptions_ThrowMyCheckedException"
val methodName3 = "testDifferentExceptions_ThrowIllegalArgumentException"
val methodName4 = "testDifferentExceptions_INotEquals2"

val displayName1 = "return 100 / i : True -> ThrowArithmeticException"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,33 @@ class SummaryExceptionExampleTest : SummaryTestCaseGeneratorTest(

summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
}

@Test
fun testThrowExceptionInMethodUnderTest() {
val summary1 = "@utbot.classUnderTest {@link ExceptionExamples}\n" +
"@utbot.methodUnderTest {@link org.utbot.examples.exceptions.ExceptionExamples#throwExceptionInMethodUnderTest()}\n" +
"@utbot.throwsException {@link java.lang.RuntimeException} in: throw new RuntimeException(\"Exception message\");\n"

val methodName1 = "testThrowExceptionInMethodUnderTest_ThrowRuntimeException"

val displayName1 = " -> ThrowRuntimeException"

val summaryKeys = listOf(
summary1,
)

val displayNames = listOf(
displayName1,
)

val methodNames = listOf(
methodName1,
)

val method = ExceptionExamples::throwExceptionInMethodUnderTest
val mockStrategy = MockStrategyApi.NO_MOCKS
val coverage = DoNotCalculate

summaryCheck(method, mockStrategy, coverage, summaryKeys, methodNames, displayNames)
}
}
Loading

0 comments on commit 231d9e0

Please sign in to comment.