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

Make writing tests for WRONG_INDENTATION checks easier #1499

Merged
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
2 changes: 1 addition & 1 deletion diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
enabled: true
# all code blocks annotated with @Nested, @ParameterizedTest (JUnit 5) will
# be ignored and not checked.
ignoreAnnotated: [ Nested, ParameterizedTest ]
ignoreAnnotated: [ Nested, ParameterizedTest, IndentationTest ]
# Checks that a single line concatenation of strings is not used
- name: STRING_CONCATENATION
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ internal interface IndentationConfigAware {
* Calls the specified function [block] with [IndentationConfigAware] as
* its receiver and returns its result.
*
* @param configuration the indentation configuration.
* @param configuration the configuration for the indentation rule.
* @param block the function block to call.
* @return the result returned by the function block.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig
import org.cqfn.diktat.ruleset.utils.indentation.KdocIndentationChecker
import org.cqfn.diktat.ruleset.utils.indentation.SuperTypeListChecker
import org.cqfn.diktat.ruleset.utils.indentation.ValueParameterListChecker
import org.cqfn.diktat.ruleset.utils.isSpaceCharacter
import org.cqfn.diktat.ruleset.utils.lastIndent
import org.cqfn.diktat.ruleset.utils.leadingSpaceCount
import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine

import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION
Expand Down Expand Up @@ -783,14 +783,6 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
return this
}

/**
* @return the number of leading space characters in this string.
*/
private fun String.leadingSpaceCount(): Int =
asSequence()
.takeWhile(::isSpaceCharacter)
.count()

/**
* @return this very integer if non-negative, 0 otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
@file:Suppress(
"HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE",
"TOP_LEVEL_ORDER",
)
@file:Suppress("HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE")

package org.cqfn.diktat.ruleset.rules.chapter3.files

import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType

/**
* An [IElementType] along with the indentation change it induces.
*/
internal typealias IndentedElementType = Pair<IElementType, IndentationAmount>

/**
* @return the element type.
*/
Expand All @@ -27,3 +19,8 @@ internal val IndentedElementType.type: IElementType
internal val IndentedElementType.indentationChange: IndentationAmount
get() =
second

/**
* An [IElementType] along with the indentation change it induces.
*/
internal typealias IndentedElementType = Pair<IElementType, IndentationAmount>
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,17 @@ fun String.removePrefix(): String {
*/
internal fun String.lastIndent() = substringAfterLast(NEWLINE).count(::isSpaceCharacter)

/**
* @return the number of leading space characters in this string.
*/
internal fun String.leadingSpaceCount(): Int =
asSequence()
.takeWhile(::isSpaceCharacter)
.count()

/**
* @param ch the character to examine.
* @return `true` if [ch] is a [SPACE], `false` otherwise.
*/
internal fun isSpaceCharacter(ch: Char): Boolean =
private fun isSpaceCharacter(ch: Char): Boolean =
ch == SPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,20 @@ internal class IndentationConfig(config: Map<String, String>) : RuleConfiguratio
internal const val INDENTATION_SIZE = "indentationSize"
internal const val NEWLINE_AT_END = "newlineAtEnd"

@Suppress(
"CUSTOM_GETTERS_SETTERS",
"STRING_TEMPLATE_QUOTES",
)
@Suppress("CUSTOM_GETTERS_SETTERS")
private val IndentationConfig.configWithExplicitDefaults: Map<String, String>
get() =
mutableMapOf<String, String>().apply {
mutableMapOf<String, Any>().apply {
putAll(config)
putIfAbsent(ALIGNED_PARAMETERS, "$alignedParameters")
putIfAbsent(EXTENDED_INDENT_AFTER_OPERATORS, "$extendedIndentAfterOperators")
putIfAbsent(EXTENDED_INDENT_BEFORE_DOT, "$extendedIndentBeforeDot")
putIfAbsent(EXTENDED_INDENT_FOR_EXPRESSION_BODIES, "$extendedIndentForExpressionBodies")
putIfAbsent(EXTENDED_INDENT_OF_PARAMETERS, "$extendedIndentOfParameters")
putIfAbsent(INDENTATION_SIZE, "$indentationSize")
putIfAbsent(NEWLINE_AT_END, "$newlineAtEnd")
putIfAbsent(ALIGNED_PARAMETERS, alignedParameters)
putIfAbsent(EXTENDED_INDENT_AFTER_OPERATORS, extendedIndentAfterOperators)
putIfAbsent(EXTENDED_INDENT_BEFORE_DOT, extendedIndentBeforeDot)
putIfAbsent(EXTENDED_INDENT_FOR_EXPRESSION_BODIES, extendedIndentForExpressionBodies)
putIfAbsent(EXTENDED_INDENT_OF_PARAMETERS, extendedIndentOfParameters)
putIfAbsent(INDENTATION_SIZE, indentationSize)
putIfAbsent(NEWLINE_AT_END, newlineAtEnd)
}.mapValues { (_, value) ->
value.toString()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.cqfn.diktat.ruleset.chapter2

import org.cqfn.diktat.ruleset.chapter2.CommentsFormattingTest.Companion.indentStyleComment
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.describe
import org.cqfn.diktat.ruleset.chapter3.spaces.describe
import org.cqfn.diktat.ruleset.rules.chapter2.kdoc.CommentsFormatting
import org.cqfn.diktat.util.FixTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.cqfn.diktat.ruleset.chapter3.spaces

import org.cqfn.diktat.ruleset.junit.ExpectedLintError

/**
* The expected indentation error (extracted from annotated code fragments).
*
* @property line the line number (1-based).
* @property column the column number (1-based).
* @property expectedIndent the expected indentation level (in space characters).
* @property actualIndent the actual indentation level (in space characters).
*/
data class ExpectedIndentationError(
override val line: Int,
override val column: Int = 1,
val expectedIndent: Int,
val actualIndent: Int
) : ExpectedLintError
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
@file:Suppress("FILE_UNORDERED_IMPORTS")// False positives, see #1494.

package org.cqfn.diktat.ruleset.chapter3.spaces

import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.IndentationConfig
import org.cqfn.diktat.ruleset.junit.NaturalDisplayName
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.EXTENDED
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.NONE
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.SINGLE
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationConfigAware.Factory.withIndentationConfig
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.INDENTATION_SIZE

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.MethodOrderer.DisplayName
import org.junit.jupiter.api.TestMethodOrder
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

@TestMethodOrder(DisplayName::class)
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationConfigFactory as IndentationConfig

@TestMethodOrder(NaturalDisplayName::class)
class IndentationConfigAwareTest {
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
@ValueSource(ints = [2, 4, 8])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.cqfn.diktat.ruleset.chapter3.spaces

import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig

internal object IndentationConfigFactory {
/**
* Creates an `IndentationConfig` from zero or more
* [config entries][configEntries]. Invoke without arguments to create a
* default `IndentationConfig`.
*
* @param configEntries the configuration entries to create this instance from.
* @return the configuration created.
* @see [IndentationConfig]
*/
operator fun invoke(vararg configEntries: Pair<String, Any>): IndentationConfig =
IndentationConfig(mapOf(*configEntries).mapValues { (_, value) ->
value.toString()
})
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package org.cqfn.diktat.ruleset.chapter3.spaces

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.IndentationConfig
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.asRulesConfigList
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.asSequenceWithConcatenation
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.assertNotNull
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.describe
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.extendedIndent
import org.cqfn.diktat.ruleset.chapter3.spaces.IndentationRuleTestMixin.withCustomParameters
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
Expand All @@ -24,11 +18,11 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.EXT
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.MethodOrderer.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
Expand All @@ -40,7 +34,7 @@ import org.junit.jupiter.params.provider.ValueSource

import java.nio.file.Path

@TestMethodOrder(DisplayName::class)
@TestMethodOrder(NaturalDisplayName::class)
class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
::IndentationRule,
listOf(
Expand Down Expand Up @@ -128,7 +122,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
* See [#1330](https://github.com/saveourtool/diktat/issues/1330).
*/
@Nested
@TestMethodOrder(DisplayName::class)
@TestMethodOrder(NaturalDisplayName::class)
inner class `Expression body functions` {
@ParameterizedTest(name = "$EXTENDED_INDENT_FOR_EXPRESSION_BODIES = {0}")
@ValueSource(booleans = [false, true])
Expand Down Expand Up @@ -162,7 +156,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
* See [#1347](https://github.com/saveourtool/diktat/issues/1347).
*/
@Nested
@TestMethodOrder(DisplayName::class)
@TestMethodOrder(NaturalDisplayName::class)
inner class `Multi-line string literals` {
@ParameterizedTest(name = "extendedIndent = {0}")
@ValueSource(booleans = [false, true])
Expand Down Expand Up @@ -196,7 +190,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
* See [#1340](https://github.com/saveourtool/diktat/issues/1340).
*/
@Nested
@TestMethodOrder(DisplayName::class)
@TestMethodOrder(NaturalDisplayName::class)
inner class `Expressions wrapped after operator` {
@ParameterizedTest(name = "$EXTENDED_INDENT_AFTER_OPERATORS = {0}")
@ValueSource(booleans = [false, true])
Expand Down Expand Up @@ -230,7 +224,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
* See [#1340](https://github.com/saveourtool/diktat/issues/1340).
*/
@Nested
@TestMethodOrder(DisplayName::class)
@TestMethodOrder(NaturalDisplayName::class)
inner class `Expressions wrapped before operator` {
@ParameterizedTest(name = "$EXTENDED_INDENT_AFTER_OPERATORS = {0}")
@ValueSource(booleans = [false, true])
Expand Down Expand Up @@ -264,7 +258,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
* See [#1409](https://github.com/saveourtool/diktat/issues/1409).
*/
@Nested
@TestMethodOrder(DisplayName::class)
@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"])
Expand Down Expand Up @@ -316,7 +310,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",
* See [#1336](https://github.com/saveourtool/diktat/issues/1336).
*/
@Nested
@TestMethodOrder(DisplayName::class)
@TestMethodOrder(NaturalDisplayName::class)
inner class `Dot- and safe-qualified expressions` {
@ParameterizedTest(name = "$EXTENDED_INDENT_BEFORE_DOT = {0}")
@ValueSource(booleans = [false, true])
Expand Down
Loading