mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
Migrate detekt-rules-naming tests to JUnit (#4574)
This commit is contained in:
@@ -5,6 +5,5 @@ plugins {
|
||||
dependencies {
|
||||
compileOnly(projects.detektApi)
|
||||
testImplementation(projects.detektTest)
|
||||
testImplementation(libs.bundles.testImplementation)
|
||||
testRuntimeOnly(libs.spek.runner)
|
||||
testImplementation(libs.assertj)
|
||||
}
|
||||
|
||||
@@ -1,59 +1,65 @@
|
||||
package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
|
||||
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
|
||||
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.compileAndLintWithContext
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class BooleanPropertyNamingSpec : Spek({
|
||||
setupKotlinEnvironment()
|
||||
@KotlinCoreEnvironmentTest
|
||||
class BooleanPropertyNamingSpec(val env: KotlinCoreEnvironment) {
|
||||
val subject = BooleanPropertyNaming()
|
||||
|
||||
val subject by memoized { BooleanPropertyNaming() }
|
||||
val env: KotlinCoreEnvironment by memoized()
|
||||
@Nested
|
||||
inner class `BooleanPropertyNaming rule` {
|
||||
|
||||
describe("BooleanPropertyNaming rule") {
|
||||
|
||||
context("argument declarations") {
|
||||
it("should warn about Kotlin Boolean") {
|
||||
@Nested
|
||||
inner class `argument declarations` {
|
||||
@Test
|
||||
fun `should warn about Kotlin Boolean`() {
|
||||
val code = """data class Test (var default: Boolean)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about Kotlin Boolean nullable") {
|
||||
@Test
|
||||
fun `should warn about Kotlin Boolean nullable`() {
|
||||
val code = """data class Test (var default: Boolean?)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about Kotlin Boolean initialized") {
|
||||
@Test
|
||||
fun `should warn about Kotlin Boolean initialized`() {
|
||||
val code = """data class Test (var default: Boolean = false)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about Java Boolean") {
|
||||
@Test
|
||||
fun `should warn about Java Boolean`() {
|
||||
val code = """data class Test (var default: java.lang.Boolean)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not detect primitive types") {
|
||||
@Test
|
||||
fun `should not detect primitive types`() {
|
||||
val code = """data class Test (var count: Int)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect names that match an allowed pattern") {
|
||||
@Test
|
||||
fun `should not detect names that match an allowed pattern`() {
|
||||
val code = """data class Test (var isEnabled: Boolean, var hasDefault: Boolean)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
@@ -61,8 +67,10 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
context("property declarations") {
|
||||
it("should warn about Kotlin Boolean") {
|
||||
@Nested
|
||||
inner class `property declarations` {
|
||||
@Test
|
||||
fun `should warn about Kotlin Boolean`() {
|
||||
val code = """
|
||||
class Test {
|
||||
var default: Boolean = true
|
||||
@@ -73,7 +81,8 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about Kotlin Boolean nullable") {
|
||||
@Test
|
||||
fun `should warn about Kotlin Boolean nullable`() {
|
||||
val code = """
|
||||
class Test {
|
||||
var default: Boolean? = null
|
||||
@@ -84,7 +93,8 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about Kotlin Boolean initialized") {
|
||||
@Test
|
||||
fun `should warn about Kotlin Boolean initialized`() {
|
||||
val code = """
|
||||
class Test {
|
||||
var default: Boolean = false
|
||||
@@ -95,7 +105,8 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about inferred boolean type") {
|
||||
@Test
|
||||
fun `should warn about inferred boolean type`() {
|
||||
val code = """
|
||||
class Test {
|
||||
var default = true
|
||||
@@ -106,7 +117,8 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about Java Boolean") {
|
||||
@Test
|
||||
fun `should warn about Java Boolean`() {
|
||||
val code = """
|
||||
class Test {
|
||||
var default: java.lang.Boolean = java.lang.Boolean(true)
|
||||
@@ -117,7 +129,8 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not detect primitive types") {
|
||||
@Test
|
||||
fun `should not detect primitive types`() {
|
||||
val code = """
|
||||
class Test {
|
||||
var count: Int = 0
|
||||
@@ -128,7 +141,8 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect names that match an allowed pattern") {
|
||||
@Test
|
||||
fun `should not detect names that match an allowed pattern`() {
|
||||
val code = """
|
||||
class Test {
|
||||
var isEnabled: Boolean = true
|
||||
@@ -140,7 +154,8 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect names that match an allowed pattern from config") {
|
||||
@Test
|
||||
fun `should not detect names that match an allowed pattern from config`() {
|
||||
val code = """
|
||||
class Test {
|
||||
var needReload: Boolean = true
|
||||
@@ -153,6 +168,6 @@ class BooleanPropertyNamingSpec : Spek({
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private const val ALLOWED_PATTERN = "allowedPattern"
|
||||
|
||||
@@ -2,14 +2,16 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class ClassNamingSpec : Spek({
|
||||
class ClassNamingSpec {
|
||||
|
||||
describe("different naming conventions inside classes") {
|
||||
@Nested
|
||||
inner class `different naming conventions inside classes` {
|
||||
|
||||
it("should detect no violations class with numbers") {
|
||||
@Test
|
||||
fun `should detect no violations class with numbers`() {
|
||||
val code = """
|
||||
class MyClassWithNumbers5
|
||||
"""
|
||||
@@ -17,7 +19,8 @@ class ClassNamingSpec : Spek({
|
||||
assertThat(ClassNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect no violations") {
|
||||
@Test
|
||||
fun `should detect no violations`() {
|
||||
val code = """
|
||||
class NamingConventions {
|
||||
}
|
||||
@@ -26,7 +29,8 @@ class ClassNamingSpec : Spek({
|
||||
assertThat(ClassNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect no violations with class using backticks") {
|
||||
@Test
|
||||
fun `should detect no violations with class using backticks`() {
|
||||
val code = """
|
||||
class `NamingConventions`
|
||||
"""
|
||||
@@ -34,7 +38,8 @@ class ClassNamingSpec : Spek({
|
||||
assertThat(ClassNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect because it have a _") {
|
||||
@Test
|
||||
fun `should detect because it have a _`() {
|
||||
val code = """
|
||||
class _NamingConventions
|
||||
"""
|
||||
@@ -44,7 +49,8 @@ class ClassNamingSpec : Spek({
|
||||
.hasTextLocations(6 to 24)
|
||||
}
|
||||
|
||||
it("should detect because it have starts with lowercase") {
|
||||
@Test
|
||||
fun `should detect because it have starts with lowercase`() {
|
||||
val code = """
|
||||
class namingConventions {}
|
||||
"""
|
||||
@@ -54,7 +60,8 @@ class ClassNamingSpec : Spek({
|
||||
.hasTextLocations(6 to 23)
|
||||
}
|
||||
|
||||
it("should ignore the issue by alias suppression") {
|
||||
@Test
|
||||
fun `should ignore the issue by alias suppression`() {
|
||||
val code = """
|
||||
@Suppress("ClassName")
|
||||
class namingConventions {}
|
||||
@@ -62,4 +69,4 @@ class ClassNamingSpec : Spek({
|
||||
assertThat(ClassNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,14 +3,16 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class ConstructorParameterNamingSpec : Spek({
|
||||
class ConstructorParameterNamingSpec {
|
||||
|
||||
describe("parameters in a constructor of a class") {
|
||||
@Nested
|
||||
inner class `parameters in a constructor of a class` {
|
||||
|
||||
it("should detect no violations") {
|
||||
@Test
|
||||
fun `should detect no violations`() {
|
||||
val code = """
|
||||
class C(val param: String, private val privateParam: String)
|
||||
|
||||
@@ -22,7 +24,8 @@ class ConstructorParameterNamingSpec : Spek({
|
||||
assertThat(ConstructorParameterNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should find some violations") {
|
||||
@Test
|
||||
fun `should find some violations`() {
|
||||
val code = """
|
||||
class C(val PARAM: String, private val PRIVATE_PARAM: String)
|
||||
|
||||
@@ -34,14 +37,16 @@ class ConstructorParameterNamingSpec : Spek({
|
||||
assertThat(ConstructorParameterNaming().compileAndLint(code)).hasSize(5)
|
||||
}
|
||||
|
||||
it("should find a violation in the correct text location") {
|
||||
@Test
|
||||
fun `should find a violation in the correct text location`() {
|
||||
val code = """
|
||||
class C(val PARAM: String)
|
||||
"""
|
||||
assertThat(ConstructorParameterNaming().compileAndLint(code)).hasTextLocations(8 to 25)
|
||||
}
|
||||
|
||||
it("should not complain about override") {
|
||||
@Test
|
||||
fun `should not complain about override by default`() {
|
||||
val code = """
|
||||
class C(override val PARAM: String) : I
|
||||
|
||||
@@ -50,7 +55,8 @@ class ConstructorParameterNamingSpec : Spek({
|
||||
assertThat(ConstructorParameterNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should not complain about override") {
|
||||
@Test
|
||||
fun `should not complain about override when ignore overridden = false`() {
|
||||
val code = """
|
||||
class C(override val PARAM: String) : I
|
||||
|
||||
@@ -60,6 +66,6 @@ class ConstructorParameterNamingSpec : Spek({
|
||||
assertThat(ConstructorParameterNaming(config).compileAndLint(code)).hasTextLocations(8 to 34)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private const val IGNORE_OVERRIDDEN = "ignoreOverridden"
|
||||
|
||||
@@ -2,14 +2,16 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class EnumNamingSpec : Spek({
|
||||
class EnumNamingSpec {
|
||||
|
||||
describe("some enum entry declarations") {
|
||||
@Nested
|
||||
inner class `some enum entry declarations` {
|
||||
|
||||
it("should detect no violation") {
|
||||
@Test
|
||||
fun `should detect no violation`() {
|
||||
val findings = EnumNaming().compileAndLint(
|
||||
"""
|
||||
enum class WorkFlow {
|
||||
@@ -20,7 +22,8 @@ class EnumNamingSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("enum name that start with lowercase") {
|
||||
@Test
|
||||
fun `enum name that start with lowercase`() {
|
||||
val code = """
|
||||
enum class WorkFlow {
|
||||
default
|
||||
@@ -28,7 +31,8 @@ class EnumNamingSpec : Spek({
|
||||
assertThat(NamingRules().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("reports an underscore in enum name") {
|
||||
@Test
|
||||
fun `reports an underscore in enum name`() {
|
||||
val code = """
|
||||
enum class WorkFlow {
|
||||
_Default
|
||||
@@ -36,7 +40,8 @@ class EnumNamingSpec : Spek({
|
||||
assertThat(EnumNaming().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("no reports an underscore in enum name because it's suppressed") {
|
||||
@Test
|
||||
fun `no reports an underscore in enum name because it's suppressed`() {
|
||||
val code = """
|
||||
enum class WorkFlow {
|
||||
@Suppress("EnumNaming") _Default
|
||||
@@ -44,7 +49,8 @@ class EnumNamingSpec : Spek({
|
||||
assertThat(EnumNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("reports the correct text location in enum name") {
|
||||
@Test
|
||||
fun `reports the correct text location in enum name`() {
|
||||
val code = """
|
||||
enum class WorkFlow {
|
||||
_Default,
|
||||
@@ -53,4 +59,4 @@ class EnumNamingSpec : Spek({
|
||||
assertThat(findings).hasTextLocations(26 to 34)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,16 +3,18 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
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
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
private const val FORBIDDEN_NAME = "forbiddenName"
|
||||
|
||||
class ForbiddenClassNameSpec : Spek({
|
||||
class ForbiddenClassNameSpec {
|
||||
|
||||
describe("ForbiddenClassName rule") {
|
||||
@Nested
|
||||
inner class `ForbiddenClassName rule` {
|
||||
|
||||
it("should report classes with forbidden names") {
|
||||
@Test
|
||||
fun `should report classes with forbidden names`() {
|
||||
val code = """
|
||||
class TestManager {} // violation
|
||||
class TestProvider {} // violation
|
||||
@@ -24,7 +26,8 @@ class ForbiddenClassNameSpec : Spek({
|
||||
.hasSize(2)
|
||||
}
|
||||
|
||||
it("should report a class that starts with a forbidden name") {
|
||||
@Test
|
||||
fun `should report a class that starts with a forbidden name`() {
|
||||
val code = "class TestProvider {}"
|
||||
assertThat(
|
||||
ForbiddenClassName(TestConfig(mapOf(FORBIDDEN_NAME to listOf("test"))))
|
||||
@@ -33,7 +36,8 @@ class ForbiddenClassNameSpec : Spek({
|
||||
.hasSize(1)
|
||||
}
|
||||
|
||||
it("should report classes with forbidden names using config string") {
|
||||
@Test
|
||||
fun `should report classes with forbidden names using config string`() {
|
||||
val code = """
|
||||
class TestManager {} // violation
|
||||
class TestProvider {} // violation
|
||||
@@ -45,7 +49,8 @@ class ForbiddenClassNameSpec : Spek({
|
||||
.hasSize(2)
|
||||
}
|
||||
|
||||
it("should report classes with forbidden names using config string using wildcards") {
|
||||
@Test
|
||||
fun `should report classes with forbidden names using config string using wildcards`() {
|
||||
val code = """
|
||||
class TestManager {} // violation
|
||||
class TestProvider {} // violation
|
||||
@@ -56,7 +61,9 @@ class ForbiddenClassNameSpec : Spek({
|
||||
)
|
||||
.hasSize(2)
|
||||
}
|
||||
it("should report all forbidden names in message") {
|
||||
|
||||
@Test
|
||||
fun `should report all forbidden names in message`() {
|
||||
val code = """
|
||||
class TestManager {}"""
|
||||
val actual = ForbiddenClassName(TestConfig(mapOf(FORBIDDEN_NAME to "Test, Manager, Provider")))
|
||||
@@ -65,4 +72,4 @@ class ForbiddenClassNameSpec : Spek({
|
||||
.isEqualTo("Class name TestManager is forbidden as it contains: Test, Manager")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,14 +4,16 @@ import io.gitlab.arturbosch.detekt.api.SourceLocation
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class FunctionNamingSpec : Spek({
|
||||
class FunctionNamingSpec {
|
||||
|
||||
describe("FunctionNaming rule") {
|
||||
@Nested
|
||||
inner class `FunctionNaming rule` {
|
||||
|
||||
it("allows FunctionName as alias for suppressing") {
|
||||
@Test
|
||||
fun `allows FunctionName as alias for suppressing`() {
|
||||
val code = """
|
||||
@Suppress("FunctionName")
|
||||
fun MY_FUN() {}
|
||||
@@ -19,7 +21,8 @@ class FunctionNamingSpec : Spek({
|
||||
assertThat(FunctionNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("allows anonymous functions") {
|
||||
@Test
|
||||
fun `allows anonymous functions`() {
|
||||
val code = """
|
||||
val f: (Int) -> Int = fun(i: Int): Int {
|
||||
return i + i
|
||||
@@ -28,7 +31,8 @@ class FunctionNamingSpec : Spek({
|
||||
assertThat(FunctionNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("ignores functions in classes matching excludeClassPattern") {
|
||||
@Test
|
||||
fun `ignores functions in classes matching excludeClassPattern`() {
|
||||
val code = """
|
||||
class WhateverTest {
|
||||
fun SHOULD_NOT_BE_FLAGGED() {}
|
||||
@@ -38,7 +42,8 @@ class FunctionNamingSpec : Spek({
|
||||
assertThat(FunctionNaming(config).compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("flags functions inside functions") {
|
||||
@Test
|
||||
fun `flags functions inside functions`() {
|
||||
val code = """
|
||||
class C : I {
|
||||
override fun shouldNotBeFlagged() {
|
||||
@@ -50,7 +55,8 @@ class FunctionNamingSpec : Spek({
|
||||
assertThat(FunctionNaming().compileAndLint(code)).hasSourceLocation(3, 13)
|
||||
}
|
||||
|
||||
it("ignores overridden functions by default") {
|
||||
@Test
|
||||
fun `ignores overridden functions by default`() {
|
||||
val code = """
|
||||
class C : I {
|
||||
override fun SHOULD_NOT_BE_FLAGGED() {}
|
||||
@@ -60,7 +66,8 @@ class FunctionNamingSpec : Spek({
|
||||
assertThat(FunctionNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("does not report when the function name is identical to the type of the result") {
|
||||
@Test
|
||||
fun `does not report when the function name is identical to the type of the result`() {
|
||||
val code = """
|
||||
interface Foo
|
||||
private class FooImpl : Foo
|
||||
@@ -71,7 +78,8 @@ class FunctionNamingSpec : Spek({
|
||||
assertThat(FunctionNaming(config).compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("flags functions with bad names inside overridden functions by default") {
|
||||
@Test
|
||||
fun `flags functions with bad names inside overridden functions by default`() {
|
||||
val code = """
|
||||
class C : I {
|
||||
override fun SHOULD_BE_FLAGGED() {
|
||||
@@ -83,7 +91,8 @@ class FunctionNamingSpec : Spek({
|
||||
assertThat(FunctionNaming().compileAndLint(code)).hasSourceLocation(3, 13)
|
||||
}
|
||||
|
||||
it("doesn't ignore overridden functions if ignoreOverridden is false") {
|
||||
@Test
|
||||
fun `doesn't ignore overridden functions if ignoreOverridden is false`() {
|
||||
val code = """
|
||||
class C : I {
|
||||
override fun SHOULD_BE_FLAGGED() {}
|
||||
@@ -97,11 +106,12 @@ class FunctionNamingSpec : Spek({
|
||||
)
|
||||
}
|
||||
|
||||
it("doesn't allow functions with backtick") {
|
||||
@Test
|
||||
fun `doesn't allow functions with backtick`() {
|
||||
val code = """
|
||||
fun `7his is a function name _`() = Unit
|
||||
"""
|
||||
assertThat(FunctionNaming().compileAndLint(code)).hasSourceLocations(SourceLocation(1, 5))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,17 +3,19 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
private const val EXCLUDE_CLASS_PATTERN = "excludeClassPattern"
|
||||
private const val IGNORE_OVERRIDDEN = "ignoreOverridden"
|
||||
|
||||
object FunctionParameterNamingSpec : Spek({
|
||||
class FunctionParameterNamingSpec {
|
||||
|
||||
describe("parameters in a function of a class") {
|
||||
@Nested
|
||||
inner class `parameters in a function of a class` {
|
||||
|
||||
it("should detect no violations") {
|
||||
@Test
|
||||
fun `should detect no violations`() {
|
||||
val code = """
|
||||
class C {
|
||||
fun someStuff(param: String) {}
|
||||
@@ -22,7 +24,8 @@ object FunctionParameterNamingSpec : Spek({
|
||||
assertThat(FunctionParameterNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect violations in overridden function by default") {
|
||||
@Test
|
||||
fun `should not detect violations in overridden function by default`() {
|
||||
val code = """
|
||||
class C : I {
|
||||
override fun someStuff(`object`: String) {}
|
||||
@@ -32,7 +35,8 @@ object FunctionParameterNamingSpec : Spek({
|
||||
assertThat(FunctionParameterNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect violations in overridden function if ignoreOverridden is false") {
|
||||
@Test
|
||||
fun `should detect violations in overridden function if ignoreOverridden is false`() {
|
||||
val code = """
|
||||
class C : I {
|
||||
override fun someStuff(`object`: String) {}
|
||||
@@ -43,7 +47,8 @@ object FunctionParameterNamingSpec : Spek({
|
||||
assertThat(FunctionParameterNaming(config).compileAndLint(code)).hasSize(2)
|
||||
}
|
||||
|
||||
it("should find some violations") {
|
||||
@Test
|
||||
fun `should find some violations`() {
|
||||
val code = """
|
||||
class C {
|
||||
fun someStuff(PARAM: String) {}
|
||||
@@ -53,11 +58,13 @@ object FunctionParameterNamingSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
describe("parameters in a function of an excluded class") {
|
||||
@Nested
|
||||
inner class `parameters in a function of an excluded class` {
|
||||
|
||||
val config by memoized { TestConfig(mapOf(EXCLUDE_CLASS_PATTERN to "Excluded")) }
|
||||
val config = TestConfig(mapOf(EXCLUDE_CLASS_PATTERN to "Excluded"))
|
||||
|
||||
it("should not detect function parameter") {
|
||||
@Test
|
||||
fun `should not detect function parameter`() {
|
||||
val code = """
|
||||
class Excluded {
|
||||
fun f(PARAM: Int) {}
|
||||
@@ -66,9 +73,10 @@ object FunctionParameterNamingSpec : Spek({
|
||||
assertThat(FunctionParameterNaming(config).compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect constructor parameter") {
|
||||
@Test
|
||||
fun `should not detect constructor parameter`() {
|
||||
val code = "class Excluded(val PARAM: Int) {}"
|
||||
assertThat(FunctionParameterNaming(config).compileAndLint(code)).isEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,19 +4,21 @@ import io.github.detekt.test.utils.compileContentForTest
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.lint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.nio.file.FileSystems
|
||||
import java.nio.file.Paths
|
||||
|
||||
private const val ROOT_PACKAGE = "rootPackage"
|
||||
private const val REQUIRE_ROOT_PACKAGE = "requireRootInDeclaration"
|
||||
|
||||
internal class InvalidPackageDeclarationSpec : Spek({
|
||||
class InvalidPackageDeclarationSpec {
|
||||
|
||||
describe("InvalidPackageDeclaration rule") {
|
||||
@Nested
|
||||
inner class `InvalidPackageDeclaration rule` {
|
||||
|
||||
it("should pass if package declaration is correct") {
|
||||
@Test
|
||||
fun `should pass if package declaration is correct`() {
|
||||
val source = """
|
||||
package foo.bar
|
||||
|
||||
@@ -29,7 +31,8 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should report if package declaration does not match source location") {
|
||||
@Test
|
||||
fun `should report if package declaration does not match source location`() {
|
||||
val source = "package foo\n\nclass C"
|
||||
|
||||
val ktFile = compileContentForTest(source, createPath("project/src/bar/File.kt"))
|
||||
@@ -39,11 +42,13 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
assertThat(findings).hasTextLocations(0 to 11)
|
||||
}
|
||||
|
||||
describe("with root package specified") {
|
||||
@Nested
|
||||
inner class `with root package specified` {
|
||||
|
||||
val config by memoized { TestConfig(mapOf(ROOT_PACKAGE to "com.example")) }
|
||||
val config = TestConfig(mapOf(ROOT_PACKAGE to "com.example"))
|
||||
|
||||
it("should pass if file is located within the root package") {
|
||||
@Test
|
||||
fun `should pass if file is located within the root package`() {
|
||||
val source = """
|
||||
package com.example
|
||||
|
||||
@@ -56,7 +61,8 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass if file is located relative to root package") {
|
||||
@Test
|
||||
fun `should pass if file is located relative to root package`() {
|
||||
val source = """
|
||||
package com.example.foo.bar
|
||||
|
||||
@@ -69,7 +75,8 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass if file is located in directory corresponding to package declaration") {
|
||||
@Test
|
||||
fun `should pass if file is located in directory corresponding to package declaration`() {
|
||||
val source = """
|
||||
package com.example.foo.bar
|
||||
|
||||
@@ -82,7 +89,8 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should report if package declaration does not match") {
|
||||
@Test
|
||||
fun `should report if package declaration does not match`() {
|
||||
val source = """
|
||||
package com.example.foo.baz
|
||||
|
||||
@@ -95,7 +103,8 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should report if file path matches root package but package declaration differs") {
|
||||
@Test
|
||||
fun `should report if file path matches root package but package declaration differs`() {
|
||||
val source = """
|
||||
package io.foo.bar
|
||||
|
||||
@@ -109,11 +118,13 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
describe("with root package required") {
|
||||
@Nested
|
||||
inner class `with root package required` {
|
||||
|
||||
val config by memoized { TestConfig(mapOf(ROOT_PACKAGE to "com.example", REQUIRE_ROOT_PACKAGE to true)) }
|
||||
val config = TestConfig(mapOf(ROOT_PACKAGE to "com.example", REQUIRE_ROOT_PACKAGE to true))
|
||||
|
||||
it("should pass if declaration starts with root package") {
|
||||
@Test
|
||||
fun `should pass if declaration starts with root package`() {
|
||||
val source = """
|
||||
package com.example.foo.bar
|
||||
|
||||
@@ -131,7 +142,8 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
assertThat(findingsForFullPath).isEmpty()
|
||||
}
|
||||
|
||||
it("should report if root package is missing") {
|
||||
@Test
|
||||
fun `should report if root package is missing`() {
|
||||
val source = """
|
||||
package foo.bar
|
||||
|
||||
@@ -145,7 +157,7 @@ internal class InvalidPackageDeclarationSpec : Spek({
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun createPath(universalPath: String): String {
|
||||
val pathSegments = universalPath.split('/')
|
||||
|
||||
@@ -2,13 +2,15 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
object LambdaParameterNamingSpec : Spek({
|
||||
class LambdaParameterNamingSpec {
|
||||
|
||||
describe("lambda parameters") {
|
||||
it("Reports no supported parameter names") {
|
||||
@Nested
|
||||
inner class `lambda parameters` {
|
||||
@Test
|
||||
fun `Reports no supported parameter names`() {
|
||||
val code = """
|
||||
val a: (String) -> Unit = { HELLO_THERE -> Unit }
|
||||
"""
|
||||
@@ -17,7 +19,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.hasTextLocations("HELLO_THERE")
|
||||
}
|
||||
|
||||
it("Reports no supported parameter names when there are multiple") {
|
||||
@Test
|
||||
fun `Reports no supported parameter names when there are multiple`() {
|
||||
val code = """
|
||||
val a: (String, Int) -> Unit = { HI, HELLO_THERE -> Unit }
|
||||
"""
|
||||
@@ -26,7 +29,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.hasTextLocations("HI", "HELLO_THERE")
|
||||
}
|
||||
|
||||
it("Doesn't report a valid parameter") {
|
||||
@Test
|
||||
fun `Doesn't report a valid parameter`() {
|
||||
val code = """
|
||||
val a: (String) -> Unit = { helloThere -> Unit }
|
||||
"""
|
||||
@@ -34,7 +38,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.isEmpty()
|
||||
}
|
||||
|
||||
it("Doesn't report a valid parameter when define type") {
|
||||
@Test
|
||||
fun `Doesn't report a valid parameter when define type`() {
|
||||
val code = """
|
||||
val a = { helloThere: String -> Unit }
|
||||
"""
|
||||
@@ -42,7 +47,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.isEmpty()
|
||||
}
|
||||
|
||||
it("Doesn't report _") {
|
||||
@Test
|
||||
fun `Doesn't report _`() {
|
||||
val code = """
|
||||
val a: (String) -> Unit = { _ -> Unit }
|
||||
"""
|
||||
@@ -50,7 +56,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.isEmpty()
|
||||
}
|
||||
|
||||
it("Doesn't report by using implicit name") {
|
||||
@Test
|
||||
fun `Doesn't report by using implicit name`() {
|
||||
val code = """
|
||||
val a: (String) -> Unit = { Unit }
|
||||
"""
|
||||
@@ -58,7 +65,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.isEmpty()
|
||||
}
|
||||
|
||||
it("Doesn't report if there aren't parameters") {
|
||||
@Test
|
||||
fun `Doesn't report if there aren't parameters`() {
|
||||
val code = """
|
||||
val a: () -> Unit = { Unit }
|
||||
"""
|
||||
@@ -66,7 +74,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.isEmpty()
|
||||
}
|
||||
|
||||
it("Reports no supported destructuring parameter names") {
|
||||
@Test
|
||||
fun `Reports no supported destructuring parameter names`() {
|
||||
val code = """
|
||||
data class Bar(val a: String)
|
||||
val a: (Bar) -> Unit = { (HELLO_THERE) -> Unit }
|
||||
@@ -76,7 +85,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.hasTextLocations("HELLO_THERE")
|
||||
}
|
||||
|
||||
it("Reports no supported destructuring parameter names when there are multiple") {
|
||||
@Test
|
||||
fun `Reports no supported destructuring parameter names when there are multiple`() {
|
||||
val code = """
|
||||
data class Bar(val a: String, val b: String)
|
||||
val a: (Bar) -> Unit = { (HI, HELLO_THERE) -> Unit }
|
||||
@@ -86,7 +96,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.hasTextLocations("HI", "HELLO_THERE")
|
||||
}
|
||||
|
||||
it("Doesn't report valid destructuring parameters") {
|
||||
@Test
|
||||
fun `Doesn't report valid destructuring parameters`() {
|
||||
val code = """
|
||||
data class Bar(val a: String, val b: String)
|
||||
val a: (Bar) -> Unit = { (a, b) -> Unit }
|
||||
@@ -95,7 +106,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.isEmpty()
|
||||
}
|
||||
|
||||
it("Doesn't report valid destructuring parameters when define type") {
|
||||
@Test
|
||||
fun `Doesn't report valid destructuring parameters when define type`() {
|
||||
val code = """
|
||||
data class Bar(val a: String, val b: String)
|
||||
val a: (Bar) -> Unit = { (a: String, b) -> Unit }
|
||||
@@ -104,7 +116,8 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.isEmpty()
|
||||
}
|
||||
|
||||
it("Doesn't report valid destructuring parameters using _") {
|
||||
@Test
|
||||
fun `Doesn't report valid destructuring parameters using _`() {
|
||||
val code = """
|
||||
data class Bar(val a: String, val b: String)
|
||||
val a: (Bar) -> Unit = { (_, b) -> Unit }
|
||||
@@ -113,4 +126,4 @@ object LambdaParameterNamingSpec : Spek({
|
||||
.isEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,22 +4,26 @@ import io.github.detekt.test.utils.compileContentForTest
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.lint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class MatchingDeclarationNameSpec : Spek({
|
||||
class MatchingDeclarationNameSpec {
|
||||
|
||||
describe("MatchingDeclarationName rule") {
|
||||
@Nested
|
||||
inner class `MatchingDeclarationName rule` {
|
||||
|
||||
context("compliant test cases") {
|
||||
@Nested
|
||||
inner class `compliant test cases` {
|
||||
|
||||
it("should pass for object declaration") {
|
||||
@Test
|
||||
fun `should pass for object declaration`() {
|
||||
val ktFile = compileContentForTest("object O", filename = "O.kt")
|
||||
val findings = MatchingDeclarationName().lint(ktFile)
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for suppress") {
|
||||
@Test
|
||||
fun `should pass for suppress`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""@file:Suppress("MatchingDeclarationName") object O""",
|
||||
filename = "Objects.kt"
|
||||
@@ -28,19 +32,22 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for class declaration") {
|
||||
@Test
|
||||
fun `should pass for class declaration`() {
|
||||
val ktFile = compileContentForTest("class C", filename = "C.kt")
|
||||
val findings = MatchingDeclarationName().lint(ktFile)
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for interface declaration") {
|
||||
@Test
|
||||
fun `should pass for interface declaration`() {
|
||||
val ktFile = compileContentForTest("interface I", filename = "I.kt")
|
||||
val findings = MatchingDeclarationName().lint(ktFile)
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for enum declaration") {
|
||||
@Test
|
||||
fun `should pass for enum declaration`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""
|
||||
enum class E {
|
||||
@@ -53,7 +60,8 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for multiple declaration") {
|
||||
@Test
|
||||
fun `should pass for multiple declaration`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""
|
||||
class C
|
||||
@@ -66,7 +74,8 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for class declaration with utility functions") {
|
||||
@Test
|
||||
fun `should pass for class declaration with utility functions`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""
|
||||
class C
|
||||
@@ -79,7 +88,8 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for class declaration not as first declaration with utility functions") {
|
||||
@Test
|
||||
fun `should pass for class declaration not as first declaration with utility functions`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""
|
||||
fun a() = 5
|
||||
@@ -92,7 +102,8 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for private class declaration") {
|
||||
@Test
|
||||
fun `should pass for private class declaration`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""
|
||||
private class C
|
||||
@@ -104,7 +115,8 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should pass for a class with a typealias") {
|
||||
@Test
|
||||
fun `should pass for a class with a typealias`() {
|
||||
val code = """
|
||||
typealias Foo = FooImpl
|
||||
|
||||
@@ -115,15 +127,18 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
context("non-compliant test cases") {
|
||||
@Nested
|
||||
inner class `non-compliant test cases` {
|
||||
|
||||
it("should not pass for object declaration") {
|
||||
@Test
|
||||
fun `should not pass for object declaration`() {
|
||||
val ktFile = compileContentForTest("object O", filename = "Objects.kt")
|
||||
val findings = MatchingDeclarationName().lint(ktFile)
|
||||
assertThat(findings).hasSourceLocation(1, 1)
|
||||
}
|
||||
|
||||
it("should not pass for object declaration even with suppress on the object") {
|
||||
@Test
|
||||
fun `should not pass for object declaration even with suppress on the object`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""@Suppress("MatchingDeclarationName") object O""",
|
||||
filename = "Objects.kt"
|
||||
@@ -132,13 +147,15 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).hasSourceLocation(1, 1)
|
||||
}
|
||||
|
||||
it("should not pass for class declaration") {
|
||||
@Test
|
||||
fun `should not pass for class declaration`() {
|
||||
val ktFile = compileContentForTest("class C", filename = "Classes.kt")
|
||||
val findings = MatchingDeclarationName().lint(ktFile)
|
||||
assertThat(findings).hasSourceLocation(1, 1)
|
||||
}
|
||||
|
||||
it("should not pass for class declaration as first declaration with utility functions") {
|
||||
@Test
|
||||
fun `should not pass for class declaration as first declaration with utility functions`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""
|
||||
class C
|
||||
@@ -151,13 +168,15 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).hasSourceLocation(1, 1)
|
||||
}
|
||||
|
||||
it("should not pass for interface declaration") {
|
||||
@Test
|
||||
fun `should not pass for interface declaration`() {
|
||||
val ktFile = compileContentForTest("interface I", filename = "Not_I.kt")
|
||||
val findings = MatchingDeclarationName().lint(ktFile)
|
||||
assertThat(findings).hasSourceLocation(1, 1)
|
||||
}
|
||||
|
||||
it("should not pass for enum declaration") {
|
||||
@Test
|
||||
fun `should not pass for enum declaration`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""
|
||||
enum class NOT_E {
|
||||
@@ -170,7 +189,8 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).hasSourceLocation(1, 1)
|
||||
}
|
||||
|
||||
it("should not pass for a typealias with a different name") {
|
||||
@Test
|
||||
fun `should not pass for a typealias with a different name`() {
|
||||
val code = """
|
||||
class FooImpl {}
|
||||
typealias Bar = FooImpl
|
||||
@@ -180,12 +200,8 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it(
|
||||
"""
|
||||
should not pass for class declaration not as first declaration with utility functions
|
||||
when mustBeFirst is false.
|
||||
""".trimIndent()
|
||||
) {
|
||||
@Test
|
||||
fun `should not pass for class declaration not as first declaration with utility functions when mustBeFirst is false`() {
|
||||
val ktFile = compileContentForTest(
|
||||
"""
|
||||
fun a() = 5
|
||||
@@ -201,4 +217,4 @@ internal class MatchingDeclarationNameSpec : Spek({
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,36 +1,37 @@
|
||||
package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.api.Config
|
||||
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
|
||||
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
|
||||
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.compileAndLintWithContext
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
private const val IGNORE_OVERRIDDEN = "ignoreOverridden"
|
||||
|
||||
class MemberNameEqualsClassNameSpec : Spek({
|
||||
setupKotlinEnvironment()
|
||||
@KotlinCoreEnvironmentTest
|
||||
class MemberNameEqualsClassNameSpec(val env: KotlinCoreEnvironment) {
|
||||
val subject = MemberNameEqualsClassName(Config.empty)
|
||||
|
||||
val env: KotlinCoreEnvironment by memoized()
|
||||
val subject by memoized { MemberNameEqualsClassName(Config.empty) }
|
||||
@Nested
|
||||
inner class `MemberNameEqualsClassName rule` {
|
||||
|
||||
describe("MemberNameEqualsClassName rule") {
|
||||
|
||||
val noIgnoreOverridden by memoized {
|
||||
val noIgnoreOverridden =
|
||||
TestConfig(
|
||||
mapOf(
|
||||
IGNORE_OVERRIDDEN to "false"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
context("some classes with methods which don't have the same name") {
|
||||
@Nested
|
||||
inner class `some classes with methods which don't have the same name` {
|
||||
|
||||
it("does not report a nested function with the same name as the class") {
|
||||
@Test
|
||||
fun `does not report a nested function with the same name as the class`() {
|
||||
val code = """
|
||||
class MethodNameNotEqualsClassName {
|
||||
fun nestedFunction() {
|
||||
@@ -41,7 +42,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
|
||||
}
|
||||
|
||||
it("does not report a function with same name in nested class") {
|
||||
@Test
|
||||
fun `does not report a function with same name in nested class`() {
|
||||
val code = """
|
||||
class MethodNameNotEqualsClassName {
|
||||
class NestedNameEqualsTopClassName {
|
||||
@@ -52,7 +54,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
|
||||
}
|
||||
|
||||
it("does not report a function with the same name as a companion object") {
|
||||
@Test
|
||||
fun `does not report a function with the same name as a companion object`() {
|
||||
val code = """
|
||||
class StaticMethodNameEqualsObjectName {
|
||||
companion object A {
|
||||
@@ -64,9 +67,11 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
context("some classes with members which have the same name") {
|
||||
@Nested
|
||||
inner class `some classes with members which have the same name` {
|
||||
|
||||
it("reports a method which is named after the class") {
|
||||
@Test
|
||||
fun `reports a method which is named after the class`() {
|
||||
val code = """
|
||||
class MethodNameEqualsClassName {
|
||||
fun methodNameEqualsClassName() {}
|
||||
@@ -75,7 +80,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("reports a method which is named after the object") {
|
||||
@Test
|
||||
fun `reports a method which is named after the object`() {
|
||||
val code = """
|
||||
object MethodNameEqualsObjectName {
|
||||
fun MethodNameEqualsObjectName() {}
|
||||
@@ -84,7 +90,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("reports a property which is named after the class") {
|
||||
@Test
|
||||
fun `reports a property which is named after the class`() {
|
||||
val code = """
|
||||
class PropertyNameEqualsClassName {
|
||||
val propertyNameEqualsClassName = 0
|
||||
@@ -93,7 +100,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("reports a property which is named after the object") {
|
||||
@Test
|
||||
fun `reports a property which is named after the object`() {
|
||||
val code = """
|
||||
object PropertyNameEqualsObjectName {
|
||||
val propertyNameEqualsObjectName = 0
|
||||
@@ -102,7 +110,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("reports a companion object function which is named after the class") {
|
||||
@Test
|
||||
fun `reports a companion object function which is named after the class`() {
|
||||
val code = """
|
||||
class StaticMethodNameEqualsClassName {
|
||||
companion object {
|
||||
@@ -113,7 +122,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("reports a method which is named after the class even when it's inside another one") {
|
||||
@Test
|
||||
fun `reports a method which is named after the class even when it's inside another one`() {
|
||||
val code = """
|
||||
class MethodNameContainer {
|
||||
class MethodNameEqualsNestedClassName {
|
||||
@@ -124,7 +134,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("doesn't report overridden methods which are named after the class") {
|
||||
@Test
|
||||
fun `doesn't report overridden methods which are named after the class`() {
|
||||
val code = """
|
||||
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
|
||||
override fun AbstractMethodNameEqualsClassName() {}
|
||||
@@ -136,7 +147,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("doesn't report an methods which are named after the interface") {
|
||||
@Test
|
||||
fun `doesn't report an methods which are named after the interface`() {
|
||||
val code = """
|
||||
interface MethodNameEqualsInterfaceName {
|
||||
fun MethodNameEqualsInterfaceName() {}
|
||||
@@ -145,7 +157,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("reports overridden methods which are named after the class if they are not ignored") {
|
||||
@Test
|
||||
fun `reports overridden methods which are named after the class if they are not ignored`() {
|
||||
val code = """
|
||||
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
|
||||
override fun AbstractMethodNameEqualsClassName() {}
|
||||
@@ -157,7 +170,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName(noIgnoreOverridden).compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("doesn't report overridden properties which are named after the class") {
|
||||
@Test
|
||||
fun `doesn't report overridden properties which are named after the class`() {
|
||||
val code = """
|
||||
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
|
||||
override val AbstractMethodNameEqualsClassName = ""
|
||||
@@ -169,7 +183,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("reports overridden properties which are named after the class if they are not ignored") {
|
||||
@Test
|
||||
fun `reports overridden properties which are named after the class if they are not ignored`() {
|
||||
val code = """
|
||||
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
|
||||
override val AbstractMethodNameEqualsClassName = ""
|
||||
@@ -182,9 +197,11 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
context("some companion object functions named after the class (factory functions)") {
|
||||
@Nested
|
||||
inner class `some companion object functions named after the class (factory functions)` {
|
||||
|
||||
it("reports a function which has no return type") {
|
||||
@Test
|
||||
fun `reports a function which has no return type`() {
|
||||
val code = """
|
||||
class WrongFactoryClass1 {
|
||||
|
||||
@@ -196,7 +213,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("reports a function which has the wrong return type") {
|
||||
@Test
|
||||
fun `reports a function which has the wrong return type`() {
|
||||
val code = """
|
||||
class WrongFactoryClass2 {
|
||||
|
||||
@@ -210,7 +228,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("reports a body-less function which has the wrong return type") {
|
||||
@Test
|
||||
fun `reports a body-less function which has the wrong return type`() {
|
||||
val code = """
|
||||
class WrongFactoryClass3 {
|
||||
|
||||
@@ -222,7 +241,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("doesn't report a factory function") {
|
||||
@Test
|
||||
fun `doesn't report a factory function`() {
|
||||
val code = """
|
||||
open class A {
|
||||
companion object {
|
||||
@@ -239,7 +259,8 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).isEmpty()
|
||||
}
|
||||
|
||||
it("doesn't report a generic factory function") {
|
||||
@Test
|
||||
fun `doesn't report a generic factory function`() {
|
||||
val code = """
|
||||
data class GenericClass<T>(val wrapped: T) {
|
||||
companion object {
|
||||
@@ -255,7 +276,9 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).isEmpty()
|
||||
}
|
||||
|
||||
context("doesn't report a body-less factory function") {
|
||||
@Nested
|
||||
@DisplayName("doesn't report a body-less factory function")
|
||||
inner class IgnoreBodylessFactoryFunction {
|
||||
val code = """
|
||||
open class A {
|
||||
companion object {
|
||||
@@ -268,14 +291,16 @@ class MemberNameEqualsClassNameSpec : Spek({
|
||||
class C: A()
|
||||
"""
|
||||
|
||||
it("with type solving") {
|
||||
@Test
|
||||
fun `with type solving`() {
|
||||
assertThat(MemberNameEqualsClassName().compileAndLintWithContext(env, code)).isEmpty()
|
||||
}
|
||||
|
||||
it("without type solving") {
|
||||
@Test
|
||||
fun `without type solving`() {
|
||||
assertThat(MemberNameEqualsClassName().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatExceptionOfType
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.regex.PatternSyntaxException
|
||||
|
||||
class NamingConventionCustomPatternSpec : Spek({
|
||||
class NamingConventionCustomPatternSpec {
|
||||
|
||||
val configCustomRules by memoized {
|
||||
val configCustomRules =
|
||||
object : TestConfig() {
|
||||
override fun subConfig(key: String): TestConfig = this
|
||||
|
||||
@@ -27,9 +27,8 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
else -> default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val testConfig by memoized {
|
||||
val testConfig =
|
||||
object : TestConfig() {
|
||||
override fun subConfig(key: String): TestConfig =
|
||||
when (key) {
|
||||
@@ -45,7 +44,6 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
|
||||
override fun <T : Any> valueOrDefault(key: String, default: T): T = default
|
||||
}
|
||||
}
|
||||
|
||||
val excludeClassPatternVariableRegexCode = """
|
||||
class Bar {
|
||||
@@ -65,9 +63,11 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
fun MYFun() {}
|
||||
}"""
|
||||
|
||||
describe("NamingRules rule") {
|
||||
@Nested
|
||||
inner class `NamingRules rule` {
|
||||
|
||||
it("should use custom name for method and class") {
|
||||
@Test
|
||||
fun `should use custom name for method and class`() {
|
||||
val rule = NamingRules(testConfig)
|
||||
assertThat(
|
||||
rule.compileAndLint(
|
||||
@@ -86,7 +86,8 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
).isEmpty()
|
||||
}
|
||||
|
||||
it("should use custom name for constant") {
|
||||
@Test
|
||||
fun `should use custom name for constant`() {
|
||||
val rule = NamingRules(testConfig)
|
||||
assertThat(
|
||||
rule.compileAndLint(
|
||||
@@ -101,7 +102,8 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
).isEmpty()
|
||||
}
|
||||
|
||||
it("should use custom name for enum") {
|
||||
@Test
|
||||
fun `should use custom name for enum`() {
|
||||
val rule = NamingRules(testConfig)
|
||||
assertThat(
|
||||
rule.compileAndLint(
|
||||
@@ -116,12 +118,14 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
).isEmpty()
|
||||
}
|
||||
|
||||
it("should use custom name for package") {
|
||||
@Test
|
||||
fun `should use custom name for package`() {
|
||||
val rule = NamingRules(testConfig)
|
||||
assertThat(rule.compileAndLint("package package_1")).isEmpty()
|
||||
}
|
||||
|
||||
it("shouldExcludeClassesFromVariableNaming") {
|
||||
@Test
|
||||
fun shouldExcludeClassesFromVariableNaming() {
|
||||
val code = """
|
||||
class Bar {
|
||||
val MYVar = 3
|
||||
@@ -134,7 +138,8 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
assertThat(VariableNaming(config).compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("shouldNotFailWithInvalidRegexWhenDisabledVariableNaming") {
|
||||
@Test
|
||||
fun shouldNotFailWithInvalidRegexWhenDisabledVariableNaming() {
|
||||
val configValues = mapOf(
|
||||
"active" to "false",
|
||||
VariableNaming.EXCLUDE_CLASS_PATTERN to "*Foo"
|
||||
@@ -143,14 +148,16 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
assertThat(VariableNaming(config).compileAndLint(excludeClassPatternVariableRegexCode)).isEmpty()
|
||||
}
|
||||
|
||||
it("shouldFailWithInvalidRegexVariableNaming") {
|
||||
@Test
|
||||
fun shouldFailWithInvalidRegexVariableNaming() {
|
||||
val config = TestConfig(mapOf(VariableNaming.EXCLUDE_CLASS_PATTERN to "*Foo"))
|
||||
assertThatExceptionOfType(PatternSyntaxException::class.java).isThrownBy {
|
||||
VariableNaming(config).compileAndLint(excludeClassPatternVariableRegexCode)
|
||||
}
|
||||
}
|
||||
|
||||
it("shouldExcludeClassesFromFunctionNaming") {
|
||||
@Test
|
||||
fun shouldExcludeClassesFromFunctionNaming() {
|
||||
val code = """
|
||||
class Bar {
|
||||
fun MYFun() {}
|
||||
@@ -163,7 +170,8 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
assertThat(FunctionNaming(config).compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("shouldNotFailWithInvalidRegexWhenDisabledFunctionNaming") {
|
||||
@Test
|
||||
fun shouldNotFailWithInvalidRegexWhenDisabledFunctionNaming() {
|
||||
val configRules = mapOf(
|
||||
"active" to "false",
|
||||
FunctionNaming.EXCLUDE_CLASS_PATTERN to "*Foo"
|
||||
@@ -172,11 +180,12 @@ class NamingConventionCustomPatternSpec : Spek({
|
||||
assertThat(FunctionNaming(config).compileAndLint(excludeClassPatternFunctionRegexCode)).isEmpty()
|
||||
}
|
||||
|
||||
it("shouldFailWithInvalidRegexFunctionNaming") {
|
||||
@Test
|
||||
fun shouldFailWithInvalidRegexFunctionNaming() {
|
||||
val config = TestConfig(mapOf(FunctionNaming.EXCLUDE_CLASS_PATTERN to "*Foo"))
|
||||
assertThatExceptionOfType(PatternSyntaxException::class.java).isThrownBy {
|
||||
FunctionNaming(config).compileAndLint(excludeClassPatternFunctionRegexCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,16 +3,17 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
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
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class NamingConventionLengthSpec : Spek({
|
||||
class NamingConventionLengthSpec {
|
||||
|
||||
val subject by memoized { NamingRules() }
|
||||
@Nested
|
||||
inner class `NamingRules rule` {
|
||||
|
||||
describe("NamingRules rule") {
|
||||
|
||||
it("should not report underscore variable names") {
|
||||
@Test
|
||||
fun `should not report underscore variable names`() {
|
||||
val subject = NamingRules()
|
||||
val code = """
|
||||
fun getResult(): Pair<String, String> = TODO()
|
||||
fun function() {
|
||||
@@ -23,24 +24,28 @@ class NamingConventionLengthSpec : Spek({
|
||||
assertThat(subject.findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not report a variable with single letter name") {
|
||||
@Test
|
||||
fun `should not report a variable with single letter name`() {
|
||||
val subject = NamingRules()
|
||||
val code = "private val a = 3"
|
||||
subject.compileAndLint(code)
|
||||
assertThat(subject.findings).isEmpty()
|
||||
}
|
||||
|
||||
context("VariableMinLength rule with a custom minimum length") {
|
||||
@Nested
|
||||
inner class `VariableMinLength rule with a custom minimum length` {
|
||||
|
||||
val variableMinLength by memoized {
|
||||
val variableMinLength =
|
||||
VariableMinLength(TestConfig(mapOf(VariableMinLength.MINIMUM_VARIABLE_NAME_LENGTH to "2")))
|
||||
}
|
||||
|
||||
it("reports a very short variable name") {
|
||||
@Test
|
||||
fun `reports a very short variable name`() {
|
||||
val code = "private val a = 3"
|
||||
assertThat(variableMinLength.compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("does not report a variable with only a single underscore") {
|
||||
@Test
|
||||
fun `does not report a variable with only a single underscore`() {
|
||||
val code = """
|
||||
class C {
|
||||
val prop: (Int) -> Unit = { _ -> Unit }
|
||||
@@ -49,46 +54,60 @@ class NamingConventionLengthSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
it("should not report a variable with 64 letters") {
|
||||
@Test
|
||||
fun `should not report a variable with 64 letters`() {
|
||||
val subject = NamingRules()
|
||||
val code = "private val varThatIsExactly64LettersLongWhichYouMightNotWantToBelieveInLolz = 3"
|
||||
subject.compileAndLint(code)
|
||||
assertThat(subject.findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should report a variable name that is too long") {
|
||||
@Test
|
||||
fun `should report a variable name that is too long`() {
|
||||
val subject = NamingRules()
|
||||
val code = "private val thisVariableIsDefinitelyWayTooLongLongerThanEverythingAndShouldBeMuchShorter = 3"
|
||||
subject.compileAndLint(code)
|
||||
assertThat(subject.findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not report a variable name that is okay") {
|
||||
@Test
|
||||
fun `should not report a variable name that is okay`() {
|
||||
val subject = NamingRules()
|
||||
val code = "private val thisOneIsCool = 3"
|
||||
subject.compileAndLint(code)
|
||||
assertThat(subject.findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should report a function name that is too short") {
|
||||
@Test
|
||||
fun `should report a function name that is too short`() {
|
||||
val subject = NamingRules()
|
||||
val code = "fun a() = 3"
|
||||
subject.compileAndLint(code)
|
||||
assertThat(subject.findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should report a function name that is too long") {
|
||||
@Test
|
||||
fun `should report a function name that is too long`() {
|
||||
val subject = NamingRules()
|
||||
val code = "fun thisFunctionIsDefinitelyWayTooLongAndShouldBeMuchShorter() = 3"
|
||||
subject.compileAndLint(code)
|
||||
assertThat(subject.findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not report a function name that is okay") {
|
||||
@Test
|
||||
fun `should not report a function name that is okay`() {
|
||||
val subject = NamingRules()
|
||||
val code = "fun three() = 3"
|
||||
subject.compileAndLint(code)
|
||||
assertThat(subject.findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should report a function name that begins with a backtick, capitals, and spaces") {
|
||||
@Test
|
||||
fun `should report a function name that begins with a backtick, capitals, and spaces`() {
|
||||
val subject = NamingRules()
|
||||
val code = "fun `Hi bye`() = 3"
|
||||
subject.compileAndLint(code)
|
||||
assertThat(subject.findings).hasSize(1)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,13 +2,15 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class NamingRulesSpec : Spek({
|
||||
class NamingRulesSpec {
|
||||
|
||||
describe("naming like in constants is allowed for destructuring") {
|
||||
it("should not detect any") {
|
||||
@Nested
|
||||
inner class `naming like in constants is allowed for destructuring` {
|
||||
@Test
|
||||
fun `should not detect any`() {
|
||||
val code = """
|
||||
data class D(val i: Int, val j: Int)
|
||||
fun doStuff() {
|
||||
@@ -18,4 +20,4 @@ class NamingRulesSpec : Spek({
|
||||
assertThat(NamingRules().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
|
||||
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class NoNameShadowingSpec : Spek({
|
||||
setupKotlinEnvironment()
|
||||
val env: KotlinCoreEnvironment by memoized()
|
||||
val subject by memoized { NoNameShadowing() }
|
||||
@KotlinCoreEnvironmentTest
|
||||
class NoNameShadowingSpec(val env: KotlinCoreEnvironment) {
|
||||
val subject = NoNameShadowing()
|
||||
|
||||
describe("NoNameShadowing rule") {
|
||||
it("report shadowing variable") {
|
||||
@Nested
|
||||
inner class `NoNameShadowing rule` {
|
||||
@Test
|
||||
fun `report shadowing variable`() {
|
||||
val code = """
|
||||
fun test(i: Int) {
|
||||
val i = 1
|
||||
@@ -25,7 +26,8 @@ class NoNameShadowingSpec : Spek({
|
||||
assertThat(findings[0]).hasMessage("Name shadowed: i")
|
||||
}
|
||||
|
||||
it("report shadowing destructuring declaration entry") {
|
||||
@Test
|
||||
fun `report shadowing destructuring declaration entry`() {
|
||||
val code = """
|
||||
fun test(j: Int) {
|
||||
val (j, _) = 1 to 2
|
||||
@@ -36,7 +38,8 @@ class NoNameShadowingSpec : Spek({
|
||||
assertThat(findings[0]).hasMessage("Name shadowed: j")
|
||||
}
|
||||
|
||||
it("report shadowing lambda parameter") {
|
||||
@Test
|
||||
fun `report shadowing lambda parameter`() {
|
||||
val code = """
|
||||
fun test(k: Int) {
|
||||
listOf(1).map { k ->
|
||||
@@ -48,7 +51,8 @@ class NoNameShadowingSpec : Spek({
|
||||
assertThat(findings[0]).hasMessage("Name shadowed: k")
|
||||
}
|
||||
|
||||
it("report shadowing nested lambda 'it' parameter") {
|
||||
@Test
|
||||
fun `report shadowing nested lambda 'it' parameter`() {
|
||||
val code = """
|
||||
fun test() {
|
||||
listOf(1).forEach {
|
||||
@@ -62,7 +66,8 @@ class NoNameShadowingSpec : Spek({
|
||||
assertThat(findings[0]).hasMessage("Name shadowed: it")
|
||||
}
|
||||
|
||||
it("does not report when implicit 'it' parameter isn't used") {
|
||||
@Test
|
||||
fun `does not report when implicit 'it' parameter isn't used`() {
|
||||
val code = """
|
||||
fun test() {
|
||||
listOf(1).forEach {
|
||||
@@ -75,7 +80,8 @@ class NoNameShadowingSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("does not report not shadowing variable") {
|
||||
@Test
|
||||
fun `does not report not shadowing variable`() {
|
||||
val code = """
|
||||
fun test(i: Int) {
|
||||
val j = i
|
||||
@@ -85,7 +91,8 @@ class NoNameShadowingSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("does not report not shadowing nested lambda implicit 'it' parameter") {
|
||||
@Test
|
||||
fun `does not report not shadowing nested lambda implicit 'it' parameter`() {
|
||||
val code = """
|
||||
fun test() {
|
||||
listOf(1).forEach { i ->
|
||||
@@ -107,4 +114,4 @@ class NoNameShadowingSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,57 +1,63 @@
|
||||
package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.rules.setupKotlinEnvironment
|
||||
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
setupKotlinEnvironment()
|
||||
@KotlinCoreEnvironmentTest
|
||||
class NonBooleanPropertyWithPrefixIsSpec(val env: KotlinCoreEnvironment) {
|
||||
val subject = NonBooleanPropertyPrefixedWithIs()
|
||||
|
||||
val subject by memoized { NonBooleanPropertyPrefixedWithIs() }
|
||||
val env: KotlinCoreEnvironment by memoized()
|
||||
@Nested
|
||||
inner class `IsPropertyNaming rule` {
|
||||
|
||||
describe("IsPropertyNaming rule") {
|
||||
|
||||
context("argument declarations") {
|
||||
it("should not detect Kotlin Boolean") {
|
||||
@Nested
|
||||
inner class `argument declarations` {
|
||||
@Test
|
||||
fun `should not detect Kotlin Boolean`() {
|
||||
val code = """data class O (var isDefault: Boolean)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect Kotlin Boolean nullable") {
|
||||
@Test
|
||||
fun `should not detect Kotlin Boolean nullable`() {
|
||||
val code = """data class O (var isDefault: Boolean?)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect Kotlin Boolean initialized") {
|
||||
@Test
|
||||
fun `should not detect Kotlin Boolean initialized`() {
|
||||
val code = """data class O (var isDefault: Boolean = false)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect Java Boolean") {
|
||||
@Test
|
||||
fun `should not detect Java Boolean`() {
|
||||
val code = """data class O (var isDefault: java.lang.Boolean)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should warn about primitive types") {
|
||||
@Test
|
||||
fun `should warn about primitive types`() {
|
||||
val code = """data class O (var isDefault: Int)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about inner classes") {
|
||||
@Test
|
||||
fun `should warn about inner classes`() {
|
||||
val code = """
|
||||
data class O (var isDefault: Inner) {
|
||||
class Inner
|
||||
@@ -62,14 +68,16 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not detect short names") {
|
||||
@Test
|
||||
fun `should not detect short names`() {
|
||||
val code = """class O (var `is`: Int)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect titles, starting with 'is'") {
|
||||
@Test
|
||||
fun `should not detect titles, starting with 'is'`() {
|
||||
val code = """class O (var isengardTowerHeightInFeet: Int)"""
|
||||
val findings = subject.compileAndLintWithContext(env, code)
|
||||
|
||||
@@ -77,8 +85,10 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
context("property declarations") {
|
||||
it("should not detect Kotlin Boolean") {
|
||||
@Nested
|
||||
inner class `property declarations` {
|
||||
@Test
|
||||
fun `should not detect Kotlin Boolean`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault = false
|
||||
@@ -89,7 +99,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect Kotlin Boolean property uninitialized") {
|
||||
@Test
|
||||
fun `should not detect Kotlin Boolean property uninitialized`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault: Boolean
|
||||
@@ -104,7 +115,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect Kotlin Boolean nullable") {
|
||||
@Test
|
||||
fun `should not detect Kotlin Boolean nullable`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault: Boolean? = null
|
||||
@@ -115,7 +127,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect Java Boolean") {
|
||||
@Test
|
||||
fun `should not detect Java Boolean`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault: java.lang.Boolean = java.lang.Boolean(false)
|
||||
@@ -126,7 +139,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect Java Boolean uninitialized") {
|
||||
@Test
|
||||
fun `should not detect Java Boolean uninitialized`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault: java.lang.Boolean
|
||||
@@ -141,7 +155,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect Java Boolean nullable") {
|
||||
@Test
|
||||
fun `should not detect Java Boolean nullable`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault: java.lang.Boolean? = null
|
||||
@@ -152,7 +167,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should warn about primitive types") {
|
||||
@Test
|
||||
fun `should warn about primitive types in class`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault: Int = 0
|
||||
@@ -163,7 +179,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about inferred primitive types") {
|
||||
@Test
|
||||
fun `should warn about inferred primitive types`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault = 0
|
||||
@@ -174,7 +191,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about inferred non-primitive types") {
|
||||
@Test
|
||||
fun `should warn about inferred non-primitive types`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault = listOf(1, 2, 3)
|
||||
@@ -185,7 +203,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should warn about inner classes") {
|
||||
@Test
|
||||
fun `should warn about inner classes`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isDefault: Inner = Inner()
|
||||
@@ -198,7 +217,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not detect short names") {
|
||||
@Test
|
||||
fun `should not detect short names`() {
|
||||
val code = """
|
||||
class O {
|
||||
var `is`: Int = 0
|
||||
@@ -209,7 +229,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect titles, starting with 'is'") {
|
||||
@Test
|
||||
fun `should not detect titles, starting with 'is'`() {
|
||||
val code = """
|
||||
class O {
|
||||
var isengardTowerHeightInFeet: Int = 500
|
||||
@@ -220,7 +241,8 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
|
||||
it("should warn about primitive types") {
|
||||
@Test
|
||||
fun `should warn about primitive types in function`() {
|
||||
val code = """
|
||||
fun f() {
|
||||
var isDefault: Int = 0
|
||||
@@ -232,4 +254,4 @@ class NonBooleanPropertyWithPrefixIsSpec : Spek({
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,20 +3,22 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
private const val CONSTANT_PATTERN = "constantPattern"
|
||||
private const val PROPERTY_PATTERN = "propertyPattern"
|
||||
private const val PRIVATE_PROPERTY_PATTERN = "privatePropertyPattern"
|
||||
|
||||
class ObjectPropertyNamingSpec : Spek({
|
||||
class ObjectPropertyNamingSpec {
|
||||
|
||||
describe("constants in object declarations") {
|
||||
@Nested
|
||||
inner class `constants in object declarations` {
|
||||
|
||||
val subject by memoized { ObjectPropertyNaming() }
|
||||
val subject = ObjectPropertyNaming()
|
||||
|
||||
it("should not detect public constants complying to the naming rules") {
|
||||
@Test
|
||||
fun `should not detect public constants complying to the naming rules`() {
|
||||
val code = """
|
||||
object O {
|
||||
${PublicConst.negative}
|
||||
@@ -25,7 +27,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect public constants not complying to the naming rules") {
|
||||
@Test
|
||||
fun `should detect public constants not complying to the naming rules`() {
|
||||
val code = """
|
||||
object O {
|
||||
${PublicConst.positive}
|
||||
@@ -34,7 +37,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not detect private constants complying to the naming rules") {
|
||||
@Test
|
||||
fun `should not detect private constants complying to the naming rules`() {
|
||||
val code = """
|
||||
object O {
|
||||
${PrivateConst.negative}
|
||||
@@ -43,7 +47,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect private constants not complying to the naming rules") {
|
||||
@Test
|
||||
fun `should detect private constants not complying to the naming rules`() {
|
||||
val code = """
|
||||
object O {
|
||||
${PrivateConst.positive}
|
||||
@@ -52,7 +57,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("should report constants not complying to the naming rules at the right position") {
|
||||
@Test
|
||||
fun `should report constants not complying to the naming rules at the right position`() {
|
||||
val code = """
|
||||
object O {
|
||||
${PublicConst.positive}
|
||||
@@ -62,11 +68,13 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
describe("constants in companion object") {
|
||||
@Nested
|
||||
inner class `constants in companion object` {
|
||||
|
||||
val subject by memoized { ObjectPropertyNaming() }
|
||||
val subject = ObjectPropertyNaming()
|
||||
|
||||
it("should not detect public constants complying to the naming rules") {
|
||||
@Test
|
||||
fun `should not detect public constants complying to the naming rules`() {
|
||||
val code = """
|
||||
class C {
|
||||
companion object {
|
||||
@@ -77,7 +85,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect public constants not complying to the naming rules") {
|
||||
@Test
|
||||
fun `should detect public constants not complying to the naming rules`() {
|
||||
val code = """
|
||||
class C {
|
||||
companion object {
|
||||
@@ -88,7 +97,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not detect private constants complying to the naming rules") {
|
||||
@Test
|
||||
fun `should not detect private constants complying to the naming rules`() {
|
||||
val code = """
|
||||
class C {
|
||||
companion object {
|
||||
@@ -99,7 +109,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect private constants not complying to the naming rules") {
|
||||
@Test
|
||||
fun `should detect private constants not complying to the naming rules`() {
|
||||
val code = """
|
||||
class C {
|
||||
companion object {
|
||||
@@ -110,7 +121,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("should report constants not complying to the naming rules at the right position") {
|
||||
@Test
|
||||
fun `should report constants not complying to the naming rules at the right position`() {
|
||||
val code = """
|
||||
class C {
|
||||
companion object {
|
||||
@@ -122,11 +134,13 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
describe("variables in objects") {
|
||||
@Nested
|
||||
inner class `variables in objects` {
|
||||
|
||||
val subject by memoized { ObjectPropertyNaming() }
|
||||
val subject = ObjectPropertyNaming()
|
||||
|
||||
it("should not detect public variables complying to the naming rules") {
|
||||
@Test
|
||||
fun `should not detect public variables complying to the naming rules`() {
|
||||
val code = """
|
||||
object O {
|
||||
${PublicVal.negative}
|
||||
@@ -135,7 +149,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect public variables not complying to the naming rules") {
|
||||
@Test
|
||||
fun `should detect public variables not complying to the naming rules`() {
|
||||
val code = """
|
||||
object O {
|
||||
${PublicVal.positive}
|
||||
@@ -144,7 +159,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("should not detect private variables complying to the naming rules") {
|
||||
@Test
|
||||
fun `should not detect private variables complying to the naming rules`() {
|
||||
val code = """
|
||||
object O {
|
||||
${PrivateVal.negative}
|
||||
@@ -153,7 +169,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect private variables not complying to the naming rules") {
|
||||
@Test
|
||||
fun `should detect private variables not complying to the naming rules`() {
|
||||
val code = """
|
||||
object O {
|
||||
private val __NAME = "Artur"
|
||||
@@ -163,19 +180,20 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
describe("variables and constants in objects with custom config") {
|
||||
@Nested
|
||||
inner class `variables and constants in objects with custom config` {
|
||||
|
||||
val config by memoized {
|
||||
val config =
|
||||
TestConfig(
|
||||
mapOf(
|
||||
CONSTANT_PATTERN to "_[A-Za-z]*",
|
||||
PRIVATE_PROPERTY_PATTERN to ".*"
|
||||
)
|
||||
)
|
||||
}
|
||||
val subject by memoized { ObjectPropertyNaming(config) }
|
||||
val subject = ObjectPropertyNaming(config)
|
||||
|
||||
it("should not detect constants in object with underscores") {
|
||||
@Test
|
||||
fun `should not detect constants in object with underscores`() {
|
||||
val code = """
|
||||
object O {
|
||||
const val _NAME = "Artur"
|
||||
@@ -185,7 +203,8 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should not detect private properties in object") {
|
||||
@Test
|
||||
fun `should not detect private properties in object`() {
|
||||
val code = """
|
||||
object O {
|
||||
private val __NAME = "Artur"
|
||||
@@ -196,8 +215,10 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
describe("local properties") {
|
||||
it("should not detect local properties") {
|
||||
@Nested
|
||||
inner class `local properties` {
|
||||
@Test
|
||||
fun `should not detect local properties`() {
|
||||
val config = TestConfig(
|
||||
mapOf(
|
||||
PROPERTY_PATTERN to "valid"
|
||||
@@ -216,7 +237,7 @@ class ObjectPropertyNamingSpec : Spek({
|
||||
assertThat(subject.compileAndLint(code)).isEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@Suppress("UnnecessaryAbstractClass")
|
||||
abstract class NamingSnippet(private val isPrivate: Boolean, private val isConst: Boolean) {
|
||||
|
||||
@@ -2,14 +2,16 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
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
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class PackageNamingSpec : Spek({
|
||||
class PackageNamingSpec {
|
||||
|
||||
describe("PackageNaming rule") {
|
||||
@Nested
|
||||
inner class `PackageNaming rule` {
|
||||
|
||||
it("should ignore the issue by alias suppression") {
|
||||
@Test
|
||||
fun `should ignore the issue by alias suppression`() {
|
||||
assertThat(
|
||||
PackageNaming().compileAndLint(
|
||||
"""
|
||||
@@ -20,20 +22,24 @@ class PackageNamingSpec : Spek({
|
||||
).isEmpty()
|
||||
}
|
||||
|
||||
it("should find a uppercase package name") {
|
||||
@Test
|
||||
fun `should find a uppercase package name`() {
|
||||
assertThat(PackageNaming().compileAndLint("package FOO.BAR")).hasSize(1)
|
||||
}
|
||||
|
||||
it("should find a upper camel case package name") {
|
||||
@Test
|
||||
fun `should find a upper camel case package name`() {
|
||||
assertThat(PackageNaming().compileAndLint("package Foo.Bar")).hasSize(1)
|
||||
}
|
||||
|
||||
it("should find a camel case package name") {
|
||||
@Test
|
||||
fun `should find a camel case package name`() {
|
||||
assertThat(PackageNaming().compileAndLint("package fOO.bAR")).hasSize(1)
|
||||
}
|
||||
|
||||
it("should check an valid package name") {
|
||||
@Test
|
||||
fun `should check an valid package name`() {
|
||||
assertThat(PackageNaming().compileAndLint("package foo.bar")).isEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,16 +2,18 @@ package io.gitlab.arturbosch.detekt.rules.naming
|
||||
|
||||
import io.gitlab.arturbosch.detekt.test.lint
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class TopLevelPropertyNamingSpec : Spek({
|
||||
class TopLevelPropertyNamingSpec {
|
||||
|
||||
val subject by memoized { TopLevelPropertyNaming() }
|
||||
val subject = TopLevelPropertyNaming()
|
||||
|
||||
describe("constants on top level") {
|
||||
@Nested
|
||||
inner class `constants on top level` {
|
||||
|
||||
it("should not detect any constants not complying to the naming rules") {
|
||||
@Test
|
||||
fun `should not detect any constants not complying to the naming rules`() {
|
||||
val code = """
|
||||
const val MY_NAME_8 = "Artur"
|
||||
const val MYNAME = "Artur"
|
||||
@@ -19,7 +21,8 @@ class TopLevelPropertyNamingSpec : Spek({
|
||||
assertThat(subject.lint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should detect five constants not complying to the naming rules") {
|
||||
@Test
|
||||
fun `should detect five constants not complying to the naming rules`() {
|
||||
val code = """
|
||||
const val MyNAME = "Artur"
|
||||
const val name = "Artur"
|
||||
@@ -31,9 +34,11 @@ class TopLevelPropertyNamingSpec : Spek({
|
||||
}
|
||||
}
|
||||
|
||||
describe("variables on top level") {
|
||||
@Nested
|
||||
inner class `variables on top level` {
|
||||
|
||||
it("should not report any") {
|
||||
@Test
|
||||
fun `should not report any`() {
|
||||
val code = """
|
||||
val name = "Artur"
|
||||
val nAme8 = "Artur"
|
||||
@@ -49,18 +54,20 @@ class TopLevelPropertyNamingSpec : Spek({
|
||||
assertThat(subject.lint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should report non private top level property using underscore") {
|
||||
@Test
|
||||
fun `should report non private top level property using underscore`() {
|
||||
val code = """
|
||||
val _nAme = "Artur"
|
||||
"""
|
||||
assertThat(subject.lint(code)).hasSize(1)
|
||||
}
|
||||
|
||||
it("should report private top level property using two underscores") {
|
||||
@Test
|
||||
fun `should report private top level property using two underscores`() {
|
||||
val code = """
|
||||
private val __NAME = "Artur"
|
||||
"""
|
||||
io.gitlab.arturbosch.detekt.test.assertThat(subject.lint(code)).hasSize(1)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,14 +4,16 @@ import io.gitlab.arturbosch.detekt.api.SourceLocation
|
||||
import io.gitlab.arturbosch.detekt.test.TestConfig
|
||||
import io.gitlab.arturbosch.detekt.test.assertThat
|
||||
import io.gitlab.arturbosch.detekt.test.compileAndLint
|
||||
import org.spekframework.spek2.Spek
|
||||
import org.spekframework.spek2.style.specification.describe
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class VariableNamingSpec : Spek({
|
||||
class VariableNamingSpec {
|
||||
|
||||
describe("properties in classes") {
|
||||
@Nested
|
||||
inner class `properties in classes` {
|
||||
|
||||
it("should detect all positive cases") {
|
||||
@Test
|
||||
fun `should detect all positive cases`() {
|
||||
val code = """
|
||||
class C {
|
||||
private val _FIELD = 5
|
||||
@@ -27,7 +29,8 @@ class VariableNamingSpec : Spek({
|
||||
)
|
||||
}
|
||||
|
||||
it("checks all negative cases") {
|
||||
@Test
|
||||
fun `checks all negative cases`() {
|
||||
val code = """
|
||||
class C {
|
||||
private val _field = 5
|
||||
@@ -38,7 +41,8 @@ class VariableNamingSpec : Spek({
|
||||
assertThat(VariableNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("should not flag overridden member properties by default") {
|
||||
@Test
|
||||
fun `should not flag overridden member properties by default`() {
|
||||
val code = """
|
||||
class C : I {
|
||||
override val SHOULD_NOT_BE_FLAGGED = "banana"
|
||||
@@ -53,7 +57,8 @@ class VariableNamingSpec : Spek({
|
||||
assertThat(VariableNaming().compileAndLint(code)).isEmpty()
|
||||
}
|
||||
|
||||
it("doesn't ignore overridden member properties if ignoreOverridden is false") {
|
||||
@Test
|
||||
fun `doesn't ignore overridden member properties if ignoreOverridden is false`() {
|
||||
val code = """
|
||||
class C : I {
|
||||
override val SHOULD_BE_FLAGGED = "banana"
|
||||
@@ -73,6 +78,6 @@ class VariableNamingSpec : Spek({
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private const val IGNORE_OVERRIDDEN = "ignoreOverridden"
|
||||
|
||||
Reference in New Issue
Block a user