Skip to content

Commit

Permalink
Workaround ordering issue with supertypes in Kotlin scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex committed Mar 21, 2024
1 parent 005046d commit 3f64cb1
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 58 deletions.
Expand Up @@ -19,19 +19,18 @@ class WrappingSpec {
@Test
fun `Given a wrong wrapping in the class definition`() {
val code = """
class A() : B,
C {
}
interface B
interface C
class A() : B,
C {
}
""".trimIndent()

assertThat(subject.compileAndLint(code))
.hasSize(1)
.hasStartSourceLocation(1, 12)
.hasTextLocations(11 to 12)
.hasStartSourceLocation(5, 12)
.hasTextLocations(37 to 38)
}
}
Expand Up @@ -35,16 +35,16 @@ class DeprecationSpec(private val env: KotlinCoreEnvironment) {
@Test
fun `does not report when supertype is not deprecated`() {
val code = """
abstract class Oof : Foo() {
fun spam() {
}
}
abstract class Foo {
abstract fun bar() : Int
fun baz() {
}
}
abstract class Oof : Foo() {
fun spam() {
}
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
Expand Down
Expand Up @@ -38,9 +38,9 @@ class ObjectExtendsThrowableSpec(val env: KotlinCoreEnvironment) {
@Test
fun `reports object that extends custom exception`() {
val code = """
object ObjectCustomException : CustomException("singleton custom exception")
open class CustomException(message: String) : RuntimeException(message)
object ObjectCustomException : CustomException("singleton custom exception")
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
Expand Down Expand Up @@ -79,15 +79,14 @@ class ObjectExtendsThrowableSpec(val env: KotlinCoreEnvironment) {
fun `does not report objects that do not extend Throwable`() {
val code = """
object BanException
open class CustomException(message: String)
object AuthException : CustomException(message = "Authentication failed!")
sealed class DomainException {
object Exception1 : DomainException()
object Exception2 : DomainException()
object Exception3 : DomainException()
}
open class CustomException(message: String)
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
Expand Down Expand Up @@ -117,15 +116,14 @@ class ObjectExtendsThrowableSpec(val env: KotlinCoreEnvironment) {
data class AuthException(val code: Int) : RuntimeException()
class ReportedException : Exception()
class FatalException : Error()
open class CustomException(message: String)
class ObjectCustomException : CustomException("singleton custom exception")
sealed class DomainException : RuntimeException() {
data class Exception1(val prop1: String, val prop2: Boolean) : DomainException()
class Exception2 : DomainException()
class Exception3 : DomainException()
}
open class CustomException(message: String)
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
Expand Down
Expand Up @@ -45,9 +45,9 @@ class ConstructorParameterNamingSpec {
@Test
fun `should not complain about override`() {
val code = """
class C(override val PARAM: String) : I
interface I { val PARAM: String }
class C(override val PARAM: String) : I
""".trimIndent()
assertThat(ConstructorParameterNaming(Config.empty).compileAndLint(code)).isEmpty()
}
Expand Down
Expand Up @@ -32,10 +32,10 @@ class FunctionMinNameLengthSpec {
@Test
fun `should not report an overridden function name that is too short`() {
val code = """
interface I { @Suppress("FunctionNameMinLength") fun tooShortButShouldNotBeReportedByDefault() }
class C : I {
override fun tooShortButShouldNotBeReportedByDefault() {}
}
interface I { @Suppress("FunctionNameMinLength") fun tooShortButShouldNotBeReportedByDefault() }
""".trimIndent()
assertThat(
FunctionNameMinLength(
Expand Down
Expand Up @@ -19,10 +19,10 @@ class FunctionNameMaxLengthSpec {
@Test
fun `should not report an overridden function name that is too long`() {
val code = """
interface I { @Suppress("FunctionNameMaxLength") fun thisFunctionIsWayTooLongButStillShouldNotBeReportedByDefault() }
class C : I {
override fun thisFunctionIsWayTooLongButStillShouldNotBeReportedByDefault() {}
}
interface I { @Suppress("FunctionNameMaxLength") fun thisFunctionIsWayTooLongButStillShouldNotBeReportedByDefault() }
""".trimIndent()
assertThat(
FunctionNameMaxLength(TestConfig("maximumFunctionNameLength" to 10)).compileAndLint(code)
Expand Down
Expand Up @@ -45,23 +45,23 @@ class FunctionNamingSpec {
@Test
fun `flags functions inside functions`() {
val code = """
interface I { fun shouldNotBeFlagged() }
class C : I {
override fun shouldNotBeFlagged() {
fun SHOULD_BE_FLAGGED() { }
}
}
interface I { fun shouldNotBeFlagged() }
""".trimIndent()
assertThat(FunctionNaming(Config.empty).compileAndLint(code)).hasStartSourceLocation(3, 13)
assertThat(FunctionNaming(Config.empty).compileAndLint(code)).hasStartSourceLocation(4, 13)
}

@Test
fun `ignores overridden functions`() {
val code = """
interface I { @Suppress("FunctionNaming") fun SHOULD_NOT_BE_FLAGGED() }
class C : I {
override fun SHOULD_NOT_BE_FLAGGED() {}
}
interface I { @Suppress("FunctionNaming") fun SHOULD_NOT_BE_FLAGGED() }
""".trimIndent()
assertThat(FunctionNaming(Config.empty).compileAndLint(code)).isEmpty()
}
Expand Down Expand Up @@ -89,14 +89,14 @@ class FunctionNamingSpec {
@Test
fun `flags functions with bad names inside overridden functions by default`() {
val code = """
interface I { @Suppress("FunctionNaming") fun SHOULD_BE_FLAGGED() }
class C : I {
override fun SHOULD_BE_FLAGGED() {
fun SHOULD_BE_FLAGGED() {}
}
}
interface I { @Suppress("FunctionNaming") fun SHOULD_BE_FLAGGED() }
""".trimIndent()
assertThat(FunctionNaming(Config.empty).compileAndLint(code)).hasStartSourceLocation(3, 13)
assertThat(FunctionNaming(Config.empty).compileAndLint(code)).hasStartSourceLocation(4, 13)
}

@Test
Expand Down
Expand Up @@ -27,10 +27,10 @@ class FunctionParameterNamingSpec {
@Test
fun `should not detect violations in overridden function`() {
val code = """
interface I { fun someStuff(@Suppress("FunctionParameterNaming") `object`: String) }
class C : I {
override fun someStuff(`object`: String) {}
}
interface I { fun someStuff(@Suppress("FunctionParameterNaming") `object`: String) }
""".trimIndent()
assertThat(FunctionParameterNaming(Config.empty).compileAndLint(code)).isEmpty()
}
Expand Down
Expand Up @@ -127,12 +127,12 @@ class MemberNameEqualsClassNameSpec(val env: KotlinCoreEnvironment) {
@Test
fun `doesn't report overridden methods which are named after the class`() {
val code = """
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
override fun AbstractMethodNameEqualsClassName() {}
}
abstract class BaseClassForMethodNameEqualsClassName {
abstract fun AbstractMethodNameEqualsClassName()
}
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
override fun AbstractMethodNameEqualsClassName() {}
}
""".trimIndent()
assertThat(MemberNameEqualsClassName(Config.empty).compileAndLint(code)).isEmpty()
}
Expand All @@ -150,38 +150,38 @@ class MemberNameEqualsClassNameSpec(val env: KotlinCoreEnvironment) {
@Test
fun `reports overridden methods which are named after the class if they are not ignored`() {
val code = """
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
override fun AbstractMethodNameEqualsClassName() {}
}
abstract class BaseClassForMethodNameEqualsClassName {
abstract fun AbstractMethodNameEqualsClassName()
}
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
override fun AbstractMethodNameEqualsClassName() {}
}
""".trimIndent()
assertThat(MemberNameEqualsClassName(noIgnoreOverridden).compileAndLint(code)).hasSize(1)
}

@Test
fun `doesn't report overridden properties which are named after the class`() {
val code = """
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
override val AbstractMethodNameEqualsClassName = ""
}
abstract class BaseClassForMethodNameEqualsClassName {
abstract val AbstractMethodNameEqualsClassName: String
}
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
override val AbstractMethodNameEqualsClassName = ""
}
""".trimIndent()
assertThat(MemberNameEqualsClassName(Config.empty).compileAndLint(code)).isEmpty()
}

@Test
fun `reports overridden properties which are named after the class if they are not ignored`() {
val code = """
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
override val AbstractMethodNameEqualsClassName = ""
}
abstract class BaseClassForMethodNameEqualsClassName {
abstract val AbstractMethodNameEqualsClassName: String
}
class AbstractMethodNameEqualsClassName : BaseClassForMethodNameEqualsClassName() {
override val AbstractMethodNameEqualsClassName = ""
}
""".trimIndent()
assertThat(MemberNameEqualsClassName(noIgnoreOverridden).compileAndLint(code)).hasSize(1)
}
Expand Down
Expand Up @@ -28,14 +28,14 @@ class VariableMaxLengthSpec {
@Test
fun `should not report an overridden variable name that is too long`() {
val code = """
class C : I {
override val tooLongButShouldNotBeReported = "banana"
interface I2 {
@Suppress("VariableMaxLength") val tooLongButShouldNotBeReported: String
}
interface I : I2 {
override val tooLongButShouldNotBeReported: String
}
interface I2 {
@Suppress("VariableMaxLength") val tooLongButShouldNotBeReported: String
class C : I {
override val tooLongButShouldNotBeReported = "banana"
}
""".trimIndent()
assertThat(
Expand Down
Expand Up @@ -58,14 +58,14 @@ class VariableMinLengthSpec {
@Test
fun `should not report an overridden variable name that is too short`() {
val code = """
class C : I {
override val shortButOk = "banana"
interface I2 {
@Suppress("VariableMinLength") val shortButOk: String
}
interface I : I2 {
override val shortButOk: String
}
interface I2 {
@Suppress("VariableMinLength") val shortButOk: String
class C : I {
override val shortButOk = "banana"
}
""".trimIndent()
assertThat(
Expand Down
Expand Up @@ -81,14 +81,14 @@ class VariableNamingSpec {
@Test
fun `should not flag overridden member properties`() {
val code = """
class C : I {
override val SHOULD_NOT_BE_FLAGGED = "banana"
interface I2 {
@Suppress("VariableNaming") val SHOULD_NOT_BE_FLAGGED: String
}
interface I : I2 {
override val SHOULD_NOT_BE_FLAGGED: String
}
interface I2 {
@Suppress("VariableNaming") val SHOULD_NOT_BE_FLAGGED: String
class C : I {
override val SHOULD_NOT_BE_FLAGGED = "banana"
}
""".trimIndent()
assertThat(VariableNaming(Config.empty).compileAndLint(code)).isEmpty()
Expand Down
Expand Up @@ -277,13 +277,12 @@ class AbstractClassCanBeConcreteClassSpec(val env: KotlinCoreEnvironment) {
@Test
fun `does not report an abstract class with a function derived from an interface`() {
val code = """
abstract class A : Interface {
fun g() {}
}
interface Interface {
fun f()
}
abstract class A : Interface {
fun g() {}
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
Expand Down
Expand Up @@ -281,13 +281,12 @@ class AbstractClassCanBeInterfaceSpec(val env: KotlinCoreEnvironment) {
@Test
fun `does not report an abstract class with a function derived from an interface`() {
val code = """
abstract class A : Interface {
fun g() {}
}
interface Interface {
fun f()
}
abstract class A : Interface {
fun g() {}
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
Expand Down

0 comments on commit 3f64cb1

Please sign in to comment.