Matt Thompson
10/04/2023, 9:21 PMhasTestClass()
) i.e.
Konsist.scopeFromTest()
.classes()
.assert { it.hasNameStartingWithNonTestClassName() }
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
}
igor.wojda
10/04/2023, 10:33 PM3. Test Classes Should Have Test Subject Named Sut
https://docs.konsist.lemonappdev.com/inspiration/snippets/test-snippetsMatt Thompson
10/05/2023, 4:47 PM.withoutAnnotation {
it.name == "Suppress" && it.hasArgument { arg ->
arg.value.contains("TestNaming")
}
}
igor.wojda
10/05/2023, 11:07 PMimport 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)
}
Matt Thompson
10/06/2023, 3:32 PMigor.wojda
10/06/2023, 3:34 PMMatt Thompson
10/06/2023, 3:36 PM