Skip to content

Commit

Permalink
WRONG_INDENTATION: add more tests for extendedIndentAfterOperators
Browse files Browse the repository at this point in the history
### What's done:

 * Tests for expressions wrapped on an operator have been added.
 * Both cases (when `extendedIndentAfterOperators` is `true` and `false`) are
   tested.
 * Both binary operators and functions are tested.
 * Related: #1340
  • Loading branch information
0x6675636b796f75676974687562 committed Jun 24, 2022
1 parent dbb88ee commit 1c8e599
Show file tree
Hide file tree
Showing 7 changed files with 800 additions and 224 deletions.
5 changes: 5 additions & 0 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import org.cqfn.diktat.util.FixTestBase
import generated.WarningNames
import org.assertj.core.api.SoftAssertions.assertSoftly
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.MethodOrderer.MethodName
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestMethodOrder
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

import java.nio.file.Path

@TestMethodOrder(MethodName::class)
@TestMethodOrder(MethodOrderer.DisplayName::class)
class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
::IndentationRule,
listOf(
Expand Down Expand Up @@ -61,138 +65,6 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
fixAndCompare("MultilionStringExpected.kt", "MultilionStringTest.kt")
}

/**
* This test has a counterpart under [IndentationRuleWarnTest].
*
* See [#1330](https://github.com/saveourtool/diktat/issues/1330).
*/
@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `expression body functions should remain unchanged if properly indented (extendedIndentAfterOperators = true)`(@TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to true)

lintMultipleMethods(
expressionBodyFunctionsContinuationIndent,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* This test has a counterpart under [IndentationRuleWarnTest].
*
* See [#1330](https://github.com/saveourtool/diktat/issues/1330).
*/
@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `expression body functions should remain unchanged if properly indented (extendedIndentAfterOperators = false)`(@TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to false)

lintMultipleMethods(
expressionBodyFunctionsSingleIndent,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* This test has a counterpart under [IndentationRuleWarnTest].
*
* See [#1330](https://github.com/saveourtool/diktat/issues/1330).
*/
@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `expression body functions should be reformatted if mis-indented (extendedIndentAfterOperators = true)`(@TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to true)

lintMultipleMethods(
actualContent = expressionBodyFunctionsSingleIndent,
expectedContent = expressionBodyFunctionsContinuationIndent,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* This test has a counterpart under [IndentationRuleWarnTest].
*
* See [#1330](https://github.com/saveourtool/diktat/issues/1330).
*/
@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `expression body functions should be reformatted if mis-indented (extendedIndentAfterOperators = false)`(@TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to false)

lintMultipleMethods(
actualContent = expressionBodyFunctionsContinuationIndent,
expectedContent = expressionBodyFunctionsSingleIndent,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* See [#1347](https://github.com/saveourtool/diktat/issues/1347).
*/
@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `no whitespace should be injected into multi-line string literals (code matches settings, extendedIndent = true)`(@TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters(*extendedIndent(enabled = true))

lintMultipleMethods(
whitespaceInStringLiteralsContinuationIndent,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* See [#1347](https://github.com/saveourtool/diktat/issues/1347).
*/
@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `no whitespace should be injected into multi-line string literals (code matches settings, extendedIndent = false)`(@TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters(*extendedIndent(enabled = false))

lintMultipleMethods(
whitespaceInStringLiteralsSingleIndent,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* See [#1347](https://github.com/saveourtool/diktat/issues/1347).
*/
@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `no whitespace should be injected into multi-line string literals (mis-indented code reformatted, extendedIndent = true)`(@TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters(*extendedIndent(enabled = true))

lintMultipleMethods(
actualContent = whitespaceInStringLiteralsSingleIndent,
expectedContent = whitespaceInStringLiteralsContinuationIndent,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* See [#1347](https://github.com/saveourtool/diktat/issues/1347).
*/
@Test
@Tag(WarningNames.WRONG_INDENTATION)
fun `no whitespace should be injected into multi-line string literals (mis-indented code reformatted, extendedIndent = false)`(@TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters(*extendedIndent(enabled = false))

lintMultipleMethods(
actualContent = whitespaceInStringLiteralsContinuationIndent,
expectedContent = whitespaceInStringLiteralsSingleIndent,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* @param actualContent the original file content (may well be modified as
* fixes are applied).
Expand Down Expand Up @@ -230,4 +102,173 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
}
}
}

/**
* See [#1330](https://github.com/saveourtool/diktat/issues/1330).
*
* @see IndentationRuleWarnTest.ExpressionBodyFunctions
*/
@Nested
@DisplayName("Expression body functions")
@TestMethodOrder(MethodOrderer.DisplayName::class)
inner class ExpressionBodyFunctions {
/**
* @see IndentationRuleWarnTest.ExpressionBodyFunctions.shouldBeProperlyIndented
*/
@ParameterizedTest(name = "extendedIndentAfterOperators = {0}")
@ValueSource(booleans = [false, true])
@DisplayName("should remain unchanged if properly indented")
@Tag(WarningNames.WRONG_INDENTATION)
fun shouldRemainUnchangedIfProperlyIndented(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators)

val expressionBodyFunctions = when {
extendedIndentAfterOperators -> expressionBodyFunctionsContinuationIndent
else -> expressionBodyFunctionsSingleIndent
}

lintMultipleMethods(
expressionBodyFunctions,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* @see IndentationRuleWarnTest.ExpressionBodyFunctions.shouldBeReportedIfMisIndented
*/
@ParameterizedTest(name = "extendedIndentAfterOperators = {0}")
@ValueSource(booleans = [false, true])
@DisplayName("should be reformatted if mis-indented")
@Tag(WarningNames.WRONG_INDENTATION)
fun shouldBeReformattedIfMisIndented(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators)

val expressionBodyFunctionsActual = when {
extendedIndentAfterOperators -> expressionBodyFunctionsSingleIndent
else -> expressionBodyFunctionsContinuationIndent
}
val expressionBodyFunctionsExpected = when {
extendedIndentAfterOperators -> expressionBodyFunctionsContinuationIndent
else -> expressionBodyFunctionsSingleIndent
}

lintMultipleMethods(
actualContent = expressionBodyFunctionsActual,
expectedContent = expressionBodyFunctionsExpected,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}
}

/**
* See [#1347](https://github.com/saveourtool/diktat/issues/1347).
*/
@Nested
@DisplayName("Multi-line string literals")
@TestMethodOrder(MethodOrderer.DisplayName::class)
inner class MultilineStringLiterals {
@ParameterizedTest(name = "extendedIndent = {0}")
@ValueSource(booleans = [false, true])
@DisplayName("no whitespace should be injected (code matches settings)")
@Tag(WarningNames.WRONG_INDENTATION)
fun noWhitespaceShouldBeInjectedCodeMatchesSettings(extendedIndent: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters(*extendedIndent(enabled = extendedIndent))

val whitespaceInStringLiterals = when {
extendedIndent -> whitespaceInStringLiteralsContinuationIndent
else -> whitespaceInStringLiteralsSingleIndent
}

lintMultipleMethods(
whitespaceInStringLiterals,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

@ParameterizedTest(name = "extendedIndent = {0}")
@ValueSource(booleans = [false, true])
@DisplayName("no whitespace should be injected (mis-indented code reformatted)")
@Tag(WarningNames.WRONG_INDENTATION)
fun noWhitespaceShouldBeInjectedMisIndentedCodeReformatted(extendedIndent: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters(*extendedIndent(enabled = extendedIndent))

val whitespaceInStringLiteralsActual = when {
extendedIndent -> whitespaceInStringLiteralsSingleIndent
else -> whitespaceInStringLiteralsContinuationIndent
}
val whitespaceInStringLiteralsExpected = when {
extendedIndent -> whitespaceInStringLiteralsContinuationIndent
else -> whitespaceInStringLiteralsSingleIndent
}

lintMultipleMethods(
actualContent = whitespaceInStringLiteralsActual,
expectedContent = whitespaceInStringLiteralsExpected,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}
}

/**
* See [#1340](https://github.com/saveourtool/diktat/issues/1340).
*
* @see IndentationRuleWarnTest.ExpressionsWrappedAfterOperator
*/
@Nested
@DisplayName("Expressions wrapped after operator")
@TestMethodOrder(MethodOrderer.DisplayName::class)
inner class ExpressionsWrappedAfterOperator {
/**
* @see IndentationRuleWarnTest.ExpressionsWrappedAfterOperator.shouldBeProperlyIndented
*/
@ParameterizedTest(name = "extendedIndentAfterOperators = {0}")
@ValueSource(booleans = [false, true])
@DisplayName("should be properly indented")
@Tag(WarningNames.WRONG_INDENTATION)
fun shouldRemainUnchangedIfProperlyIndented(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators)

val expressionsWrappedAfterOperator = when {
extendedIndentAfterOperators -> expressionsWrappedAfterOperatorContinuationIndent
else -> expressionsWrappedAfterOperatorSingleIndent
}

lintMultipleMethods(
expressionsWrappedAfterOperator,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

/**
* @see IndentationRuleWarnTest.ExpressionsWrappedAfterOperator.shouldBeReportedIfMisIndented
*/
@ParameterizedTest(name = "extendedIndentAfterOperators = {0}")
@ValueSource(booleans = [false, true])
@DisplayName("should be reformatted if mis-indented")
@Tag(WarningNames.WRONG_INDENTATION)
fun shouldBeReformattedIfMisIndented(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig("newlineAtEnd" to false)
val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators)

val expressionsWrappedAfterOperatorActual = when {
extendedIndentAfterOperators -> expressionsWrappedAfterOperatorSingleIndent
else -> expressionsWrappedAfterOperatorContinuationIndent
}
val expressionsWrappedAfterOperatorExpected = when {
extendedIndentAfterOperators -> expressionsWrappedAfterOperatorContinuationIndent
else -> expressionsWrappedAfterOperatorSingleIndent
}

lintMultipleMethods(
actualContent = expressionsWrappedAfterOperatorActual,
expectedContent = expressionsWrappedAfterOperatorExpected,
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.cqfn.diktat.ruleset.chapter3.spaces

import org.junit.platform.suite.api.SelectClasses
import org.junit.platform.suite.api.Suite

@Suite
@SelectClasses(
IndentationRuleWarnTest::class,
IndentationRuleFixTest::class)
class IndentationRuleTest
Loading

0 comments on commit 1c8e599

Please sign in to comment.