-
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.
introduce InjectByConstructor interface
- Loading branch information
1 parent
ebc996d
commit ef1ebbc
Showing
4 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
dikt-annotations/src/commonMain/kotlin/dev/shustoff/dikt/InjectByConstructor.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 InjectByConstructor |
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
66 changes: 66 additions & 0 deletions
66
dikt-compiler-plugin/src/test/kotlin/dev/shustoff/dikt/compiler/InjectByConstructorTest.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,66 @@ | ||
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 InjectByConstructorTest { | ||
@Rule | ||
@JvmField | ||
var folder: TemporaryFolder = TemporaryFolder() | ||
|
||
@Test | ||
fun `allow constructor calls for InjectByConstructor implementations`() { | ||
val result = compile( | ||
folder.root, | ||
SourceFile.kotlin( | ||
"MyModule.kt", | ||
""" | ||
package dev.shustoff.dikt.compiler | ||
import dev.shustoff.dikt.* | ||
class Dependency : InjectByConstructor | ||
class Injectable(val dependency: Dependency): InjectByConstructor | ||
class MyModule { | ||
fun injectable(): Injectable = resolve() | ||
} | ||
""" | ||
) | ||
) | ||
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK) | ||
} | ||
|
||
@Test | ||
fun `resolve InjectByConstructor from nested module if parameters for InjectByConstructor not available`() { | ||
val result = compile( | ||
folder.root, | ||
SourceFile.kotlin( | ||
"MyModule.kt", | ||
""" | ||
package dev.shustoff.dikt.compiler | ||
import dev.shustoff.dikt.* | ||
class Dependency | ||
class Injectable(val dependency: Dependency): InjectByConstructor | ||
class NestedModule(private val dependency: Dependency) { | ||
fun injectable() = resolve<Injectable>() | ||
} | ||
class MyModule(@ProvidesMembers val module: NestedModule) { | ||
fun injectable(): Injectable = resolve() | ||
} | ||
""" | ||
) | ||
) | ||
Truth.assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK) | ||
} | ||
} |