|
| 1 | +package com.squareup.anvil.plugin |
| 2 | + |
| 3 | +import com.rickbusarow.kase.Kase4 |
| 4 | +import com.rickbusarow.kase.gradle.GradleDependencyVersion |
| 5 | +import com.rickbusarow.kase.gradle.GradleKotlinTestVersions |
| 6 | +import com.rickbusarow.kase.gradle.GradleProjectBuilder |
| 7 | +import com.rickbusarow.kase.gradle.HasGradleDependencyVersion |
| 8 | +import com.rickbusarow.kase.gradle.HasKotlinDependencyVersion |
| 9 | +import com.rickbusarow.kase.gradle.KotlinDependencyVersion |
| 10 | +import com.rickbusarow.kase.kase |
| 11 | +import com.rickbusarow.kase.times |
| 12 | +import io.kotest.matchers.string.shouldContain |
| 13 | +import org.junit.jupiter.api.DynamicNode |
| 14 | +import org.junit.jupiter.api.Nested |
| 15 | +import org.junit.jupiter.api.TestFactory |
| 16 | +import java.util.stream.Stream |
| 17 | + |
| 18 | +class AnvilExtensionTest : BaseGradleTest() { |
| 19 | + |
| 20 | + @Nested |
| 21 | + inner class `gradle property to extension property mapping` { |
| 22 | + |
| 23 | + val properties = listOf( |
| 24 | + kase(a1 = "generateDaggerFactories", false), |
| 25 | + kase(a1 = "generateDaggerFactoriesOnly", false), |
| 26 | + kase(a1 = "disableComponentMerging", false), |
| 27 | + kase(a1 = "syncGeneratedSources", false), |
| 28 | + kase(a1 = "addOptionalAnnotations", false), |
| 29 | + ) |
| 30 | + |
| 31 | + fun GradleProjectBuilder.gradlePropertiesFile(propertyName: String, value: Any) { |
| 32 | + val props = buildList { |
| 33 | + if (propertyName == "generateDaggerFactoriesOnly") { |
| 34 | + // The "__FactoriesOnly" toggle requires the "__Factories" toggle to be enabled. |
| 35 | + add("com.squareup.anvil.generateDaggerFactories=true") |
| 36 | + } |
| 37 | + add("com.squareup.anvil.$propertyName=$value") |
| 38 | + } |
| 39 | + gradlePropertiesFile(props.joinToString("\n")) |
| 40 | + } |
| 41 | + |
| 42 | + @TestFactory |
| 43 | + fun `extension properties are their default value when not set anywhere`() = |
| 44 | + tests { versions, propertyName, default -> |
| 45 | + |
| 46 | + rootProject.buildFile( |
| 47 | + """ |
| 48 | + plugins { |
| 49 | + kotlin("jvm") version "${versions.kotlinVersion}" |
| 50 | + id("com.squareup.anvil") version "$anvilVersion" |
| 51 | + } |
| 52 | + |
| 53 | + tasks.register("printProperty") { |
| 54 | + doLast { |
| 55 | + println("$propertyName: ${'$'}{anvil.$propertyName.get()}") |
| 56 | + } |
| 57 | + } |
| 58 | + """.trimIndent(), |
| 59 | + ) |
| 60 | + |
| 61 | + shouldSucceed("printProperty") { |
| 62 | + output shouldContain "$propertyName: $default" |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @TestFactory |
| 67 | + fun `the extension property is not default when only set in gradle properties`() = |
| 68 | + tests { versions, propertyName, default -> |
| 69 | + rootProject { |
| 70 | + buildFile( |
| 71 | + """ |
| 72 | + plugins { |
| 73 | + kotlin("jvm") version "${versions.kotlinVersion}" |
| 74 | + id("com.squareup.anvil") version "$anvilVersion" |
| 75 | + } |
| 76 | + |
| 77 | + tasks.register("printProperty") { |
| 78 | + doLast { |
| 79 | + println("$propertyName: ${'$'}{anvil.$propertyName.get()}") |
| 80 | + } |
| 81 | + } |
| 82 | + """.trimIndent(), |
| 83 | + ) |
| 84 | + |
| 85 | + gradlePropertiesFile(propertyName, !default) |
| 86 | + } |
| 87 | + |
| 88 | + shouldSucceed("printProperty") { |
| 89 | + output shouldContain "$propertyName: ${!default}" |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @TestFactory |
| 94 | + fun `the extension property is default when explicitly set to it in gradle properties`() = |
| 95 | + tests { versions, propertyName, default -> |
| 96 | + rootProject { |
| 97 | + buildFile( |
| 98 | + """ |
| 99 | + plugins { |
| 100 | + kotlin("jvm") version "${versions.kotlinVersion}" |
| 101 | + id("com.squareup.anvil") version "$anvilVersion" |
| 102 | + } |
| 103 | + |
| 104 | + tasks.register("printProperty") { |
| 105 | + doLast { |
| 106 | + println("$propertyName: ${'$'}{anvil.$propertyName.get()}") |
| 107 | + } |
| 108 | + } |
| 109 | + """.trimIndent(), |
| 110 | + ) |
| 111 | + |
| 112 | + gradlePropertiesFile(propertyName, default) |
| 113 | + } |
| 114 | + |
| 115 | + shouldSucceed("printProperty") { |
| 116 | + output shouldContain "$propertyName: $default" |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @TestFactory |
| 121 | + fun `the extension property overrides the gradle properties value when set`() = |
| 122 | + tests { versions, propertyName, default -> |
| 123 | + |
| 124 | + rootProject { |
| 125 | + val extensionText = if (propertyName == "generateDaggerFactoriesOnly") { |
| 126 | + """ |
| 127 | + anvil { |
| 128 | + generateDaggerFactories = true |
| 129 | + $propertyName = ${!default} |
| 130 | + } |
| 131 | + """.trimIndent() |
| 132 | + } else { |
| 133 | + """ |
| 134 | + anvil { |
| 135 | + $propertyName = ${!default} |
| 136 | + } |
| 137 | + """.trimIndent() |
| 138 | + } |
| 139 | + buildFile( |
| 140 | + """ |
| 141 | + plugins { |
| 142 | + kotlin("jvm") version "${versions.kotlinVersion}" |
| 143 | + id("com.squareup.anvil") version "$anvilVersion" |
| 144 | + } |
| 145 | + |
| 146 | + tasks.register("printProperty") { |
| 147 | + doLast { |
| 148 | + println("$propertyName: ${'$'}{anvil.$propertyName.get()}") |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + $extensionText |
| 153 | + """.trimIndent(), |
| 154 | + ) |
| 155 | + |
| 156 | + gradlePropertiesFile(propertyName, default) |
| 157 | + } |
| 158 | + |
| 159 | + shouldSucceed("printProperty") { |
| 160 | + output shouldContain "$propertyName: ${!default}" |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + inline fun tests( |
| 165 | + crossinline action: AnvilGradleTestEnvironment.( |
| 166 | + GradleKotlinTestVersions, |
| 167 | + String, |
| 168 | + Boolean, |
| 169 | + ) -> Unit, |
| 170 | + ): Stream<out DynamicNode> = params.asContainers { versions -> |
| 171 | + properties.times(listOf(versions), instanceFactory = ::PropertyKase) |
| 172 | + .asTests { kase -> action(versions, kase.propertyName, kase.default) } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +@PublishedApi |
| 178 | +internal class PropertyKase( |
| 179 | + val propertyName: String, |
| 180 | + val default: Boolean, |
| 181 | + override val gradle: GradleDependencyVersion, |
| 182 | + override val kotlin: KotlinDependencyVersion, |
| 183 | +) : Kase4<GradleDependencyVersion, KotlinDependencyVersion, String, Boolean> by kase( |
| 184 | + displayName = "property: $propertyName | default: $default", |
| 185 | + a1 = gradle, |
| 186 | + a2 = kotlin, |
| 187 | + a3 = propertyName, |
| 188 | + a4 = default, |
| 189 | +), |
| 190 | + GradleKotlinTestVersions, |
| 191 | + HasGradleDependencyVersion by HasGradleDependencyVersion(gradle), |
| 192 | + HasKotlinDependencyVersion by HasKotlinDependencyVersion(kotlin) |
0 commit comments