Skip to content

Commit 6266cc6

Browse files
committed
Added test
1 parent a3fc92a commit 6266cc6

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

utbot-framework/src/test/kotlin/org/utbot/examples/AbstractTestCaseGeneratorTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,3 +2826,13 @@ inline fun <reified T> withFeaturePath(featurePath: String, block: () -> T): T {
28262826
UtSettings.enableFeatureProcess = prevEnableFeatureProcess
28272827
}
28282828
}
2829+
2830+
inline fun <reified T> withUsingReflectionForMaximizingCoverage(maximizeCoverage: Boolean, block: () -> T): T {
2831+
val prev = UtSettings.maximizeCoverageUsingReflection
2832+
UtSettings.maximizeCoverageUsingReflection = maximizeCoverage
2833+
try {
2834+
return block()
2835+
} finally {
2836+
UtSettings.maximizeCoverageUsingReflection = prev
2837+
}
2838+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.utbot.examples.stdlib
2+
3+
import org.junit.jupiter.api.Tag
4+
import org.junit.jupiter.api.Test
5+
import org.utbot.examples.AbstractTestCaseGeneratorTest
6+
import org.utbot.examples.eq
7+
import org.utbot.examples.isException
8+
import org.utbot.examples.withUsingReflectionForMaximizingCoverage
9+
import java.util.Date
10+
11+
class DateExampleTest : AbstractTestCaseGeneratorTest(testClass = DateExample::class) {
12+
@Suppress("SpellCheckingInspection")
13+
@Tag("slow")
14+
@Test
15+
fun testGetTimeWithNpeChecksForNonPublicFields() {
16+
withUsingReflectionForMaximizingCoverage(maximizeCoverage = true) {
17+
checkWithException(
18+
DateExample::getTime,
19+
eq(5),
20+
*commonMatchers,
21+
{ date: Date?, r: Result<Boolean> ->
22+
val cdate = date!!.getDeclaredFieldValue("cdate")
23+
val calendarDate = cdate!!.getDeclaredFieldValue("date")
24+
25+
calendarDate == null && r.isException<NullPointerException>()
26+
},
27+
{ date: Date?, r: Result<Boolean> ->
28+
val cdate = date!!.getDeclaredFieldValue("cdate")
29+
val calendarDate = cdate!!.getDeclaredFieldValue("date")
30+
31+
val gcal = date.getDeclaredFieldValue("gcal")
32+
33+
val normalized = calendarDate!!.getDeclaredFieldValue("normalized") as Boolean
34+
val gregorianYear = calendarDate.getDeclaredFieldValue("gregorianYear") as Int
35+
36+
gcal == null && !normalized && gregorianYear >= 1582 && r.isException<NullPointerException>()
37+
}
38+
)
39+
}
40+
}
41+
42+
@Test
43+
fun testGetTimeWithoutReflection() {
44+
withUsingReflectionForMaximizingCoverage(maximizeCoverage = false) {
45+
checkWithException(
46+
DateExample::getTime,
47+
eq(3),
48+
*commonMatchers
49+
)
50+
}
51+
}
52+
53+
private val commonMatchers = arrayOf(
54+
{ date: Date?, r: Result<Boolean> -> date == null && r.isException<NullPointerException>() },
55+
{ date: Date?, r: Result<Boolean> -> date != null && date.time == 100L && r.getOrThrow() },
56+
{ date: Date?, r: Result<Boolean> -> date != null && date.time != 100L && !r.getOrThrow() }
57+
)
58+
59+
private fun Any.getDeclaredFieldValue(fieldName: String): Any? {
60+
val declaredField = javaClass.getDeclaredField(fieldName)
61+
declaredField.isAccessible = true
62+
63+
return declaredField.get(this)
64+
}
65+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.utbot.examples.stdlib;
2+
3+
import java.util.Date;
4+
5+
public class DateExample {
6+
public boolean getTime(Date date) {
7+
return date.getTime() == 100;
8+
}
9+
}

0 commit comments

Comments
 (0)