-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support singletons without annotation in module
- Loading branch information
1 parent
a8500b5
commit 26f511a
Showing
7 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...compiler-plugin/src/test/kotlin/dev/shustoff/dikt/compiler/InjectableSingleInScopeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package dev.shustoff.dikt.compiler | ||
|
||
import com.google.common.truth.Truth | ||
import com.tschuchort.compiletesting.KotlinCompilation | ||
import com.tschuchort.compiletesting.SourceFile | ||
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
|
||
@OptIn(ExperimentalCompilerApi::class) | ||
class InjectableSingleInScopeTest { | ||
|
||
@Rule | ||
@JvmField | ||
var folder: TemporaryFolder = TemporaryFolder() | ||
|
||
|
||
@Test | ||
fun `can compile for singleton injectable`() { | ||
val result = compile( | ||
folder.root, | ||
SourceFile.kotlin( | ||
"MyModule.kt", | ||
""" | ||
package dev.shustoff.dikt.compiler | ||
import dev.shustoff.dikt.* | ||
class Scope | ||
class TestObject : InjectableSingleInScope<Scope> | ||
@ModuleScopes(Scope::class) | ||
class MyModule { | ||
fun injectable(): TestObject = resolve() | ||
} | ||
""" | ||
) | ||
) | ||
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK) | ||
} | ||
|
||
@Test | ||
fun `fail on parameters in cached injectable`() { | ||
val result = compile( | ||
folder.root, | ||
SourceFile.kotlin( | ||
"MyModule.kt", | ||
""" | ||
package dev.shustoff.dikt.compiler | ||
import dev.shustoff.dikt.* | ||
class Scope | ||
class TestObject(val name: String) : InjectableSingleInScope<Scope> | ||
@ModuleScopes(Scope::class) | ||
class MyModule { | ||
fun injectable(name: String): TestObject = resolve() | ||
} | ||
""" | ||
) | ||
) | ||
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR) | ||
Truth.assertThat(result.messages).contains("Can't resolve dependency kotlin.String") | ||
} | ||
|
||
@Test | ||
fun `don't resolve singleton from nested module (because it's singleton)`() { | ||
val result = compile( | ||
folder.root, | ||
SourceFile.kotlin( | ||
"MyModule.kt", | ||
""" | ||
package dev.shustoff.dikt.compiler | ||
import dev.shustoff.dikt.* | ||
class Scope | ||
class Dependency | ||
class TestObject(val dependency: Dependency): InjectableSingleInScope<Scope> | ||
class NestedModule(private val dependency: Dependency) { | ||
fun injectable() = resolve<TestObject>() | ||
} | ||
@ModuleScopes(Scope::class) | ||
class MyModule(@ProvidesMembers val module: NestedModule) { | ||
fun injectable(): TestObject = resolve() | ||
} | ||
""" | ||
) | ||
) | ||
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR) | ||
Truth.assertThat(result.messages).contains("Can't resolve dependency dev.shustoff.dikt.compiler.TestObject") | ||
} | ||
|
||
@Test | ||
fun `fail on cached extension functions`() { | ||
val result = compile( | ||
folder.root, | ||
SourceFile.kotlin( | ||
"MyModule.kt", | ||
""" | ||
package dev.shustoff.dikt.compiler | ||
import dev.shustoff.dikt.* | ||
class Scope | ||
class TestObject(val name: String) : InjectableSingleInScope<Scope> | ||
fun String.injectable(): TestObject = resolve() | ||
""" | ||
) | ||
) | ||
|
||
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR) | ||
Truth.assertThat(result.messages).contains("Can't resolve dependency dev.shustoff.dikt.compiler.TestObject") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
dikt-internal/src/commonMain/kotlin/dev/shustoff/dikt/ModuleScopes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package dev.shustoff.dikt | ||
|
||
import kotlin.reflect.KClass | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.SOURCE) | ||
annotation class ModuleScopes(vararg val scopes: KClass<*>) |
3 changes: 3 additions & 0 deletions
3
dikt/src/commonMain/kotlin/dev/shustoff/dikt/InjectableSingleInScope.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dev.shustoff.dikt | ||
|
||
interface InjectableSingleInScope<T: Any> |