Skip to content

Commit

Permalink
Merge pull request #20213 from wordpress-mobile/processor_unit_tests
Browse files Browse the repository at this point in the history
Add `:libs:processors` unit tests
  • Loading branch information
Antonis Lilis authored Feb 20, 2024
2 parents db5bbc7 + ec1baba commit 40ece18
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ext {
kotlinxCoroutinesVersion = '1.7.3'
lottieVersion = '6.1.0'
philjayMpAndroidChartVersion = 'v3.1.0'
squareupKotlinPoetVersion = '1.6.0'
squareupKotlinPoetVersion = '1.16.0'
squareupOkioVersion = '3.6.0'
squareupRetrofitVersion = '2.9.0'
uCropVersion = '2.2.8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FeaturesInDevelopmentDefaultsBuilder(private val featuresInDevelopment: Li
.build()
return FileSpec.builder("org.wordpress.android.util.config", "FeaturesInDevelopment")
.addType(remoteConfigDefaults)
.addComment("Automatically generated file. DO NOT MODIFY")
.addFileComment("Automatically generated file. DO NOT MODIFY")
.indent(" ")
.build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.processor

import com.google.auto.service.AutoService
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.asTypeName
import org.wordpress.android.annotation.Experiment
Expand All @@ -26,6 +27,7 @@ import javax.tools.Diagnostic.Kind
"org.wordpress.android.annotation.RemoteFieldDefaultGenerater"
)
class RemoteConfigProcessor : AbstractProcessor() {
@OptIn(DelicateKotlinPoetApi::class)
@Suppress("DEPRECATION")
override fun process(p0: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean {
val experiments = roundEnvironment?.getElementsAnnotatedWith(Experiment::class.java)?.map { element ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class RemoteFeatureConfigCheckBuilder(private val remoteFeatures: List<TypeName>
.build()
return FileSpec.builder("org.wordpress.android.util.config", "RemoteFeatureConfigCheck")
.addType(remoteFeatureConfigDefaults)
.addComment("Automatically generated file. DO NOT MODIFY")
.addFileComment("Automatically generated file. DO NOT MODIFY")
.indent(" ")
.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RemoteFeatureConfigDefaultsBuilder(private val defaults: Map<String, Strin
.build()
return FileSpec.builder("org.wordpress.android.util.config", FILE_NAME)
.addType(remoteConfigDefaults)
.addComment("Automatically generated file. DO NOT MODIFY")
.addFileComment("Automatically generated file. DO NOT MODIFY")
.indent(" ")
.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RemoteFieldConfigDefaultsBuilder(private val defaults: Map<String, String>
.build()
return FileSpec.builder("org.wordpress.android.util.config", FILE_NAME)
.addType(remoteConfigDefaults)
.addComment("Automatically generated file. DO NOT MODIFY")
.addFileComment("Automatically generated file. DO NOT MODIFY")
.indent(" ")
.build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.wordpress.android.processor

import org.assertj.core.api.Assertions
import org.junit.Test

class FeaturesInDevelopmentDefaultsBuilderTest {
@Test
fun `given a list of features in development, when building the object, then generate the correct list`() {
// given
val featureA = "valueA"
val featureB = "valueB"
val features = listOf(featureA, featureB)

// when
val sut = FeaturesInDevelopmentDefaultsBuilder(features)

// then
Assertions.assertThat(sut.getContent().toString()).isEqualTo(
"""
// Automatically generated file. DO NOT MODIFY
package org.wordpress.android.util.config
import kotlin.String
import kotlin.collections.List
public object FeaturesInDevelopment {
public val featuresInDevelopment: List<String> = listOf(
"$featureA",
"$featureB"
)
}
""".trimIndent()
)
}

@Test
fun `given an empty list of features in development, when building the object, then generate empty list`() {
// given
val features = emptyList<String>()

// when
val sut = FeaturesInDevelopmentDefaultsBuilder(features)

// then
Assertions.assertThat(sut.getContent().toString()).isEqualTo(
"""
// Automatically generated file. DO NOT MODIFY
package org.wordpress.android.util.config
import kotlin.String
import kotlin.collections.List
public object FeaturesInDevelopment {
public val featuresInDevelopment: List<String> = listOf(
)
}
""".trimIndent()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.wordpress.android.processor

import com.squareup.kotlinpoet.ClassName
import org.assertj.core.api.Assertions
import org.junit.Test

class RemoteFeatureConfigCheckBuilderTest {
@Test
fun `given feature classes, when building config check, then generate the correct checks`() {
// given
val classA = "customClassA"
val classB = "customClassB"
val features = listOf(
ClassName("org.wordpress", listOf(classA)),
ClassName("org.wordpress", listOf(classB))
)

// when
val sut = RemoteFeatureConfigCheckBuilder(features)

// then
Assertions.assertThat(sut.getContent().toString()).isEqualTo(
"""
// Automatically generated file. DO NOT MODIFY
package org.wordpress.android.util.config
import org.wordpress.$classA
import org.wordpress.$classB
public class RemoteFeatureConfigCheck(
public val appConfig: AppConfig,
) {
public val $classA: $classA = $classA(appConfig)
public val $classB: $classB = $classB(appConfig)
public fun checkRemoteFields() {
if ($classA.remoteField == null) {
throw IllegalArgumentException(""${'"'}org.wordpress.$classA needs to define
remoteField""${'"'})
}
if ($classB.remoteField == null) {
throw IllegalArgumentException(""${'"'}org.wordpress.$classB needs to define
remoteField""${'"'})
}
}
}
""".trimIndent()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class RemoteFeatureConfigDefaultsBuilderTest {
import kotlin.String
import kotlin.collections.Map
object RemoteFeatureConfigDefaults {
val remoteFeatureConfigDefaults: Map<String, Any> = mapOf(
public object RemoteFeatureConfigDefaults {
public val remoteFeatureConfigDefaults: Map<String, Any> = mapOf(
"$keyA" to "$valueA",
"$keyB" to "$valueB"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.wordpress.android.processor

import org.junit.Assert.assertEquals
import org.junit.Test

class RemoteFieldConfigDefaultsBuilderTest {
@Test
fun `given a list of remote fields, when building the object, then generate list of remote fields`() {
// given
val keyA = "keyA"
val valueA = "valueA"
val keyB = "keyB"
val valueB = "valueB"

// when
val sut = RemoteFieldConfigDefaultsBuilder(mapOf(keyA to valueA, keyB to valueB))

// then
assertEquals(
"""
// Automatically generated file. DO NOT MODIFY
package org.wordpress.android.util.config
import kotlin.Any
import kotlin.String
import kotlin.collections.Map
public object RemoteFieldConfigDefaults {
public val remoteFieldConfigDefaults: Map<String, Any> = mapOf(
"$keyA" to "$valueA",
"$keyB" to "$valueB"
)
}
""".trimIndent(), sut.getContent().toString()
)
}

@Test
fun `given an empty list of remote fields, when building the object, then generate empty list of remote fields`() {
// given
val remoteFields = emptyMap<String, String>()

// when
val sut = RemoteFieldConfigDefaultsBuilder(remoteFields)

// then
assertEquals(
"""
// Automatically generated file. DO NOT MODIFY
package org.wordpress.android.util.config
import kotlin.Any
import kotlin.String
import kotlin.collections.Map
public object RemoteFieldConfigDefaults {
public val remoteFieldConfigDefaults: Map<String, Any> = mapOf(
)
}
""".trimIndent(), sut.getContent().toString()
)
}
}

0 comments on commit 40ece18

Please sign in to comment.