Hi :slightly_smiling_face: I want to ensure that a...
# konsist
r
Hi 🙂 I want to ensure that all classes and functions within a specific package aren't public. I thought I could use
hasPublicOrDefaultModifier
for this. But functions within an internal class will return hasPublicOrDefaultModifier=true. Is there anything I can do to test this? Code sample added in comments.
This is the best I can do 🙂 But it would be nice if there was a function that took parent class' visibility into account, something like
.isPublicAccessable
, that would return false if the function is declared within an internal or private class.
Copy code
Konsist
.scopeFromProduction()
.files
.filter { file ->
     file.resideInPath("..data..")
}.declarations()
.stream()
.flatMap { declaration ->
    when {
        declaration is KoClassDeclaration && declaration.isTopLevel -> {
            Stream.of(
                DynamicTest.dynamicTest(
                    "${declaration.name} should be private or internal"
                ) {
                    declaration.assertTrue(
                        testName = "${declaration.name} should be private or internal"
                    ) {
                        !it.hasPublicOrDefaultModifier
                    }
                }
            )
        }

        declaration is KoFunctionDeclaration && declaration.isTopLevel -> {
            Stream.of(
                DynamicTest.dynamicTest(
                    "${declaration.name} should be private or internal"
                ) {
                    declaration.assertTrue(
                        testName = "${declaration.name} should be private or internal "
                    ) {
                        !it.hasPublicOrDefaultModifier
                    }
                }
            )
        }

        else -> null
    }
}
p
Why not excluding internal classes from the declarations you check?
r
Can I do that?
p
Something like
Copy code
!declaration.parentClass.hasInternalModifier
r
There is no parentClass on KoBaseDeclaration and I dont think it make sense to filter the declarations, which seem to require some casting and also do casting in the flatMap.
p
You are already smart cast in the when - block from
KoBaseDeclaration
to
KoClassDeclaration
or
KoFunctionDeclaration
so in the case of the classes you can easily write:
Copy code
declaration is KoClassDeclaration && declaration.isTopLevel && !declaration.hasInternalModifier
r
But that is more or less also what I'm doing, right? Smart casting inside the when from
KoBaseDeclaration
to
KoClassDeclaration
or
KoFunctionDeclaration
, check it's topLevel and than I assertTrue on
!hasPublicOrDefaultModifier
?
p
Correct, but didn't you mention, that
hasPublicOrDefaultModifier
does not work for internal classes?
r
I my first iteration I missed the isTopLevel, meaning that functions within a class would be returned in the function case in the when statement and in that case the check on
hasPublicOrDefaultModifier
would give "wrong"/unexpected results. So I guess that I was hoping for or requesting was a
isPublicAccessable
function that took parentClass visibility into account, so that functions within a internal or private class would return false for
isPublicAccessable
instead of
hasPublicOrDefaultModifier
that would return true when inside a private or internal class.
p
Ah okay, in that case I got it wrong.
r
Sorry if I was unclear. At least we came up with the same solution 🙂 Thanks!