-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing and rule improvement for EmptyElseBlock (#4349)
* Added tests for empty else-block; Consolidated logic for empty if- and else-block rules * Added KDoc for EmptyIfElseBlock.kt * Removed EmptyIfElseBlock and replaced with a shared utility function * Fixed Detekt issue
- Loading branch information
1 parent
e4e2b5b
commit ed829aa
Showing
4 changed files
with
134 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...rules-empty/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/empty/EmptyElseBlockSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package io.gitlab.arturbosch.detekt.rules.empty | ||
|
||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.test.compileAndLint | ||
import org.assertj.core.api.Assertions | ||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.specification.describe | ||
|
||
class EmptyElseBlockSpec : Spek({ | ||
|
||
val subject by memoized { EmptyElseBlock(Config.empty) } | ||
|
||
describe("EmptyElseBlock rule") { | ||
it("reports empty else block") { | ||
val code = """ | ||
fun f() { | ||
val i = 0 | ||
if (i == 0) { | ||
println(i) | ||
} else { | ||
} | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
|
||
it("reports empty else blocks with trailing semicolon") { | ||
val code = """ | ||
fun f() { | ||
val i = 0 | ||
if (i == 0) { | ||
println(i) | ||
} else ; | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
it("reports empty else with trailing semicolon on new line") { | ||
val code = """ | ||
fun f() { | ||
var i = 0 | ||
if (i == 0) { | ||
println(i) | ||
} else | ||
; | ||
i++ | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
|
||
it("reports empty else with trailing semicolon and braces") { | ||
val code = """ | ||
fun f() { | ||
var i = 0 | ||
if (i == 0) { | ||
println() | ||
} else; { | ||
} | ||
i++ | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLint(code)).hasSize(1) | ||
} | ||
|
||
it("does not report nonempty else with braces") { | ||
val code = """ | ||
fun f() { | ||
var i = 0 | ||
if (i == 0) { | ||
println(i) | ||
} else { | ||
i++ | ||
} | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("does not report nonempty else without braces") { | ||
val code = """ | ||
fun f() { | ||
var i = 0 | ||
if (i == 0) { | ||
println(i) | ||
} else i++ | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
|
||
it("does not report nonempty else without braces but semicolon") { | ||
val code = """ | ||
fun f() { | ||
var i = 0 | ||
if (i == 0) { | ||
println(i) | ||
} else i++; | ||
} | ||
""" | ||
Assertions.assertThat(subject.compileAndLint(code)).isEmpty() | ||
} | ||
} | ||
}) |