Skip to content

Commit

Permalink
[#1347] Clean up the code (#1376)
Browse files Browse the repository at this point in the history
### What's done:

 * Code cleaned up in preparation to fix #1347.
 * No logic is modified with this change.
  • Loading branch information
0x6675636b796f75676974687562 authored Jun 21, 2022
1 parent b2a52ab commit fcbb82b
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.cqfn.diktat.ruleset.rules.chapter3.files

/**
* @property expected expected indentation as a number of spaces
* @property actual actual indentation as a number of spaces
*/
internal data class IndentationError(val expected: Int, val actual: Int)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ package org.cqfn.diktat.ruleset.utils

import org.jetbrains.kotlin.lexer.KtTokens

internal const val NEWLINE = '\n'

internal const val SPACE = ' '

internal const val TAB = '\t'

@Suppress("VARIABLE_NAME_INCORRECT_FORMAT")
val JAVA = arrayOf("abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const",
Expand Down Expand Up @@ -97,3 +103,15 @@ fun String.removePrefix(): String {
}
return this
}

/**
* @return the indentation of the last line of this string.
*/
internal fun String.lastIndent() = substringAfterLast(NEWLINE).count(::isSpaceCharacter)

/**
* @param ch the character to examine.
* @return `true` if [ch] is a [SPACE], `false` otherwise.
*/
internal fun isSpaceCharacter(ch: Char): Boolean =
ch == SPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package org.cqfn.diktat.ruleset.utils.indentation

import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationError
import org.cqfn.diktat.ruleset.rules.chapter3.files.lastIndent
import org.cqfn.diktat.ruleset.utils.hasParent
import org.cqfn.diktat.ruleset.utils.lastIndent

import com.pinterest.ktlint.core.ast.ElementType.ARROW
import com.pinterest.ktlint.core.ast.ElementType.AS_KEYWORD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ internal class IndentationConfig(config: Map<String, String>) : RuleConfiguratio
/**
* The indentation size for each file
*/
val indentationSize = config["indentationSize"]?.toInt() ?: IndentationRule.INDENT_SIZE
val indentationSize = config["indentationSize"]?.toInt() ?: IndentationRule.DEFAULT_INDENT_SIZE
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation",

if (!lintResult.isSuccessful) {
softly.assertThat(lintResult.actualContent)
.describedAs("lint result for \"$actual\"")
.describedAs("lint result for ${actual.describe()}")
.isEqualTo(lintResult.expectedContent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,26 @@ internal interface IndentationRuleTestMixin {
}
}

/**
* @return a brief description of this code fragment.
*/
fun String.describe(): String {
val lines = splitToSequence('\n')

var first: String? = null

val count = lines.onEachIndexed { index, line ->
if (index == 0) {
first = line
}
}.count()

return when (count) {
1 -> "\"$this\""
else -> "\"$first\u2026\" ($count line(s))"
}
}

/**
* @return `true` if known-to-fail unit tests can be muted on the CI server.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule), IndentationRule
assertSoftly { softly ->
expressionBodyFunctionsSingleIndent.forEach { code ->
softly.assertThat(lintResult(code, customConfig.asRulesConfigList()))
.describedAs("lint result for \"$code\"")
.describedAs("lint result for ${code.describe()}")
.isNotEmpty
.hasSizeBetween(1, 3).allSatisfy(Consumer { lintError ->
assertThat(lintError.ruleId).describedAs("ruleId").isEqualTo(ruleId)
Expand All @@ -782,7 +782,7 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule), IndentationRule
assertSoftly { softly ->
expressionBodyFunctionsContinuationIndent.forEach { code ->
softly.assertThat(lintResult(code, customConfig.asRulesConfigList()))
.describedAs("lint result for \"$code\"")
.describedAs("lint result for ${code.describe()}")
.isNotEmpty
.hasSizeBetween(1, 3).allSatisfy(Consumer { lintError ->
assertThat(lintError.ruleId).describedAs("ruleId").isEqualTo(ruleId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -43,6 +44,7 @@ class DiktatRuleSetProviderTest {
.map { it.nameWithoutExtension }
.filterNot { it in ignoreFile }
val rulesName = DiktatRuleSetProvider().get()
.asSequence()
.onEachIndexed { index, rule ->
if (index != 0) {
Assertions.assertTrue(
Expand All @@ -53,8 +55,9 @@ class DiktatRuleSetProviderTest {
}
.map { (it as? DiktatRuleSetProvider.OrderedRule)?.rule ?: it }
.map { it::class.simpleName!! }
.filter { it != "DummyWarning" }
Assertions.assertEquals(filesName.sorted().toList(), rulesName.sorted())
.filterNot { it == "DummyWarning" }
.toList()
assertThat(rulesName.sorted()).containsExactlyElementsOf(filesName.sorted().toList())
}

@Test
Expand Down Expand Up @@ -122,6 +125,9 @@ class DiktatRuleSetProviderTest {
}

companion object {
private val ignoreFile = listOf("DiktatRuleSetProvider", "DiktatRule")
private val ignoreFile = listOf(
"DiktatRuleSetProvider",
"DiktatRule",
"IndentationError")
}
}

0 comments on commit fcbb82b

Please sign in to comment.