Hi, I am looking into Konsist and trying to figure...
# konsist
d
Hi, I am looking into Konsist and trying to figure out following scenario I have a simple class
FooClass
and an enum
BarEnum
.
Copy code
class FooClass {
    val enum: BarEnum = BarEnum.BAR
}

enum class BarEnum {
    BAR
}
Now what I need to figure out is: Does
FooClass
contain a property of any
Enum
type? Happy for any ideas/input.
b
You could do it like this:
Copy code
@Test
    fun `has class enum in it`() {
        Konsist
            .scopeFromProject()
            .classes().filter { it.name == "FooClass" }.assertTrue {
                it.properties().any {
                    (it.type?.declaration as? KoClassDeclaration)?.hasEnumModifier == true
                }
            }
    }
But maybe there is a more efficient way of doing this. 😅
d
Thanks, that is interesting I simplified my example a bit as in reality the enum is defined in a different file (same package & codebase)
the problem is that now this is not a
KoClassDeclaration
but a
KoExternalDeclarationCore
b
Again it could be written in a better way probably, but after a quick investigation on this usecase this one should work:
Copy code
@Test
    fun `has class enum in it`() {
        Konsist
            .scopeFromProject()
            .classes().filter { it.name == "FooClass" }.assertTrue {
                it.properties().any { property ->
                    property.type?.containingDeclaration.toString() == "enum"
                }
            }
    }
d
I think this is a false friend because the moment the property is named
test
it does not work anymore
p
You could use something like below if it is always a specific enum type:
Copy code
Konsist
  .scopeFromProject()
  .classes()
  .withName("FooClass")
  .assertTrue { 
      it.properties().any {
          it.hasTypeOf(BarEnum::class)
      }
   }
d
Unfortunately I would need to detect any enum (also unknown enums)
I tried
it.hasTypeOf(Enum::class)
because internally every enum is of the internal type Enum but it did not work
p
The built-in support for
isEnum
is currently missing for the parameter type. You can just try a workaround like below:
Copy code
Class.forName(...).isEnum
I did not check if it is possible to get the information from the
KoPropertyDeclaration
to get the class.
d
Thanks for your support. I was not able to find a way to get the fully qualified name that I would need to call a
Class.forName
p
Will be a good feature request 👌
d
Is there some official way to drop in a feature request ^^
p
gratitude thank you 1
i
We are wrapping up next release - should be possible to do with updated API. I will check this and let you know
gratitude thank you 1
n
Konsist 0.17.0 has been released! This release includes this improvement 🙂 The following test should be helpful in your case:
Copy code
Konsist
    .scopeFromProduction()
    .classes()
    .properties()
    .types
    .assertTrue {
        it
           .sourceDeclaration
           ?.asClassDeclaration()
           ?.hasEnumModifier == true
    }
👍 2