Skip to content

Commit 40ece18

Browse files
authored
Merge pull request #20213 from wordpress-mobile/processor_unit_tests
Add `:libs:processors` unit tests
2 parents db5bbc7 + ec1baba commit 40ece18

10 files changed

+188
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ext {
8585
kotlinxCoroutinesVersion = '1.7.3'
8686
lottieVersion = '6.1.0'
8787
philjayMpAndroidChartVersion = 'v3.1.0'
88-
squareupKotlinPoetVersion = '1.6.0'
88+
squareupKotlinPoetVersion = '1.16.0'
8989
squareupOkioVersion = '3.6.0'
9090
squareupRetrofitVersion = '2.9.0'
9191
uCropVersion = '2.2.8'

libs/processors/src/main/java/org/wordpress/android/processor/FeaturesInDevelopmentDefaultsBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class FeaturesInDevelopmentDefaultsBuilder(private val featuresInDevelopment: Li
2828
.build()
2929
return FileSpec.builder("org.wordpress.android.util.config", "FeaturesInDevelopment")
3030
.addType(remoteConfigDefaults)
31-
.addComment("Automatically generated file. DO NOT MODIFY")
31+
.addFileComment("Automatically generated file. DO NOT MODIFY")
3232
.indent(" ")
3333
.build()
3434
}

libs/processors/src/main/java/org/wordpress/android/processor/RemoteConfigProcessor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.wordpress.android.processor
22

33
import com.google.auto.service.AutoService
4+
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
45
import com.squareup.kotlinpoet.TypeName
56
import com.squareup.kotlinpoet.asTypeName
67
import org.wordpress.android.annotation.Experiment
@@ -26,6 +27,7 @@ import javax.tools.Diagnostic.Kind
2627
"org.wordpress.android.annotation.RemoteFieldDefaultGenerater"
2728
)
2829
class RemoteConfigProcessor : AbstractProcessor() {
30+
@OptIn(DelicateKotlinPoetApi::class)
2931
@Suppress("DEPRECATION")
3032
override fun process(p0: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean {
3133
val experiments = roundEnvironment?.getElementsAnnotatedWith(Experiment::class.java)?.map { element ->

libs/processors/src/main/java/org/wordpress/android/processor/RemoteFeatureConfigCheckBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class RemoteFeatureConfigCheckBuilder(private val remoteFeatures: List<TypeName>
4242
.build()
4343
return FileSpec.builder("org.wordpress.android.util.config", "RemoteFeatureConfigCheck")
4444
.addType(remoteFeatureConfigDefaults)
45-
.addComment("Automatically generated file. DO NOT MODIFY")
45+
.addFileComment("Automatically generated file. DO NOT MODIFY")
4646
.indent(" ")
4747
.build()
4848
}

libs/processors/src/main/java/org/wordpress/android/processor/RemoteFeatureConfigDefaultsBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class RemoteFeatureConfigDefaultsBuilder(private val defaults: Map<String, Strin
3131
.build()
3232
return FileSpec.builder("org.wordpress.android.util.config", FILE_NAME)
3333
.addType(remoteConfigDefaults)
34-
.addComment("Automatically generated file. DO NOT MODIFY")
34+
.addFileComment("Automatically generated file. DO NOT MODIFY")
3535
.indent(" ")
3636
.build()
3737
}

libs/processors/src/main/java/org/wordpress/android/processor/RemoteFieldConfigDefaultsBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class RemoteFieldConfigDefaultsBuilder(private val defaults: Map<String, String>
3131
.build()
3232
return FileSpec.builder("org.wordpress.android.util.config", FILE_NAME)
3333
.addType(remoteConfigDefaults)
34-
.addComment("Automatically generated file. DO NOT MODIFY")
34+
.addFileComment("Automatically generated file. DO NOT MODIFY")
3535
.indent(" ")
3636
.build()
3737
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.wordpress.android.processor
2+
3+
import org.assertj.core.api.Assertions
4+
import org.junit.Test
5+
6+
class FeaturesInDevelopmentDefaultsBuilderTest {
7+
@Test
8+
fun `given a list of features in development, when building the object, then generate the correct list`() {
9+
// given
10+
val featureA = "valueA"
11+
val featureB = "valueB"
12+
val features = listOf(featureA, featureB)
13+
14+
// when
15+
val sut = FeaturesInDevelopmentDefaultsBuilder(features)
16+
17+
// then
18+
Assertions.assertThat(sut.getContent().toString()).isEqualTo(
19+
"""
20+
// Automatically generated file. DO NOT MODIFY
21+
package org.wordpress.android.util.config
22+
23+
import kotlin.String
24+
import kotlin.collections.List
25+
26+
public object FeaturesInDevelopment {
27+
public val featuresInDevelopment: List<String> = listOf(
28+
"$featureA",
29+
"$featureB"
30+
)
31+
}
32+
33+
""".trimIndent()
34+
)
35+
}
36+
37+
@Test
38+
fun `given an empty list of features in development, when building the object, then generate empty list`() {
39+
// given
40+
val features = emptyList<String>()
41+
42+
// when
43+
val sut = FeaturesInDevelopmentDefaultsBuilder(features)
44+
45+
// then
46+
Assertions.assertThat(sut.getContent().toString()).isEqualTo(
47+
"""
48+
// Automatically generated file. DO NOT MODIFY
49+
package org.wordpress.android.util.config
50+
51+
import kotlin.String
52+
import kotlin.collections.List
53+
54+
public object FeaturesInDevelopment {
55+
public val featuresInDevelopment: List<String> = listOf(
56+
)
57+
}
58+
59+
""".trimIndent()
60+
)
61+
}
62+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.wordpress.android.processor
2+
3+
import com.squareup.kotlinpoet.ClassName
4+
import org.assertj.core.api.Assertions
5+
import org.junit.Test
6+
7+
class RemoteFeatureConfigCheckBuilderTest {
8+
@Test
9+
fun `given feature classes, when building config check, then generate the correct checks`() {
10+
// given
11+
val classA = "customClassA"
12+
val classB = "customClassB"
13+
val features = listOf(
14+
ClassName("org.wordpress", listOf(classA)),
15+
ClassName("org.wordpress", listOf(classB))
16+
)
17+
18+
// when
19+
val sut = RemoteFeatureConfigCheckBuilder(features)
20+
21+
// then
22+
Assertions.assertThat(sut.getContent().toString()).isEqualTo(
23+
"""
24+
// Automatically generated file. DO NOT MODIFY
25+
package org.wordpress.android.util.config
26+
27+
import org.wordpress.$classA
28+
import org.wordpress.$classB
29+
30+
public class RemoteFeatureConfigCheck(
31+
public val appConfig: AppConfig,
32+
) {
33+
public val $classA: $classA = $classA(appConfig)
34+
35+
public val $classB: $classB = $classB(appConfig)
36+
37+
public fun checkRemoteFields() {
38+
if ($classA.remoteField == null) {
39+
throw IllegalArgumentException(""${'"'}org.wordpress.$classA needs to define
40+
remoteField""${'"'})
41+
}
42+
if ($classB.remoteField == null) {
43+
throw IllegalArgumentException(""${'"'}org.wordpress.$classB needs to define
44+
remoteField""${'"'})
45+
}
46+
}
47+
}
48+
49+
""".trimIndent()
50+
)
51+
}
52+
}

libs/processors/src/test/kotlin/org/wordpress/android/processor/RemoteFeatureConfigDefaultsBuilderTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class RemoteFeatureConfigDefaultsBuilderTest {
2424
import kotlin.String
2525
import kotlin.collections.Map
2626
27-
object RemoteFeatureConfigDefaults {
28-
val remoteFeatureConfigDefaults: Map<String, Any> = mapOf(
27+
public object RemoteFeatureConfigDefaults {
28+
public val remoteFeatureConfigDefaults: Map<String, Any> = mapOf(
2929
"$keyA" to "$valueA",
3030
"$keyB" to "$valueB"
3131
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.wordpress.android.processor
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
class RemoteFieldConfigDefaultsBuilderTest {
7+
@Test
8+
fun `given a list of remote fields, when building the object, then generate list of remote fields`() {
9+
// given
10+
val keyA = "keyA"
11+
val valueA = "valueA"
12+
val keyB = "keyB"
13+
val valueB = "valueB"
14+
15+
// when
16+
val sut = RemoteFieldConfigDefaultsBuilder(mapOf(keyA to valueA, keyB to valueB))
17+
18+
// then
19+
assertEquals(
20+
"""
21+
// Automatically generated file. DO NOT MODIFY
22+
package org.wordpress.android.util.config
23+
24+
import kotlin.Any
25+
import kotlin.String
26+
import kotlin.collections.Map
27+
28+
public object RemoteFieldConfigDefaults {
29+
public val remoteFieldConfigDefaults: Map<String, Any> = mapOf(
30+
"$keyA" to "$valueA",
31+
"$keyB" to "$valueB"
32+
)
33+
}
34+
35+
""".trimIndent(), sut.getContent().toString()
36+
)
37+
}
38+
39+
@Test
40+
fun `given an empty list of remote fields, when building the object, then generate empty list of remote fields`() {
41+
// given
42+
val remoteFields = emptyMap<String, String>()
43+
44+
// when
45+
val sut = RemoteFieldConfigDefaultsBuilder(remoteFields)
46+
47+
// then
48+
assertEquals(
49+
"""
50+
// Automatically generated file. DO NOT MODIFY
51+
package org.wordpress.android.util.config
52+
53+
import kotlin.Any
54+
import kotlin.String
55+
import kotlin.collections.Map
56+
57+
public object RemoteFieldConfigDefaults {
58+
public val remoteFieldConfigDefaults: Map<String, Any> = mapOf(
59+
)
60+
}
61+
62+
""".trimIndent(), sut.getContent().toString()
63+
)
64+
}
65+
}

0 commit comments

Comments
 (0)