https://kotlinlang.org logo
#konsist
Title
# konsist
m

Matt Thompson

10/04/2023, 9:21 PM
is there any way to check that test class names match the class name under test without enforcing that all classes have tests? (so kind of a reverse of
hasTestClass()
) i.e.
Copy code
Konsist.scopeFromTest()
      .classes()
      .assert { it.hasNameStartingWithNonTestClassName() }
after trying some things I’m realizing it wouldn’t be as simple as that. we also have tests for functions, files, as well as kotlin tests for java classes…
so something like this gets me most of the way there, but fails on the tests for java classes:
Copy code
val classNames = Konsist.scopeFromProject()
      .classes()
      .withoutNameEndingWith("Test")
      .map { it.name }

    val fileNames = Konsist.scopeFromProject()
      .files
      .withoutNameEndingWith("Test")
      .map { it.name }

    val functionNames = Konsist.scopeFromProject()
      .functions(
        includeNested = false,
        includeLocal = false
      )
      .withTopLevel()
      .map { it.name }

    Konsist.scopeFromTest()
      .classes()
      .withNameEndingWith("Test")
      .assert {
        val subject = it.name.substringBefore("Test")
        subject in classNames || subject in fileNames || subject in functionNames
      }
i

igor.wojda

10/04/2023, 10:33 PM
We have a similar snippet in docs
3. Test Classes Should Have Test Subject Named Sut
https://docs.konsist.lemonappdev.com/inspiration/snippets/test-snippets
If you will be aple to figure this out please post all classes (test and prod). I need to take a closer look to understand this.
m

Matt Thompson

10/05/2023, 4:47 PM
for the tests written in kotlin but with subjects that are still in java I may just add support for suppressing the check like :
Copy code
.withoutAnnotation {
  it.name == "Suppress" && it.hasArgument { arg ->
    arg.value.contains("TestNaming")
  }
}
unless there’s an easy way I’m missing to get class names from the java sources
i

igor.wojda

10/05/2023, 11:07 PM
Konsist only works with Kotlin classes, however you can stil write java method to het names of all java classes:
Copy code
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths

fun getJavaClassNamesFromDir(path: String): List<String> {
    val dir = File(path)
    if (!dir.isDirectory) {
        throw IllegalArgumentException("$path is not a directory")
    }

    val classNamePattern = """\bclass\s+(\w+)""".toRegex()
    val classList = mutableListOf<String>()

    dir.walk().filter { it.extension == "java" }.forEach { file ->
        val content = Files.readString(Paths.get(file.absolutePath))
        classNamePattern.findAll(content).forEach { matchResult ->
            classList.add(matchResult.groupValues[1])
        }
    }

    return classList
}

fun main() {
    val pathToDir = "path/to/your/directory"
    val classNames = getJavaClassNamesFromDir(pathToDir)
    println(classNames)
}
m

Matt Thompson

10/06/2023, 3:32 PM
thanks for the tip. having trouble figuring out how to get it to work from the junit test though
i

igor.wojda

10/06/2023, 3:34 PM
It is hard for me to help you as I lack the full context. It would be best if you could provide a sample project and describe what you want to achieve
m

Matt Thompson

10/06/2023, 3:36 PM
I understand, no problem