Skip to content

Commit

Permalink
Fix codestyle warning && and more tests
Browse files Browse the repository at this point in the history
### What's done:
* Fix codestyle warning && and more tests
  • Loading branch information
kgevorkyan committed Apr 8, 2021
1 parent 84dfaf2 commit 08b65f8
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class CollapseIfStatementsRule(configRules: List<RulesConfig>) : DiktatRule(
it.elementType == EOL_COMMENT ||
it.elementType == BLOCK_COMMENT
}
?.toList() ?: listOf()
?.toList() ?: emptyList()
}

private fun collapse(parentNode: ASTNode, nestedNode: ASTNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,50 @@ class CollapseIfStatementsRuleWarnTest : LintTestBase(::CollapseIfStatementsRule
)
}

@Test
@Tag(WarningNames.COLLAPSE_IF_STATEMENTS)
fun `comments in compound cond`() {
lintMethod(
"""
|fun foo() {
| // comment
| if (cond1) {
| /*
| Some comments
| */
| // More comments
| if (cond2 || cond3) {
| doSomething()
| }
| }
|}
""".trimMargin(),
LintError(8, 10, ruleId, "${COLLAPSE_IF_STATEMENTS.warnText()} avoid using redundant nested if-statements", true)
)
}

@Test
@Tag(WarningNames.COLLAPSE_IF_STATEMENTS)
fun `comments in multiple if-statements`() {
lintMethod(
"""
|fun foo() {
| if (cond1) {
| // comment
| if (cond2) {
| // comment 2
| if (cond3) {
| doSomething()
| }
| }
| }
|}
""".trimMargin(),
LintError(4, 10, ruleId, "${COLLAPSE_IF_STATEMENTS.warnText()} avoid using redundant nested if-statements", true),
LintError(6, 10, ruleId, "${COLLAPSE_IF_STATEMENTS.warnText()} avoid using redundant nested if-statements", true)
)
}

@Test
@Tag(WarningNames.COLLAPSE_IF_STATEMENTS)
fun `not nested if`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,25 @@ true) {
doSomething()
}
}

fun foo() {
// comment
if (cond1 &&
/*
Some comments
*/
// More comments
(cond2 || cond3)) {
doSomething()
}
}

fun foo() {
if (cond1 &&
// comment
cond2 &&
// comment 2
cond3) {
doSomething()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,28 @@ fun foo() {
}
}
}

fun foo() {
// comment
if (cond1) {
/*
Some comments
*/
// More comments
if (cond2 || cond3) {
doSomething()
}
}
}

fun foo() {
if (cond1) {
// comment
if (cond2) {
// comment 2
if (cond3) {
doSomething()
}
}
}
}

0 comments on commit 08b65f8

Please sign in to comment.