Skip to content

Commit

Permalink
Ignore function naming in Kotest classes (#2291)
Browse files Browse the repository at this point in the history
Closes #2289
  • Loading branch information
paul-dingemans committed Oct 3, 2023
1 parent 49fc7d6 commit 418b873
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Do not force blank line before function in right hand side of assignment `blank-line-before-declaration` [#2260](https://github.com/pinterest/ktlint/issue/2260)
* Ignore override of function in rule `function-naming` [#2271](https://github.com/pinterest/ktlint/issue/2271)
* Do not replace function body having a return statement only in case the return statement contains an intermediate exit point 'function-expression-body' [#2269](https://github.com/pinterest/ktlint/issue/2269)
* Ignore function naming in Kotest classes `function-naming` [#2289](https://github.com/pinterest/ktlint/issue/2289)
* Prevent wrapping of nested multiline binary expression before operation reference as it results in a compilation error `multiline-expression-wrapping` [#2286](https://github.com/pinterest/ktlint/issue/2286)
* Force blank line before object declaration if preceded by another declaration `blank-line-before-declaration` [#2284](https://github.com/pinterest/ktlint/issues/2284)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,15 @@ public class FunctionNamingRule :
(node.psi as KtImportDirective)
.importPath
?.pathStr
?.takeIf {
it.startsWith(ORG_JUNIT) || it.startsWith(ORG_TESTNG) || it.startsWith(KOTLIN_TEST)
}?.let {
// Assume that each file that imports a Junit Jupiter Api class is a test class
isTestClass = true
}
?.takeIf { importPathString -> TEST_LIBRARIES_SET.any { importPathString.startsWith(it) } }
?.let { isTestClass = true }
}

node
.takeIf { node.elementType == FUN }
?.takeUnless {
node.isFactoryMethod() ||
node.isTestMethod() ||
node.isMethodInTestClass() ||
node.hasValidFunctionName() ||
node.isAnonymousFunction() ||
node.isOverrideFunction() ||
Expand All @@ -87,7 +83,7 @@ public class FunctionNamingRule :
(this.psi as KtFunction)
.let { it.hasDeclaredReturnType() && it.name == it.typeReference?.text }

private fun ASTNode.isTestMethod() = isTestClass && hasValidTestFunctionName()
private fun ASTNode.isMethodInTestClass() = isTestClass && hasValidTestFunctionName()

private fun ASTNode.hasValidTestFunctionName() =
findChildByType(IDENTIFIER)
Expand Down Expand Up @@ -159,9 +155,13 @@ public class FunctionNamingRule :

private val VALID_FUNCTION_NAME_REGEXP = "[a-z][A-Za-z\\d]*".regExIgnoringDiacriticsAndStrokesOnLetters()
private val VALID_TEST_FUNCTION_NAME_REGEXP = "(`.*`)|([a-z][A-Za-z\\d_]*)".regExIgnoringDiacriticsAndStrokesOnLetters()
private const val KOTLIN_TEST = "kotlin.test"
private const val ORG_JUNIT = "org.junit"
private const val ORG_TESTNG = "org.testng"
private val TEST_LIBRARIES_SET =
setOf(
"io.kotest",
"kotlin.test",
"org.junit",
"org.testng",
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class FunctionNamingRuleTest {
@ParameterizedTest(name = "Junit import: {0}")
@ValueSource(
strings = [
"io.kotest.*",
"org.junit.jupiter.api.Test",
"org.junit.jupiter.api.*",
"org.junit.jupiter.*",
Expand Down

0 comments on commit 418b873

Please sign in to comment.