Skip to content

Commit

Permalink
Rewrite WRONG_INDENTATION unit tests using the new test infrastructure
Browse files Browse the repository at this point in the history
### What's done:

 * Tests rewritten using test template annotations, increasing overall
   granularity.
  • Loading branch information
0x6675636b796f75676974687562 committed Aug 10, 2022
1 parent 59b65c8 commit d0d09a1
Show file tree
Hide file tree
Showing 11 changed files with 2,134 additions and 2,505 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
package org.cqfn.diktat.ruleset.chapter3.spaces

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestResources.dotQualifiedExpressions
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestResources.expressionBodyFunctions
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestResources.expressionsWrappedAfterOperator
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestResources.expressionsWrappedBeforeOperator
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestResources.parenthesesSurroundedInfixExpressions
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestResources.whitespaceInStringLiterals
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION
import org.cqfn.diktat.ruleset.junit.NaturalDisplayName
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationRule
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.ALIGNED_PARAMETERS
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_AFTER_OPERATORS
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_BEFORE_DOT
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_FOR_EXPRESSION_BODIES
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXTENDED_INDENT_OF_PARAMETERS
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.NEWLINE_AT_END
import org.cqfn.diktat.util.FixTestBase
import org.cqfn.diktat.util.assertNotNull

import generated.WarningNames
import org.assertj.core.api.SoftAssertions.assertSoftly
import org.intellij.lang.annotations.Language
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.CsvSource
import org.junit.jupiter.params.provider.ValueSource

import java.nio.file.Path

/**
* Legacy indentation tests.
*
* Consider adding new tests to [IndentationRuleTest] instead.
*
* @see IndentationRuleTest
*/
@TestMethodOrder(NaturalDisplayName::class)
class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
::IndentationRule,
Expand Down Expand Up @@ -79,264 +69,4 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
fun `multiline string`() {
fixAndCompare("MultilionStringExpected.kt", "MultilionStringTest.kt")
}

/**
* @param actualContent the original file content (may well be modified as
* fixes are applied).
* @param expectedContent the content the file is expected to have after the
* fixes are applied.
*/
private fun lintMultipleMethods(
@Language("kotlin") actualContent: Array<String>,
@Language("kotlin") expectedContent: Array<String> = actualContent,
tempDir: Path,
rulesConfigList: List<RulesConfig>? = null
) {
require(actualContent.isNotEmpty()) {
"code fragments is an empty array"
}

require(actualContent.size == expectedContent.size) {
"The actual and expected code fragments are arrays of different size: ${actualContent.size} != ${expectedContent.size}"
}

assertSoftly { softly ->
(actualContent.asSequenceWithConcatenation() zip
expectedContent.asSequenceWithConcatenation()).forEach { (actual, expected) ->
val lintResult = fixAndCompareContent(
actual,
expected,
tempDir,
rulesConfigList)

if (!lintResult.isSuccessful) {
softly.assertThat(lintResult.actualContent)
.describedAs("lint result for ${actual.describe()}")
.isEqualTo(lintResult.expectedContent)
}
}
}
}

/**
* See [#1330](https://github.com/saveourtool/diktat/issues/1330).
*/
@Nested
@TestMethodOrder(NaturalDisplayName::class)
inner class `Expression body functions` {
@ParameterizedTest(name = "$EXTENDED_INDENT_FOR_EXPRESSION_BODIES = {0}")
@ValueSource(booleans = [false, true])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should remain unchanged if properly indented`(extendedIndentForExpressionBodies: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val customConfig = defaultConfig.withCustomParameters(EXTENDED_INDENT_FOR_EXPRESSION_BODIES to extendedIndentForExpressionBodies)

lintMultipleMethods(
expressionBodyFunctions[extendedIndentForExpressionBodies].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

@ParameterizedTest(name = "$EXTENDED_INDENT_FOR_EXPRESSION_BODIES = {0}")
@ValueSource(booleans = [false, true])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should be reformatted if mis-indented`(extendedIndentForExpressionBodies: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val customConfig = defaultConfig.withCustomParameters(EXTENDED_INDENT_FOR_EXPRESSION_BODIES to extendedIndentForExpressionBodies)

lintMultipleMethods(
actualContent = expressionBodyFunctions[!extendedIndentForExpressionBodies].assertNotNull(),
expectedContent = expressionBodyFunctions[extendedIndentForExpressionBodies].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}
}

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

lintMultipleMethods(
whitespaceInStringLiterals[extendedIndent].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

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

lintMultipleMethods(
actualContent = whitespaceInStringLiterals[!extendedIndent].assertNotNull(),
expectedContent = whitespaceInStringLiterals[extendedIndent].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}
}

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

lintMultipleMethods(
expressionsWrappedAfterOperator[extendedIndentAfterOperators].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

@ParameterizedTest(name = "$EXTENDED_INDENT_AFTER_OPERATORS = {0}")
@ValueSource(booleans = [false, true])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should be reformatted if mis-indented`(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val customConfig = defaultConfig.withCustomParameters(EXTENDED_INDENT_AFTER_OPERATORS to extendedIndentAfterOperators)

lintMultipleMethods(
actualContent = expressionsWrappedAfterOperator[!extendedIndentAfterOperators].assertNotNull(),
expectedContent = expressionsWrappedAfterOperator[extendedIndentAfterOperators].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}
}

/**
* See [#1340](https://github.com/saveourtool/diktat/issues/1340).
*/
@Nested
@TestMethodOrder(NaturalDisplayName::class)
inner class `Expressions wrapped before operator` {
@ParameterizedTest(name = "$EXTENDED_INDENT_AFTER_OPERATORS = {0}")
@ValueSource(booleans = [false, true])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should be properly indented`(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val customConfig = defaultConfig.withCustomParameters(EXTENDED_INDENT_AFTER_OPERATORS to extendedIndentAfterOperators)

lintMultipleMethods(
expressionsWrappedBeforeOperator[extendedIndentAfterOperators].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

@ParameterizedTest(name = "$EXTENDED_INDENT_AFTER_OPERATORS = {0}")
@ValueSource(booleans = [false, true])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should be reformatted if mis-indented`(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val customConfig = defaultConfig.withCustomParameters(EXTENDED_INDENT_AFTER_OPERATORS to extendedIndentAfterOperators)

lintMultipleMethods(
actualContent = expressionsWrappedBeforeOperator[!extendedIndentAfterOperators].assertNotNull(),
expectedContent = expressionsWrappedBeforeOperator[extendedIndentAfterOperators].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}
}

/**
* See [#1409](https://github.com/saveourtool/diktat/issues/1409).
*/
@Nested
@TestMethodOrder(NaturalDisplayName::class)
inner class `Parentheses-surrounded infix expressions` {
@ParameterizedTest(name = "$EXTENDED_INDENT_FOR_EXPRESSION_BODIES = {0}, $EXTENDED_INDENT_AFTER_OPERATORS = {1}")
@CsvSource(value = ["false,true", "true,false"])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should be properly indented`(
extendedIndentForExpressionBodies: Boolean,
extendedIndentAfterOperators: Boolean,
@TempDir tempDir: Path,
) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val customConfig = defaultConfig.withCustomParameters(
EXTENDED_INDENT_FOR_EXPRESSION_BODIES to extendedIndentForExpressionBodies,
EXTENDED_INDENT_AFTER_OPERATORS to extendedIndentAfterOperators,
)

lintMultipleMethods(
parenthesesSurroundedInfixExpressions[IndentationConfig(customConfig)].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

@ParameterizedTest(name = "$EXTENDED_INDENT_FOR_EXPRESSION_BODIES = {0}, $EXTENDED_INDENT_AFTER_OPERATORS = {1}")
@CsvSource(value = ["false,true", "true,false"])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should be reformatted if mis-indented`(
extendedIndentForExpressionBodies: Boolean,
extendedIndentAfterOperators: Boolean,
@TempDir tempDir: Path,
) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val actualCodeStyle = defaultConfig.withCustomParameters(
EXTENDED_INDENT_FOR_EXPRESSION_BODIES to !extendedIndentForExpressionBodies,
EXTENDED_INDENT_AFTER_OPERATORS to !extendedIndentAfterOperators,
)
val desiredCodeStyle = defaultConfig.withCustomParameters(
EXTENDED_INDENT_FOR_EXPRESSION_BODIES to extendedIndentForExpressionBodies,
EXTENDED_INDENT_AFTER_OPERATORS to extendedIndentAfterOperators,
)

lintMultipleMethods(
actualContent = parenthesesSurroundedInfixExpressions[IndentationConfig(actualCodeStyle)].assertNotNull(),
expectedContent = parenthesesSurroundedInfixExpressions[IndentationConfig(desiredCodeStyle)].assertNotNull(),
tempDir = tempDir,
rulesConfigList = desiredCodeStyle.asRulesConfigList())
}
}

/**
* See [#1336](https://github.com/saveourtool/diktat/issues/1336).
*/
@Nested
@TestMethodOrder(NaturalDisplayName::class)
inner class `Dot- and safe-qualified expressions` {
@ParameterizedTest(name = "$EXTENDED_INDENT_BEFORE_DOT = {0}")
@ValueSource(booleans = [false, true])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should be properly indented`(extendedIndentBeforeDot: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val customConfig = defaultConfig.withCustomParameters(EXTENDED_INDENT_BEFORE_DOT to extendedIndentBeforeDot)

lintMultipleMethods(
dotQualifiedExpressions[extendedIndentBeforeDot].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}

@ParameterizedTest(name = "$EXTENDED_INDENT_BEFORE_DOT = {0}")
@ValueSource(booleans = [false, true])
@Tag(WarningNames.WRONG_INDENTATION)
fun `should be reformatted if mis-indented`(extendedIndentBeforeDot: Boolean, @TempDir tempDir: Path) {
val defaultConfig = IndentationConfig(NEWLINE_AT_END to false)
val customConfig = defaultConfig.withCustomParameters(EXTENDED_INDENT_BEFORE_DOT to extendedIndentBeforeDot)

lintMultipleMethods(
actualContent = dotQualifiedExpressions[!extendedIndentBeforeDot].assertNotNull(),
expectedContent = dotQualifiedExpressions[extendedIndentBeforeDot].assertNotNull(),
tempDir = tempDir,
rulesConfigList = customConfig.asRulesConfigList())
}
}
}
Loading

0 comments on commit d0d09a1

Please sign in to comment.