Skip to content

Commit 7f37284

Browse files
committed
add tests
1 parent 5b92ce9 commit 7f37284

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

gradle-plugin-utils/api/gradle-plugin-utils.api

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public final class build/extensions/oss/gradle/pluginutils/DurationUtilsKt {
88
}
99

1010
public final class build/extensions/oss/gradle/pluginutils/ExtensionUtilsKt {
11-
public static final fun extension (Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Object;
1211
public static final fun requiredExtension (Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Object;
1312
}
1413

gradle-plugin-utils/src/main/kotlin/build/extensions/oss/gradle/pluginutils/ExtensionUtils.kt

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,6 @@ package build.extensions.oss.gradle.pluginutils
22

33
import org.gradle.api.plugins.ExtensionAware
44

5-
/**
6-
* Gets the extension of the given name if it exists.
7-
*
8-
* Will return `null` if the receiver is not [ExtensionAware].
9-
*
10-
* @receiver the object containing extensions
11-
* @param name the extension name
12-
* @return the extension, or `null` if it does not exist
13-
*/
14-
inline fun <reified T : Any> Any.extension(name: String): T? {
15-
@Suppress("UNCHECKED_CAST")
16-
return extension(name, T::class.java)
17-
}
18-
19-
/**
20-
* Gets the extension of the given name if it exists.
21-
*
22-
* Will return `null` if the receiver is not [ExtensionAware].
23-
*
24-
* @receiver the object containing extensions
25-
* @param name the extension name
26-
* @return the extension, or `null` if it does not exist
27-
*/
28-
fun <T : Any> Any.extension(name: String, clazz: Class<T>): T? {
29-
val resultCandidate = (this as? ExtensionAware)?.extensions?.findByName(name)
30-
31-
return clazz.cast(resultCandidate)
32-
}
33-
345
/**
356
* Gets the extension of the given name if it exists.
367
*
@@ -61,19 +32,6 @@ fun <T : Any> Any.requiredExtension(name: String, clazz: Class<T>): T {
6132
}
6233

6334

64-
/**
65-
* Gets the extension of the given type if it exists.
66-
*
67-
* Will return `null` if the receiver is not [ExtensionAware].
68-
*
69-
* @receiver the object containing extensions
70-
* @param <T> the extension type
71-
* @return the extension, or `null` if it does not exist
72-
*/
73-
inline fun <reified T : Any> Any.extension(): T? =
74-
(this as? ExtensionAware)?.extensions?.findByType(typeOf<T>())
75-
76-
7735
/**
7836
* Gets the extension of the given type, throwing an exception if it does not exist.
7937
*
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package build.extensions.oss.gradle.pluginutils
2+
3+
import io.kotest.assertions.throwables.shouldThrow
4+
import io.kotest.matchers.should
5+
import io.kotest.matchers.string.haveSubstring
6+
import io.kotest.matchers.types.beTheSameInstanceAs
7+
import io.mockk.every
8+
import io.mockk.mockk
9+
import org.gradle.api.plugins.ExtensionAware
10+
import org.gradle.api.plugins.ExtensionContainer
11+
import org.junit.jupiter.api.Test
12+
13+
class ExtensionUtilsTest {
14+
15+
/**
16+
* Ensure we don't do strict cast internally
17+
*/
18+
@Test
19+
fun shouldReturnNullForNonExtensionAware() {
20+
val expectedMessages = listOf(
21+
"org.gradle.api.plugins.ExtensionAware",
22+
"cannot be cast to"
23+
)
24+
val somethingWhichIsNotExtension = "131223"
25+
26+
val exception = shouldThrow<Exception> { somethingWhichIsNotExtension.requiredExtension<String>("abc") }
27+
28+
expectedMessages.forEach { message ->
29+
exception.message should haveSubstring(message)
30+
}
31+
}
32+
33+
/**
34+
* Check if we propagate the call to API below.
35+
* Basically, we ensure that our intermediate code doesn't have something new.
36+
*/
37+
@Test
38+
fun shouldPropagateLookupCall() {
39+
val extensionName = "abc"
40+
// some object which will play a role of extension
41+
val extensionToResolve = Runnable {}
42+
43+
val extensionsContainer = mockk<ExtensionContainer> {
44+
every { getByName(extensionName) } returns extensionToResolve
45+
}
46+
val extensionAware = mockk<ExtensionAware> {
47+
every { extensions } returns extensionsContainer
48+
}
49+
50+
val exception = extensionAware.requiredExtension<Runnable>(extensionName)
51+
52+
exception should beTheSameInstanceAs(extensionToResolve)
53+
}
54+
}

0 commit comments

Comments
 (0)