Skip to content

Commit 2443d24

Browse files
committed
some fixes
1 parent d683ba5 commit 2443d24

File tree

4 files changed

+59
-92
lines changed

4 files changed

+59
-92
lines changed

utbot-framework-test/src/test/kotlin/org/utbot/examples/lambda/CustomPredicateExampleTest.kt

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,65 @@ import org.junit.jupiter.api.Test
44
import org.utbot.testcheckers.eq
55
import org.utbot.tests.infrastructure.DoNotCalculate
66
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
7+
import org.utbot.tests.infrastructure.isException
78

89
class CustomPredicateExampleTest : UtValueTestCaseChecker(testClass = CustomPredicateExample::class) {
910
@Test
1011
fun testNoCapturedValuesPredicateCheck() {
11-
check(
12+
checkWithException(
1213
CustomPredicateExample::noCapturedValuesPredicateCheck,
13-
eq(2),
14-
{ predicate, x, r -> !predicate.test(x) && !r!! },
15-
{ predicate, x, r -> predicate.test(x) && r!! },
14+
eq(3),
15+
{ predicate, x, r -> !predicate.test(x) && r.getOrNull() == false },
16+
{ predicate, x, r -> predicate.test(x) && r.getOrNull() == true },
17+
{ predicate, _, r -> predicate == null && r.isException<NullPointerException>() },
1618
coverage = DoNotCalculate
1719
)
1820
}
1921

2022
@Test
2123
fun testCapturedLocalVariablePredicateCheck() {
22-
check(
24+
checkWithException(
2325
CustomPredicateExample::capturedLocalVariablePredicateCheck,
24-
eq(2),
25-
{ predicate, x, r -> !predicate.test(x) && !r!! },
26-
{ predicate, x, r -> predicate.test(x) && r!! },
26+
eq(3),
27+
{ predicate, x, r -> !predicate.test(x) && r.getOrNull() == false },
28+
{ predicate, x, r -> predicate.test(x) && r.getOrNull() == true },
29+
{ predicate, _, r -> predicate == null && r.isException<NullPointerException>() },
2730
coverage = DoNotCalculate
2831
)
2932
}
3033

3134
@Test
3235
fun testCapturedParameterPredicateCheck() {
33-
check(
36+
checkWithException(
3437
CustomPredicateExample::capturedParameterPredicateCheck,
35-
eq(2),
36-
{ predicate, x, r -> !predicate.test(x) && !r!! },
37-
{ predicate, x, r -> predicate.test(x) && r!! },
38+
eq(3),
39+
{ predicate, x, r -> !predicate.test(x) && r.getOrNull() == false },
40+
{ predicate, x, r -> predicate.test(x) && r.getOrNull() == true },
41+
{ predicate, _, r -> predicate == null && r.isException<NullPointerException>() },
3842
coverage = DoNotCalculate
3943
)
4044
}
4145

4246
@Test
4347
fun testCapturedStaticFieldPredicateCheck() {
44-
check(
48+
checkWithException(
4549
CustomPredicateExample::capturedStaticFieldPredicateCheck,
46-
eq(2),
47-
{ predicate, x, r -> !predicate.test(x) && !r!! },
48-
{ predicate, x, r -> predicate.test(x) && r!! },
50+
eq(3),
51+
{ predicate, x, r -> !predicate.test(x) && r.getOrNull() == false },
52+
{ predicate, x, r -> predicate.test(x) && r.getOrNull() == true },
53+
{ predicate, _, r -> predicate == null && r.isException<NullPointerException>() },
4954
coverage = DoNotCalculate
5055
)
5156
}
5257

5358
@Test
5459
fun testCapturedNonStaticFieldPredicateCheck() {
55-
check(
60+
checkWithException(
5661
CustomPredicateExample::capturedNonStaticFieldPredicateCheck,
57-
eq(2),
58-
{ predicate, x, r -> !predicate.test(x) && !r!! },
59-
{ predicate, x, r -> predicate.test(x) && r!! },
62+
eq(3),
63+
{ predicate, x, r -> !predicate.test(x) && r.getOrNull() == false },
64+
{ predicate, x, r -> predicate.test(x) && r.getOrNull() == true },
65+
{ predicate, _, r -> predicate == null && r.isException<NullPointerException>() },
6066
coverage = DoNotCalculate
6167
)
6268
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgCallableAccessManager.kt

Lines changed: 28 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,9 @@ import org.utbot.framework.codegen.Junit5
55
import org.utbot.framework.codegen.TestNg
66
import org.utbot.framework.codegen.model.constructor.builtin.any
77
import org.utbot.framework.codegen.model.constructor.builtin.anyOfClass
8-
import org.utbot.framework.codegen.model.constructor.builtin.arraysDeepEqualsMethodId
9-
import org.utbot.framework.codegen.model.constructor.builtin.buildLambdaMethodId
10-
import org.utbot.framework.codegen.model.constructor.builtin.buildStaticLambdaMethodId
11-
import org.utbot.framework.codegen.model.constructor.builtin.createArrayMethodId
12-
import org.utbot.framework.codegen.model.constructor.builtin.createInstanceMethodId
13-
import org.utbot.framework.codegen.model.constructor.builtin.deepEqualsMethodId
148
import org.utbot.framework.codegen.model.constructor.builtin.forName
15-
import org.utbot.framework.codegen.model.constructor.builtin.getArrayLengthMethodId
16-
import org.utbot.framework.codegen.model.constructor.builtin.getLambdaCapturedArgumentValuesMethodId
179
import org.utbot.framework.codegen.model.constructor.builtin.getDeclaredConstructor
1810
import org.utbot.framework.codegen.model.constructor.builtin.getDeclaredMethod
19-
import org.utbot.framework.codegen.model.constructor.builtin.getEnumConstantByNameMethodId
20-
import org.utbot.framework.codegen.model.constructor.builtin.getFieldValueMethodId
21-
import org.utbot.framework.codegen.model.constructor.builtin.getInstantiatedMethodTypeMethodId
22-
import org.utbot.framework.codegen.model.constructor.builtin.getLambdaCapturedArgumentTypesMethodId
23-
import org.utbot.framework.codegen.model.constructor.builtin.getLambdaMethodMethodId
24-
import org.utbot.framework.codegen.model.constructor.builtin.getLookupInMethodId
25-
import org.utbot.framework.codegen.model.constructor.builtin.getSingleAbstractMethodMethodId
26-
import org.utbot.framework.codegen.model.constructor.builtin.getStaticFieldValueMethodId
2711
import org.utbot.framework.codegen.model.constructor.builtin.getTargetException
2812
import org.utbot.framework.codegen.model.constructor.builtin.invoke
2913
import org.utbot.framework.codegen.model.constructor.builtin.newInstance
@@ -130,7 +114,7 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
130114
//Builtin methods does not have jClass, so [methodId.method] will crash on it,
131115
//so we need to collect required exceptions manually from source codes
132116
if (methodId is BuiltinMethodId) {
133-
findExceptionTypesOf(methodId)
117+
methodId.findExceptionTypes()
134118
.forEach { addExceptionIfNeeded(it) }
135119
return
136120
}
@@ -168,51 +152,6 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
168152
}
169153
}
170154

171-
private fun BuiltinConstructorId.findExceptionTypes(): Set<ClassId> {
172-
// At the moment we do not have builtin ids for constructors that throw exceptions,
173-
// so we have this trivial when-expression. But if we ever add ids for such constructors,
174-
// then we **must** specify their exceptions here, so that we take them into account when generating code.
175-
@Suppress("UNUSED_EXPRESSION")
176-
return when (this) {
177-
else -> emptySet()
178-
}
179-
}
180-
181-
//WARN: if you make changes in the following sets of exceptions,
182-
//don't forget to change them in hardcoded [UtilMethods] as well
183-
private fun BuiltinMethodId.findExceptionTypes(): Set<ClassId> {
184-
if (!this.isUtil) return emptySet()
185-
186-
with(outerMostTestClass) {
187-
return when (this@findExceptionTypes) {
188-
getEnumConstantByNameMethodId -> setOf(IllegalAccessException::class.id)
189-
getStaticFieldValueMethodId,
190-
getFieldValueMethodId,
191-
setStaticFieldMethodId,
192-
setFieldMethodId -> setOf(IllegalAccessException::class.id, NoSuchFieldException::class.id)
193-
createInstanceMethodId -> setOf(Exception::class.id)
194-
getUnsafeInstanceMethodId -> setOf(ClassNotFoundException::class.id, NoSuchFieldException::class.id, IllegalAccessException::class.id)
195-
createArrayMethodId -> setOf(ClassNotFoundException::class.id)
196-
deepEqualsMethodId,
197-
arraysDeepEqualsMethodId,
198-
iterablesDeepEqualsMethodId,
199-
streamsDeepEqualsMethodId,
200-
mapsDeepEqualsMethodId,
201-
hasCustomEqualsMethodId,
202-
getArrayLengthMethodId,
203-
getLambdaCapturedArgumentTypesMethodId,
204-
getLambdaCapturedArgumentValuesMethodId,
205-
getInstantiatedMethodTypeMethodId,
206-
getLambdaMethodMethodId,
207-
getSingleAbstractMethodMethodId -> emptySet()
208-
buildStaticLambdaMethodId,
209-
buildLambdaMethodId -> setOf(Throwable::class.id)
210-
getLookupInMethodId ->setOf(IllegalAccessException::class.id, NoSuchFieldException::class.id)
211-
else -> error("Unknown util method ${this@findExceptionTypes}")
212-
}
213-
}
214-
}
215-
216155
private infix fun CgExpression?.canBeReceiverOf(executable: MethodId): Boolean =
217156
when {
218157
// TODO: rewrite by using CgMethodId, etc.
@@ -436,33 +375,51 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
436375
return argumentsArrayVariable
437376
}
438377

378+
private fun BuiltinConstructorId.findExceptionTypes(): Set<ClassId> {
379+
// At the moment we do not have builtin ids for constructors that throw exceptions,
380+
// so we have this trivial when-expression. But if we ever add ids for such constructors,
381+
// then we **must** specify their exceptions here, so that we take them into account when generating code.
382+
@Suppress("UNUSED_EXPRESSION")
383+
return when (this) {
384+
else -> emptySet()
385+
}
386+
}
387+
439388
//WARN: if you make changes in the following sets of exceptions,
440389
//don't forget to change them in hardcoded [UtilMethods] as well
441-
private fun findExceptionTypesOf(methodId: MethodId): Set<ClassId> {
390+
private fun BuiltinMethodId.findExceptionTypes(): Set<ClassId> {
442391
// TODO: at the moment we treat BuiltinMethodIds that are not util method ids
443392
// as if they have no exceptions. This should be fixed by storing exception types in BuiltinMethodId
444393
// or allowing us to access actual java.lang.Class for classes from mockito and other libraries
445394
// (this could be possibly solved by using user project's class loaders in UtContext)
446-
if (methodId !in utilMethodProvider.utilMethodIds) return emptySet()
395+
if (!isUtil(this)) return emptySet()
447396

448397
with(utilMethodProvider) {
449-
return when (methodId) {
450-
getEnumConstantByNameMethodId -> setOf(java.lang.IllegalAccessException::class.id)
398+
return when (this@findExceptionTypes) {
399+
getEnumConstantByNameMethodId -> setOf(IllegalAccessException::class.id)
451400
getStaticFieldValueMethodId,
452401
getFieldValueMethodId,
453402
setStaticFieldMethodId,
454-
setFieldMethodId -> setOf(java.lang.IllegalAccessException::class.id, java.lang.NoSuchFieldException::class.id)
403+
setFieldMethodId -> setOf(IllegalAccessException::class.id, NoSuchFieldException::class.id)
455404
createInstanceMethodId -> setOf(Exception::class.id)
456-
getUnsafeInstanceMethodId -> setOf(java.lang.ClassNotFoundException::class.id, java.lang.NoSuchFieldException::class.id, java.lang.IllegalAccessException::class.id)
457-
createArrayMethodId -> setOf(java.lang.ClassNotFoundException::class.id)
405+
getUnsafeInstanceMethodId -> setOf(ClassNotFoundException::class.id, NoSuchFieldException::class.id, IllegalAccessException::class.id)
406+
createArrayMethodId -> setOf(ClassNotFoundException::class.id)
458407
deepEqualsMethodId,
459408
arraysDeepEqualsMethodId,
460409
iterablesDeepEqualsMethodId,
461410
streamsDeepEqualsMethodId,
462411
mapsDeepEqualsMethodId,
463412
hasCustomEqualsMethodId,
464-
getArrayLengthMethodId -> emptySet()
465-
else -> error("Unknown util method $this")
413+
getArrayLengthMethodId,
414+
getLambdaCapturedArgumentTypesMethodId,
415+
getLambdaCapturedArgumentValuesMethodId,
416+
getInstantiatedMethodTypeMethodId,
417+
getLambdaMethodMethodId,
418+
getSingleAbstractMethodMethodId -> emptySet()
419+
buildStaticLambdaMethodId,
420+
buildLambdaMethodId -> setOf(Throwable::class.id)
421+
getLookupInMethodId ->setOf(IllegalAccessException::class.id, NoSuchFieldException::class.id)
422+
else -> error("Unknown util method ${this@findExceptionTypes}")
466423
}
467424
}
468425
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
961961
}
962962
}
963963

964+
is UtLambdaModel -> Unit // TODO: lambda model
965+
964966
is UtNullModel,
965967
is UtPrimitiveModel,
966968
is UtArrayModel,
@@ -1000,6 +1002,8 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
10001002
}
10011003
}
10021004

1005+
is UtLambdaModel -> Unit // TODO: lambda model
1006+
10031007
is UtNullModel,
10041008
is UtPrimitiveModel,
10051009
is UtArrayModel,

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ internal class CgTestClassConstructor(val context: CgContext) :
217217
*
218218
* @return a list of [CgUtilEntity] representing required util methods and classes (including their own dependencies).
219219
*/
220-
private fun createUtilEntities(): List<CgUtilEntity> {
220+
private fun collectUtilEntities(): List<CgUtilEntity> {
221221
val utilMethods = mutableListOf<CgUtilMethod>()
222222
// Some util methods depend on other util methods or some auxiliary classes.
223223
// Using this loop we make sure that all the util method dependencies are taken into account.

0 commit comments

Comments
 (0)