Eric
08/21/2024, 2:21 PMResponseEntity<Page<Foo>>
instead of ResponseEntity<List<Foo>>
(or any other collection, like Set
etc.). How can I make this assertion on a KoReturnProvider
?Eric
08/21/2024, 4:06 PM@Test
fun `test rest functions do not return a ResponseEntity containing a Collection`() {
controllers
.restFunctions()
.assertFalse { function ->
val className = (function.containingDeclaration as KoClassDeclaration).fullyQualifiedName
val clazz = Class.forName(className)
val functions = clazz.kotlin.functions.filter { it.name == function.name }
val functionReturnRegex =
".*${function.returnType?.name.orEmpty().replace("<", "<.*")}".replace("?", "\\?").toRegex()
val f = functions.first { it.returnType.toString().matches(functionReturnRegex) }
f.returnType.arguments.any { genericType ->
(genericType.type?.classifier as? KClass<*>)?.let { Collection::class.isSuperclassOf(it) } == true
}
}
}
Natalia Peterwas
11/25/2024, 11:55 AM0.17.0
has been released! This release includes this improvement :)
The following test should be helpful in your case:
Konsist
.scopeFromProduction()
.functions()
.returnTypes
.assertTrue {
it
.typeArguments
?.firstOrNull()
?.hasSourceDeclarationOf(Page::class)
}
or more general:
Konsist
.scopeFromProduction()
.functions()
.withName("sampleTest")
.returnTypes
.assertFalse {
it
.typeArguments
?.firstOrNull()
?.hasSourceDeclaration { it.isKotlinCollectionType }
}