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

Move to JSR compilation for rule Specs #1732

Merged
merged 1 commit into from
Jun 29, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.isThresholded
import io.gitlab.arturbosch.detekt.test.lint
import org.spekframework.spek2.Spek
Expand Down Expand Up @@ -60,30 +61,25 @@ class ComplexMethodSpec : Spek({
val config = TestConfig(mapOf(ComplexMethod.IGNORE_SIMPLE_WHEN_ENTRIES to "true"))
val subject = ComplexMethod(config)
val code = """
internal fun Map<String, Any>.asBundle(): Bundle {
val bundle = Bundle(size)

for ((key, value) in this) {
val transformedKey = key.asFcmId()

when (value) {
is Int -> bundle.putInt(transformedKey, value)
is String -> bundle.putString(transformedKey, value)
is Float -> bundle.putFloat(transformedKey, value)
is Double -> bundle.putDouble(transformedKey, value)
is Byte -> bundle.putByte(transformedKey, value)
is Short -> bundle.putShort(transformedKey, value)
is Long -> bundle.putLong(transformedKey, value)
is Boolean -> bundle.putBoolean(transformedKey, value)
else -> throw IllegalArgumentException("Unexpected type value")
}
}

return bundle
}
""".trimIndent()

val findings = subject.lint(code)
fun f() {
val map = HashMap<Any, String>()
for ((key, value) in map) {
when (key) {
is Int -> print("int")
is String -> print("String")
is Float -> print("Float")
is Double -> print("Double")
is Byte -> print("Byte")
is Short -> print("Short")
is Long -> print("Long")
is Boolean -> print("Boolean")
else -> throw IllegalArgumentException("Unexpected type value")
}
}
}
"""

val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}
}
Expand Down Expand Up @@ -139,7 +135,7 @@ class ComplexMethodSpec : Spek({
""".trimIndent()

it("should not count these overridden functions to base functions complexity") {
assertThat(ComplexMethod().lint(code)).isEmpty()
assertThat(ComplexMethod().compileAndLint(code)).isEmpty()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.rules.complexity

import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
Expand Down Expand Up @@ -30,9 +31,10 @@ class LabeledExpressionSpec : Spek({
it("does not report excluded label") {
val code = """fun f() {
loop@ for (i in 1..5) {}
}
"""
val config = TestConfig(mapOf(LabeledExpression.IGNORED_LABELS to "loop"))
val findings = LabeledExpression(config).lint(code)
val findings = LabeledExpression(config).compileAndLint(code)
assertThat(findings).isEmpty()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.gitlab.arturbosch.detekt.rules.complexity

import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
Expand All @@ -26,7 +27,7 @@ class MethodOverloadingSpec : Spek({
}

it("does not report overloaded methods which do not exceed the threshold") {
subject.lint("""
subject.compileAndLint("""
class Test {
fun x() { }
fun x(i: Int) { }
Expand All @@ -38,15 +39,15 @@ class MethodOverloadingSpec : Spek({
context("several overloaded extensions functions") {

it("does not report extension methods with a different receiver") {
subject.lint("""
subject.compileAndLint("""
fun Boolean.foo() {}
fun Int.foo() {}
fun Long.foo() {}""")
assertThat(subject.findings.size).isZero()
}

it("reports extension methods with the same receiver") {
subject.lint("""
subject.compileAndLint("""
fun Int.foo() {}
fun Int.foo(i: Int) {}
fun Int.foo(i: String) {}""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.rules.complexity

import io.gitlab.arturbosch.detekt.api.ThresholdedCodeSmell
import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
Expand Down Expand Up @@ -33,7 +34,7 @@ class NestedBlockDepthSpec : Spek({
}
}
}"""
assertThat(subject.lint(code)).hasSize(1)
assertThat(subject.compileAndLint(code)).hasSize(1)
}

it("should not detect valid nested block depth") {
Expand All @@ -46,7 +47,7 @@ class NestedBlockDepthSpec : Spek({
}
}
}"""
assertThat(subject.lint(code)).isEmpty()
assertThat(subject.compileAndLint(code)).isEmpty()
}

it("should not count else if as two") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.rules.empty

import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.rules.providers.EmptyCodeProvider
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileForTest
import io.gitlab.arturbosch.detekt.test.lint
import io.gitlab.arturbosch.detekt.test.yamlConfig
Expand Down Expand Up @@ -43,7 +44,7 @@ class EmptyBlocksMultiRuleSpec : Spek({
}

it("reports no duplicated findings - issue #1605") {
val findings = subject.lint("""
val findings = subject.compileAndLint("""
class EmptyBlocks {
class EmptyClass {}
fun exceptionHandling() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.compileForTest
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -20,7 +21,7 @@ class EmptyCodeSpec : Spek({
val regexTestingCode = """
fun f() {
try {
} catch (foo: MyException) {
} catch (foo: Exception) {
}
}"""

Expand All @@ -34,35 +35,35 @@ class EmptyCodeSpec : Spek({
val code = """
fun f() {
try {
} catch (ignore: IOException) {
} catch (ignore: Exception) {
try {
} catch (e: IOException) {
} catch (e: Exception) {
}
}
}"""
assertThat(EmptyCatchBlock(Config.empty).lint(code)).hasSize(1)
assertThat(EmptyCatchBlock(Config.empty).compileAndLint(code)).hasSize(1)
}

it("doesNotReportIgnoredOrExpectedException") {
val code = """
fun f() {
try {
} catch (ignore: IOException) {
} catch (ignore: IllegalArgumentException) {
} catch (expected: Exception) {
}
}"""
assertThat(EmptyCatchBlock(Config.empty).lint(code)).isEmpty()
assertThat(EmptyCatchBlock(Config.empty).compileAndLint(code)).isEmpty()
}

it("doesNotReportEmptyCatchWithConfig") {
val code = """
fun f() {
try {
} catch (foo: MyException) {
} catch (foo: Exception) {
}
}"""
val config = TestConfig(mapOf(EmptyCatchBlock.ALLOWED_EXCEPTION_NAME_REGEX to "foo"))
assertThat(EmptyCatchBlock(config).lint(code)).isEmpty()
assertThat(EmptyCatchBlock(config).compileAndLint(code)).isEmpty()
}

it("findsEmptyFinally") {
Expand Down Expand Up @@ -111,28 +112,28 @@ class EmptyCodeSpec : Spek({

it("findsEmptyDefaultConstructor") {
val rule = EmptyDefaultConstructor(Config.empty)
val text = compileForTest(Case.EmptyDefaultConstructorPositive.path()).text
assertThat(rule.lint(text)).hasSize(2)
val file = compileForTest(Case.EmptyDefaultConstructorPositive.path())
assertThat(rule.lint(file)).hasSize(2)
}

it("doesNotFindEmptyDefaultConstructor") {
val rule = EmptyDefaultConstructor(Config.empty)
val text = compileForTest(Case.EmptyDefaultConstructorNegative.path()).text
assertThat(rule.lint(text)).isEmpty()
val file = compileForTest(Case.EmptyDefaultConstructorNegative.path())
assertThat(rule.lint(file)).isEmpty()
}

it("doesNotFailWithInvalidRegexWhenDisabled") {
val configValues = mapOf("active" to "false",
EmptyCatchBlock.ALLOWED_EXCEPTION_NAME_REGEX to "*foo")
val config = TestConfig(configValues)
assertThat(EmptyCatchBlock(config).lint(regexTestingCode)).isEmpty()
assertThat(EmptyCatchBlock(config).compileAndLint(regexTestingCode)).isEmpty()
}

it("doesFailWithInvalidRegex") {
val configValues = mapOf(EmptyCatchBlock.ALLOWED_EXCEPTION_NAME_REGEX to "*foo")
val config = TestConfig(configValues)
assertThatExceptionOfType(PatternSyntaxException::class.java).isThrownBy {
EmptyCatchBlock(config).lint(regexTestingCode)
EmptyCatchBlock(config).compileAndLint(regexTestingCode)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.rules.exceptions

import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
Expand All @@ -28,7 +29,7 @@ class ExceptionRaisedInUnexpectedLocationSpec : Spek({

it("reports the configured method") {
val config = TestConfig(mapOf(ExceptionRaisedInUnexpectedLocation.METHOD_NAMES to "toDo,todo2"))
val findings = ExceptionRaisedInUnexpectedLocation(config).lint("""
val findings = ExceptionRaisedInUnexpectedLocation(config).compileAndLint("""
fun toDo() {
throw IllegalStateException()
}""")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.gitlab.arturbosch.detekt.rules.exceptions

import io.gitlab.arturbosch.detekt.test.lint
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
Expand All @@ -11,46 +11,48 @@ class InstanceOfCheckForExceptionSpec : Spek({
describe("InstanceOfCheckForException rule") {

it("has is and as checks") {

val code = """
fun x() {
try {
} catch(e: IOException) {
if (e is MyException || (e as MyException) != null) {
return
}
}
} catch(e: Exception) {
if (e is IllegalArgumentException || (e as IllegalArgumentException) != null) {
return
}
}
}
"""
assertThat(subject.lint(code)).hasSize(2)
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("has nested is and as checks") {
val code = """
fun x() {
try {
} catch(e: IOException) {
} catch(e: Exception) {
if (1 == 1) {
val b = e !is MyException || (e as MyException) != null
val b = e !is IllegalArgumentException || (e as IllegalArgumentException) != null
}
}
}
"""
assertThat(subject.lint(code)).hasSize(2)
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("has no instance of check") {
val code = """
fun x() {
try {
} catch(e: IOException) {
} catch(e: Exception) {
val s = ""
if (s is String || (s as String) != null) {
val b = s !is MyException || (s as MyException) != null
val other: Exception? = null
val b = other !is IllegalArgumentException || (other as IllegalArgumentException) != null
}
}
}
"""
assertThat(subject.lint(code)).hasSize(0)
assertThat(subject.compileAndLint(code)).hasSize(0)
}
}
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.gitlab.arturbosch.detekt.rules.exceptions

import io.gitlab.arturbosch.detekt.test.lint
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
Expand All @@ -16,7 +16,7 @@ class NotImplementedDeclarationSpec : Spek({
if (1 == 1) throw NotImplementedError()
throw NotImplementedError()
}"""
assertThat(subject.lint(code)).hasSize(2)
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("reports TODO method calls") {
Expand All @@ -25,15 +25,15 @@ class NotImplementedDeclarationSpec : Spek({
TODO("not implemented")
TODO()
}"""
assertThat(subject.lint(code)).hasSize(2)
assertThat(subject.compileAndLint(code)).hasSize(2)
}

it("does not report TODO comments") {
val code = """
fun f() {
// TODO
}"""
assertThat(subject.lint(code)).hasSize(0)
assertThat(subject.compileAndLint(code)).hasSize(0)
}
}
})