From e5d81af8d3a971c2d90d1b9ffcd6ce2f4cfc0c36 Mon Sep 17 00:00:00 2001 From: Chao Zhang Date: Sun, 24 Jul 2022 17:13:50 -0700 Subject: [PATCH] Remove TrailingCommaRuleTest completely --- .../TrailingCommaOnCallSiteRuleTest.kt | 26 ++++- .../TrailingCommaOnDeclarationSiteRuleTest.kt | 90 ++++++++++++++---- .../ruleset/standard/TrailingCommaRuleTest.kt | 95 ------------------- 3 files changed, 93 insertions(+), 118 deletions(-) delete mode 100644 ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaRuleTest.kt diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaOnCallSiteRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaOnCallSiteRuleTest.kt index 2a774b1a42..68e0da4237 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaOnCallSiteRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaOnCallSiteRuleTest.kt @@ -1,19 +1,22 @@ package com.pinterest.ktlint.ruleset.standard +import com.pinterest.ktlint.core.RuleProvider import com.pinterest.ktlint.ruleset.standard.TrailingCommaOnCallSiteRule.Companion.allowTrailingCommaOnCallSiteProperty -import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule import com.pinterest.ktlint.test.LintViolation import org.junit.jupiter.api.Test class TrailingCommaOnCallSiteRuleTest { private val ruleAssertThat = - TrailingCommaOnCallSiteRule() - .assertThat( + assertThatRule( + provider = { TrailingCommaOnCallSiteRule() }, + additionalRuleProviders = setOf( // Apply the IndentationRule always as additional rule, so that the formattedCode in the unit test looks // correct. - IndentationRule() + RuleProvider { IndentationRule() } ) + ) @Test fun `Given property allow trailing comma on call site is not set then remove trailing comma's`() { @@ -376,6 +379,21 @@ class TrailingCommaOnCallSiteRuleTest { ).isFormattedAs(formattedCode) } + @Test + fun `Given that a trailing comma is required on call site then still it should not be added to the setter`() { + val code = + """ + class Test { + var foo = Bar() + set(value) { + } + } + """.trimIndent() + ruleAssertThat(code) + .withEditorConfigOverride(allowTrailingCommaOnCallSiteProperty to true) + .hasNoLintViolations() + } + @Test fun `1297 - Given that the trailing comma is required on call site the a trailing comma to collection literal when missing`() { val code = diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaOnDeclarationSiteRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaOnDeclarationSiteRuleTest.kt index 7e04de7d25..ce5dc9f7d0 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaOnDeclarationSiteRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaOnDeclarationSiteRuleTest.kt @@ -1,19 +1,22 @@ package com.pinterest.ktlint.ruleset.standard +import com.pinterest.ktlint.core.RuleProvider import com.pinterest.ktlint.ruleset.standard.TrailingCommaOnDeclarationSiteRule.Companion.allowTrailingCommaProperty -import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat +import com.pinterest.ktlint.test.KtLintAssertThat import com.pinterest.ktlint.test.LintViolation import org.junit.jupiter.api.Test class TrailingCommaOnDeclarationSiteRuleTest { - private val trailingCommaOnDeclarationSiteRuleAssertThat = - TrailingCommaOnDeclarationSiteRule() - .assertThat( + private val ruleAssertThat = + KtLintAssertThat.assertThatRule( + provider = { TrailingCommaOnDeclarationSiteRule() }, + additionalRuleProviders = setOf( // Apply the IndentationRule always as additional rule, so that the formattedCode in the unit test looks // correct. - IndentationRule() + RuleProvider { IndentationRule() } ) + ) @Test fun `Given property allow trailing comma on declaration site is not set then remove trailing comma's`() { @@ -59,7 +62,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { val foo6: (Int, Int) -> Int = { foo, bar -> foo * bar } """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .hasLintViolations( LintViolation(1, 29, "Unnecessary trailing comma before \")\""), LintViolation(3, 16, "Unnecessary trailing comma before \">\""), @@ -93,7 +96,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { val bar: Int /* The comma before the comment should be removed without removing the comment itself */ ) """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to false) .hasLintViolations( LintViolation(1, 29, "Unnecessary trailing comma before \")\""), @@ -124,7 +127,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { val bar: Int, /* The comma should be inserted before the comment */ ) """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to true) .hasLintViolations( LintViolation(3, 17, "Missing trailing comma before \")\""), @@ -158,7 +161,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { B /* The comma before the comment should be removed without removing the comment itself */ > {} """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to false) .hasLintViolations( LintViolation(1, 16, "Unnecessary trailing comma before \">\""), @@ -193,7 +196,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { B, /* The comma should be inserted before the comment */ > {} """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to true) .hasLintViolations( LintViolation(4, 6, "Missing trailing comma before \">\""), @@ -227,7 +230,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { else -> "b" } """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to false) .hasLintViolations( LintViolation(2, 9, "Unnecessary trailing comma before \"->\""), @@ -262,7 +265,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { else -> "b" } """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to true) .hasLintViolations( LintViolation(3, 9, "Missing trailing comma before \"->\""), @@ -304,7 +307,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { ) = bar() } """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to false) .hasLintViolations( LintViolation(4, 14, "Unnecessary trailing comma before \")\""), @@ -347,7 +350,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { ) = bar() } """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to true) .hasLintViolations( LintViolation(7, 10, "Missing trailing comma before \")\""), @@ -385,7 +388,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { -> foo * bar } """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to false) .hasLintViolations( LintViolation(1, 44, "Unnecessary trailing comma before \"->\""), @@ -424,7 +427,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { -> foo * bar } """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to true) .hasLintViolations( LintViolation(4, 12, "Missing trailing comma before \"->\""), @@ -432,6 +435,55 @@ class TrailingCommaOnDeclarationSiteRuleTest { ).isFormattedAs(formattedCode) } + @Test + fun `Given that a trailing comma is required on declaration site then add it to function declaration`() { + val code = + """ + fun test( + x: Int, + y: Int, + block: (Int, Int) -> Int + ): ( + Int, Int + ) -> Int = { foo, bar -> + block(foo, bar) + } + """.trimIndent() + val formattedCode = + """ + fun test( + x: Int, + y: Int, + block: (Int, Int) -> Int, + ): ( + Int, Int, + ) -> Int = { foo, bar -> + block(foo, bar) + } + """.trimIndent() + ruleAssertThat(code) + .withEditorConfigOverride(allowTrailingCommaProperty to true) + .hasLintViolations( + LintViolation(4, 29, "Missing trailing comma before \")\""), + LintViolation(6, 13, "Missing trailing comma before \")\""), + ).isFormattedAs(formattedCode) + } + + @Test + fun `Given that a trailing comma is required on declaration site then still it should not be added to the setter`() { + val code = + """ + class Test { + var foo = Bar() + set(value) { + } + } + """.trimIndent() + ruleAssertThat(code) + .withEditorConfigOverride(allowTrailingCommaProperty to true) + .hasNoLintViolations() + } + @Test fun `Issue 1312 - Given that a trailing comma is required on declaration site and multiple elements then force lambda arrow to next line`() { val code = @@ -458,7 +510,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { else -> "d" } """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) .withEditorConfigOverride(allowTrailingCommaProperty to true) .hasLintViolations( LintViolation(4, 6, "Missing trailing comma and newline before \"->\""), @@ -494,7 +546,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { val bar3: EnumThree, ) """.trimIndent() - trailingCommaOnDeclarationSiteRuleAssertThat(code) + ruleAssertThat(code) // When running format mode, the rules are first executed in parallel to find linting errors. In this // process, no unused import are found because the trailing comma is not yet added to variable "bar3". Then // in the next stage the rules are run consecutively. Now the trailing comma rule is adding a trailing comma @@ -502,7 +554,7 @@ class TrailingCommaOnDeclarationSiteRuleTest { // was incorrectly seen as part of the type of variable "bar3" and a reference "EnumThree," (with the // trailing comma was added) which in turn resulted in not recognizing that the import of EnumThree actually // was used. - .addAdditionalRules(NoUnusedImportsRule()) + .addAdditionalRuleProvider { NoUnusedImportsRule() } .withEditorConfigOverride(allowTrailingCommaProperty to true) .hasLintViolation(9, 24, "Missing trailing comma before \")\"") .isFormattedAs(formattedCode) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaRuleTest.kt deleted file mode 100644 index d9d568d390..0000000000 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/TrailingCommaRuleTest.kt +++ /dev/null @@ -1,95 +0,0 @@ -package com.pinterest.ktlint.ruleset.standard - -import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat -import com.pinterest.ktlint.test.LintViolation -import org.junit.jupiter.api.Test - -class TrailingCommaRuleTest { - - private val combinedTrailingCommaAssertThat = - TrailingCommaOnDeclarationSiteRule() - .assertThat( - TrailingCommaOnCallSiteRule(), - // Apply the IndentationRule always as additional rule, so that the formattedCode in the unit test looks - // correct. - IndentationRule() - ) - - // TBS? - @Test - fun `Given that properties to force trailing comma's on call and declaration site have been enabled`() { - val code = - """ - fun test( - x: Int, - y: Int, - block: (Int, Int) -> Int - ): ( - Int, Int - ) -> Int = when (x) { - 1, 2 - -> { - foo, - bar /* The comma should be inserted before the comment */ - -> - block( - foo * bar, - foo + bar - ) - } - else -> { _, _ -> block(0, 0) } - } - """.trimIndent() - val formattedCode = - """ - fun test( - x: Int, - y: Int, - block: (Int, Int) -> Int, - ): ( - Int, Int, - ) -> Int = when (x) { - 1, 2, - -> { - foo, - bar, /* The comma should be inserted before the comment */ - -> - block( - foo * bar, - foo + bar, - ) - } - else -> { _, _ -> block(0, 0) } - } - """.trimIndent() - combinedTrailingCommaAssertThat(code) - .withEditorConfigOverride(TrailingCommaOnCallSiteRule.allowTrailingCommaOnCallSiteProperty to true) - .withEditorConfigOverride(TrailingCommaOnDeclarationSiteRule.allowTrailingCommaProperty to true) - .hasLintViolations( - LintViolation(4, 29, "Missing trailing comma before \")\""), - LintViolation(6, 13, "Missing trailing comma before \")\""), - LintViolation(8, 9, "Missing trailing comma before \"->\""), - LintViolation(11, 16, "Missing trailing comma before \"->\"") - ) - .hasLintViolationsForAdditionalRules( - LintViolation(15, 22, "Missing trailing comma before \")\"") - ) - .isFormattedAs(formattedCode) - } - - @Test - fun `Given that a trailing comma is required on call site and declaration site then still it should not be added to the setter`() { - val code = - """ - class Test { - var foo = Bar() - set(value) { - } - } - """.trimIndent() - combinedTrailingCommaAssertThat(code) - .withEditorConfigOverride(TrailingCommaOnDeclarationSiteRule.allowTrailingCommaProperty to true) - .withEditorConfigOverride(TrailingCommaOnCallSiteRule.allowTrailingCommaOnCallSiteProperty to true) - .hasNoLintViolations() - } -}