Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore function naming in Kotest classes #2291

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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