Skip to content

Commit

Permalink
tests for #799
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Apr 23, 2023
1 parent a905e5e commit d0375ce
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,38 @@ final class AnnotationProcessorSpec extends AbstractAndroidSpec {
where:
[gradleVersion, agpVersion] << gradleAgpMatrix()
}
def "kotlin parcelize is redundant when Parcelize annotation is unused (#gradleVersion AGP #agpVersion)"() {
given:
def project = new KotlinParcelizeIsRedundantProject(agpVersion)
androidProject = project.newProject()
when:
build(gradleVersion, androidProject, 'buildHealth')
then:
def actualAdvice = androidProject.adviceFor(project.appSpec)
def expectedAdvice = project.expectedAdvice
assertThat(actualAdvice).containsExactlyElementsIn(expectedAdvice)
where:
[gradleVersion, agpVersion] << gradleAgpMatrix()
}
def "kotlin parcelize is not redundant when Parcelize annotation is used (#gradleVersion AGP #agpVersion)"() {
given:
def project = new KotlinParcelizeIsNotRedundantProject(agpVersion)
androidProject = project.newProject()
when:
build(gradleVersion, androidProject, 'buildHealth')
then:
def actualAdvice = androidProject.adviceFor(project.appSpec)
def expectedAdvice = project.expectedAdvice
assertThat(actualAdvice).containsExactlyElementsIn(expectedAdvice)
where:
[gradleVersion, agpVersion] << gradleAgpMatrix()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,89 @@ class KaptIsRedundantWithUnusedProcsProject(agpVersion: String) {
val expectedAdvice = setOf(PluginAdvice.redundantKapt())
}

class KotlinParcelizeIsNotRedundantProject(agpVersion: String) {

fun newProject() = AndroidProject(
rootSpec = rootSpec,
appSpec = appSpec
)

val rootSpec = RootSpec(agpVersion = agpVersion)

private val sources = mapOf(
"Thing.kt" to """
package $DEFAULT_PACKAGE_NAME
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
data class Thing(val id: String) : Parcelable
""".trimIndent()
)

val appSpec = AppSpec(
plugins = setOf("kotlin-parcelize"),
sources = sources,
dependencies = listOf(
"implementation" to "org.jetbrains.kotlin:kotlin-stdlib:${Plugin.KOTLIN_VERSION}",
"implementation" to APPCOMPAT
)
)

val expectedAdvice = emptySet<PluginAdvice>()
}

class KotlinParcelizeIsRedundantProject(agpVersion: String) {

fun newProject() = AndroidProject(
rootSpec = rootSpec,
appSpec = appSpec
)

val rootSpec = RootSpec(agpVersion = agpVersion)

private val sources = mapOf(
"Thing.kt" to """
package $DEFAULT_PACKAGE_NAME
import android.os.Parcel
import android.os.Parcelable
data class Thing(val id: String) : Parcelable {
constructor(parcel: Parcel) : this(parcel.readString()!!)
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(id)
}
companion object CREATOR : Parcelable.Creator<Thing> {
override fun createFromParcel(parcel: Parcel): Thing {
return Thing(parcel)
}
override fun newArray(size: Int): Array<Thing?> {
return arrayOfNulls(size)
}
}
}
""".trimIndent()
)

val appSpec = AppSpec(
plugins = setOf("kotlin-parcelize"),
sources = sources,
dependencies = listOf(
"implementation" to "org.jetbrains.kotlin:kotlin-stdlib:${Plugin.KOTLIN_VERSION}",
"implementation" to APPCOMPAT
)
)

val expectedAdvice = setOf(PluginAdvice.redundantKotlinParcelize())
}

private val transitiveDagger = ModuleCoordinates("com.google.dagger:dagger", "2.24", GradleVariantIdentification(emptySet(), emptyMap()))
private val transitiveInject = ModuleCoordinates("javax.inject:javax.inject", "1", GradleVariantIdentification(emptySet(), emptyMap()))
private val transitiveInject2 = ModuleCoordinates("javax.inject:javax.inject", "1", GradleVariantIdentification(emptySet(), emptyMap()))
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/autonomousapps/advice/PluginAdvice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data class PluginAdvice(
const val JAVA_LIBRARY = "java-library"
const val KOTLIN_JVM = "org.jetbrains.kotlin.jvm"
const val KOTLIN_KAPT = "kotlin-kapt"
const val KOTLIN_PARCELIZE = "kotlin-parcelize"

@JvmStatic
fun redundantJavaLibrary() = PluginAdvice(
Expand All @@ -34,6 +35,13 @@ data class PluginAdvice(
reason = "this project has the kotlin-kapt (org.jetbrains.kotlin.kapt) plugin applied, but " +
"there are no used annotation processors."
)

@JvmStatic
fun redundantKotlinParcelize() = PluginAdvice(
redundantPlugin = KOTLIN_PARCELIZE,
reason = "this project has the kotlin-parcelize (org.jetbrains.kotlin.plugin.parcelize) plugin applied, but " +
"there are no classes annotated with @Parcelize."
)
}

override fun compareTo(other: PluginAdvice): Int {
Expand Down

0 comments on commit d0375ce

Please sign in to comment.