David
10/02/2023, 7:35 AMdata class
class DataClasses {
@Test
fun `data classes use only immutable parameters`() {
projectScope().classes(includeNested = true).withDataModifier().assert {
it.properties(includeNested = false).none { property ->
property.hasVarModifier
}
}
}
}
However I hit an error on one of my classes:
@Parcelize
data class Device(var prop1: String) :
Parcelable {
var prop2: String = ""
override fun somefunction(): Int {
var prop3 = ""
}
}
It can properly find prop1
, prop2
but also finds prop3
, any ideas how I can only get the top level variables? I would expect the prop3
to be removed by includeNested = false
but this does not seem to be the case.@Test
fun `data classes use only immutable declarations`() {
projectScope().classes(includeNested = true).withDataModifier().assert { koClassDeclaration
->
koClassDeclaration
.properties(includeNested = false)
.filter { koClassDeclaration == it.containingDeclaration }
.none { it.hasVarModifier }
}
}
Declaration
, instead of always getting all of them.Natalia Peterwas
10/02/2023, 10:06 AMincludeLocal = false
parameter to `properties()`:
@Test
fun `data classes use only immutable parameters`() {
projectScope().classes(includeNested = true).withDataModifier().assert {
it.properties(includeNested = false, includeLocal = false).none { property ->
property.hasVarModifier
}
}
}
Your version doesn’t work because you only exclude nested declarations (e.g. if you add a nested class inside the Device
class and have some properties in it - they will be excluded). In this case we also need to exclude local declarations.
Let me know if it works now 🙂@Test
fun `data classes use only immutable declarations`() {
getSnippetFile("test")
.classes(includeNested = true)
.withDataModifier()
.properties(includeNested = false, includeLocal = false)
.assertNot { it.hasVarModifier }
}
Using this test, if an error occurs, the invalid properties will be displayed, not the class they are in.David
10/02/2023, 11:22 AMprop2
in my example above. However, now it seems to work fine, not sure if it was because I updated Konsist or some gradle sync issue (or error by me). Seems to work just fine now. 🙃
Also big thanks for your latest update, that makes it so much cleaner and the errors easier to read. Thanks! 🤗igor.wojda
10/02/2023, 2:01 PM