Skip to content

Commit 4c0ec3d

Browse files
author
Marcel Schnelle
authored
Remove Closure-based DSL & migrate to Action<T> (#123)
This type can be automatically used as a lambda expressions & Closures. This potentially allows for a cleaner integration with both Groovy and the Kotlin DSL for Gradle. At the same time, the obsolete Callable0 and Callable1<T> types are deleted now. They weren’t even used anymore, and they originally tried to achieve the same thing. Good riddance!
1 parent c7c52b9 commit 4c0ec3d

File tree

5 files changed

+52
-186
lines changed

5 files changed

+52
-186
lines changed

android-junit5-tests/src/test/groovy/de/mannodermaus/gradle/plugins/junit5/CallableGroovyTests.groovy

Lines changed: 0 additions & 28 deletions
This file was deleted.

android-junit5-tests/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/CallableKotlinSpec.kt

Lines changed: 0 additions & 35 deletions
This file was deleted.

android-junit5-tests/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/PluginSpec.kt

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import de.mannodermaus.gradle.plugins.junit5.util.junitPlatformOptions
1515
import de.mannodermaus.gradle.plugins.junit5.util.throws
1616
import de.mannodermaus.gradle.plugins.junit5.util.times
1717
import org.assertj.core.api.Assertions.assertThat
18+
import org.gradle.api.Action
1819
import org.gradle.api.ProjectConfigurationException
1920
import org.gradle.api.Task
2021
import org.gradle.api.internal.plugins.PluginApplicationException
@@ -62,9 +63,9 @@ class PluginSpec : Spek({
6263
.build()
6364

6465
project.android.testOptions.junitPlatform {
65-
filters("unknown") {
66-
includeTags("doesnt-matter")
67-
}
66+
filters("unknown", Action {
67+
it.includeTags("doesnt-matter")
68+
})
6869
}
6970

7071
it("doesn't have a filters extension point for an unknown build type") {
@@ -658,19 +659,19 @@ class PluginSpec : Spek({
658659
excludeTags("global-exclude-tag")
659660
includePattern("com.example.package1")
660661
}
661-
filters("paid") {
662-
includeEngines("paid-include-engine")
663-
includePattern("com.example.paid")
664-
excludePattern("com.example.package1")
665-
}
666-
filters("freeDebug") {
667-
includeTags("freeDebug-include-tag")
668-
}
669-
filters("paidRelease") {
670-
includeTags("paidRelease-include-tag")
671-
includeTags("global-exclude-tag")
672-
includePattern("com.example.paid.release")
673-
}
662+
filters("paid", Action {
663+
it.includeEngines("paid-include-engine")
664+
it.includePattern("com.example.paid")
665+
it.excludePattern("com.example.package1")
666+
})
667+
filters("freeDebug", Action {
668+
it.includeTags("freeDebug-include-tag")
669+
})
670+
filters("paidRelease", Action {
671+
it.includeTags("paidRelease-include-tag")
672+
it.includeTags("global-exclude-tag")
673+
it.includePattern("com.example.paid.release")
674+
})
674675
}
675676

676677
project.evaluate()
@@ -748,18 +749,18 @@ class PluginSpec : Spek({
748749
includeEngines("global-include-engine")
749750
includePattern("pattern123")
750751
}
751-
filters("debug") {
752-
excludeTags("debug-exclude-tag")
753-
excludeEngines("debug-exclude-engine")
754-
excludePattern("pattern123")
755-
excludePattern("debug-pattern")
756-
}
757-
filters("release") {
758-
includeTags("rel-include-tag")
759-
includeEngines("rel-include-engine")
760-
excludeEngines("global-include-engine")
761-
includePattern("release-pattern")
762-
}
752+
filters("debug", Action {
753+
it.excludeTags("debug-exclude-tag")
754+
it.excludeEngines("debug-exclude-engine")
755+
it.excludePattern("pattern123")
756+
it.excludePattern("debug-pattern")
757+
})
758+
filters("release", Action {
759+
it.includeTags("rel-include-tag")
760+
it.includeEngines("rel-include-engine")
761+
it.excludeEngines("global-include-engine")
762+
it.includePattern("release-pattern")
763+
})
763764
}
764765

765766
project.evaluate()

android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/Dsl.kt

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import groovy.lang.GroovyObjectSupport
99
import org.gradle.api.Action
1010
import org.gradle.api.Project
1111
import org.gradle.api.tasks.Input
12-
import org.gradle.util.ConfigureUtil
1312
import org.junit.platform.commons.util.Preconditions
1413
import java.io.File
1514

@@ -59,11 +58,11 @@ internal fun evaluateDsl(project: Project) {
5958
val ju5 = project.android.testOptions
6059
.extensionByName<AndroidJUnitPlatformExtension>(EXTENSION_NAME)
6160

62-
ju5._filters.forEach { qualifier, configs ->
61+
ju5._filters.forEach { qualifier, actions ->
6362
val extensionName = ju5.filtersExtensionName(qualifier)
6463
val extension = ju5.extensionByName<FiltersExtension>(extensionName)
65-
configs.forEach { config ->
66-
extension.config()
64+
actions.forEach { action ->
65+
action.execute(extension)
6766
}
6867
}
6968
}
@@ -97,11 +96,11 @@ open class AndroidJUnitPlatformExtension(private val project: Project) : GroovyO
9796
// Support for filters() DSL called from Groovy
9897
val qualifier = name.substring(0, name.indexOf("Filters"))
9998
val closure = (args as Array<*>)[0] as Closure<*>
100-
return this.filters(qualifier) {
101-
closure.delegate = this
99+
return this.filters(qualifier, Action {
100+
closure.delegate = it
102101
closure.resolveStrategy = Closure.DELEGATE_FIRST
103-
closure.call(this)
104-
}
102+
closure.call(it)
103+
})
105104
}
106105

107106
return null
@@ -133,7 +132,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) : GroovyO
133132

134133
/* Filters */
135134

136-
internal val _filters = mutableMapOf<String?, MutableList<FiltersExtension.() -> Unit>>()
135+
internal val _filters = mutableMapOf<String?, MutableList<Action<FiltersExtension>>>()
137136

138137
internal fun filtersExtensionName(qualifier: String? = null) = if (qualifier.isNullOrEmpty())
139138
FILTERS_EXTENSION_NAME
@@ -149,18 +148,8 @@ open class AndroidJUnitPlatformExtension(private val project: Project) : GroovyO
149148
/**
150149
* Configure the {@link FiltersExtension}
151150
* for all executed tests, applied to all variants
152-
* (Kotlin version)
153-
*/
154-
fun filters(config: FiltersExtension.() -> Unit) {
155-
filters(null, config)
156-
}
157-
158-
/**
159-
* Configure the {@link FiltersExtension}
160-
* for all executed tests, applied to all variants
161-
* (Groovy version)
162151
*/
163-
fun filters(action: Action<FiltersExtension>) = filters(null) { action.execute(this) }
152+
fun filters(action: Action<FiltersExtension>) = filters(null, Action { action.execute(it) })
164153

165154
/**
166155
* Return the {@link FiltersExtension}
@@ -174,22 +163,13 @@ open class AndroidJUnitPlatformExtension(private val project: Project) : GroovyO
174163
/**
175164
* Configure the {@link FiltersExtension}
176165
* for tests that belong to the provided build variant
177-
* (Kotlin version)
178166
*/
179-
fun filters(qualifier: String? = null, config: FiltersExtension.() -> Unit) {
180-
val configs = _filters.getOrDefault(qualifier, mutableListOf())
181-
configs += config
182-
_filters[qualifier] = configs
167+
fun filters(qualifier: String? = null, action: Action<FiltersExtension>) {
168+
val actions = _filters.getOrDefault(qualifier, mutableListOf())
169+
actions += action
170+
_filters[qualifier] = actions
183171
}
184172

185-
/**
186-
* Configure the {@link FiltersExtension}
187-
* for tests that belong to the provided build variant
188-
* (Groovy version)
189-
*/
190-
fun filters(qualifier: String? = null, action: Action<FiltersExtension>) = filters(
191-
qualifier) { action.execute(this) }
192-
193173
/* Android Instrumentation Test support */
194174

195175
/**
@@ -204,8 +184,8 @@ open class AndroidJUnitPlatformExtension(private val project: Project) : GroovyO
204184
*
205185
* @since 1.0.22
206186
*/
207-
fun instrumentationTests(closure: Closure<InstrumentationTestOptions>) {
208-
ConfigureUtil.configure(closure, instrumentationTests)
187+
fun instrumentationTests(action: Action<InstrumentationTestOptions>) {
188+
action.execute(instrumentationTests)
209189
}
210190

211191
/* Jacoco Reporting Integration */
@@ -218,8 +198,8 @@ open class AndroidJUnitPlatformExtension(private val project: Project) : GroovyO
218198
/**
219199
* Options for controlling Jacoco reporting
220200
*/
221-
fun jacocoOptions(closure: Closure<JacocoOptions>) {
222-
ConfigureUtil.configure(closure, jacocoOptions)
201+
fun jacocoOptions(action: Action<JacocoOptions>) {
202+
action.execute(jacocoOptions)
223203
}
224204
}
225205

@@ -533,8 +513,8 @@ class JacocoOptions {
533513
/**
534514
* Options for controlling the HTML Report generated by Jacoco
535515
*/
536-
fun html(closure: Closure<Report>) {
537-
ConfigureUtil.configure(closure, html)
516+
fun html(action: Action<Report>) {
517+
action.execute(html)
538518
}
539519

540520
/**
@@ -545,8 +525,8 @@ class JacocoOptions {
545525
/**
546526
* Options for controlling the CSV Report generated by Jacoco
547527
*/
548-
fun csv(closure: Closure<Report>) {
549-
ConfigureUtil.configure(closure, csv)
528+
fun csv(action: Action<Report>) {
529+
action.execute(csv)
550530
}
551531

552532
/**
@@ -557,8 +537,8 @@ class JacocoOptions {
557537
/**
558538
* Options for controlling the XML Report generated by Jacoco
559539
*/
560-
fun xml(closure: Closure<Report>) {
561-
ConfigureUtil.configure(closure, xml)
540+
fun xml(action: Action<Report>) {
541+
action.execute(xml)
562542
}
563543

564544
/**

android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/Interop.kt

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,55 +37,3 @@ class VariantTypeCompat {
3737
val UNIT_TEST_SUFFIX = GroovyInterop.variantType_unitTestSuffix()
3838
}
3939
}
40-
41-
/**
42-
* Multi-language functional construct with no parameters,
43-
* mapped to Groovy's dynamic Closures as well as Kotlin's invoke syntax.
44-
*
45-
* A [Callable0] can be invoked with the short-hand
46-
* function syntax from both Kotlin & Groovy:
47-
*
48-
* <code><pre>
49-
* val callable = Callable0 { 2 + 2 }
50-
* val result = callable() // result == 4
51-
* </pre></code>
52-
*
53-
* <code><pre>
54-
* def callable = new Callable0({ 2 + 2 })
55-
* def result = callable() // result == 4
56-
* </pre></code>
57-
*/
58-
@Suppress("unused")
59-
class Callable0<R>(private val body: () -> R) : Closure<R>(null) {
60-
/** Kotlin's call syntax */
61-
operator fun invoke(): R = body()
62-
63-
/** Groovy's call syntax */
64-
fun doCall(): R = body()
65-
}
66-
67-
/**
68-
* Multi-language functional construct with 1 parameter,
69-
* mapped to Groovy's dynamic Closures as well as Kotlin's invoke syntax.
70-
*
71-
* A [Callable1] can be invoked with the short-hand
72-
* function syntax from both Kotlin & Groovy:
73-
*
74-
* <code><pre>
75-
* val callable = Callable1 { 2 + it }
76-
* val result = callable(2) // result == 4
77-
* </pre></code>
78-
*
79-
* <code><pre>
80-
* def callable = new Callable1({ input -> 2 + input })
81-
* def result = callable(2) // result == 4
82-
* </pre></code>
83-
*/
84-
@Suppress("unused")
85-
class Callable1<T : Any, out R : Any>(private val body: T.() -> R?) : Closure<T>(null) {
86-
/** Kotlin's call syntax */
87-
operator fun invoke(arg: T): R? = arg.body()
88-
89-
/** Groovy's call syntax */
90-
fun doCall(arg: T): R? = arg.body()
91-
}

0 commit comments

Comments
 (0)