Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stitch/api/stitch.api
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public abstract interface class com/harrytmthy/stitch/api/Qualifier {
}

public final class com/harrytmthy/stitch/api/QualifierKt {
public static final fun named (Ljava/lang/Enum;)Lcom/harrytmthy/stitch/api/Named;
public static final fun named (Ljava/lang/String;)Lcom/harrytmthy/stitch/api/Named;
public static final fun typed (Lkotlin/reflect/KClass;)Lcom/harrytmthy/stitch/api/Typed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ internal object QualifierManager {
*/
fun named(value: String): Named = QualifierManager.getOrCreate(value)

/**
* Returns a pooled [Named] qualifier for the given enum [value], creating one if it doesn't exist.
*/
fun named(value: Enum<*>): Named = QualifierManager.getOrCreate(value.name)

/**
* Returns a pooled [Typed] qualifier for the given type, creating one if it doesn't exist.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.harrytmthy.stitch.api.dependsOn
import com.harrytmthy.stitch.api.module
import com.harrytmthy.stitch.api.named
import com.harrytmthy.stitch.api.scope
import com.harrytmthy.stitch.api.typed
import com.harrytmthy.stitch.exception.CycleException
import com.harrytmthy.stitch.exception.MissingBindingException
import com.harrytmthy.stitch.exception.ScopeClosedException
Expand Down Expand Up @@ -844,4 +845,62 @@ class StitchTest {
assertSame(fragmentScopedC.b, fragmentScopeInstance.get<B>())
assertSame(activityScopeInstance.get<B>(), Stitch.get<B>())
}

@Test
fun `named enum qualifier should resolve the same instance as its string equivalent`() {
module {
singleton(qualifier = named(Environment.Prod)) { Logger() }
}.register()

val loggerFromEnum = Stitch.get<Logger>(named(Environment.Prod))
val loggerFromString = Stitch.get<Logger>(named("Prod"))
assertSame(loggerFromEnum, loggerFromString)
}

@Test
fun `named enum qualifiers with different values should not clash`() {
module {
singleton(qualifier = named(Environment.Prod)) { Logger() }
singleton(qualifier = named(Environment.Staging)) { Logger() }
}.register()

val prodLogger = Stitch.get<Logger>(named(Environment.Prod))
val stagingLogger = Stitch.get<Logger>(named(Environment.Staging))
assertNotSame(prodLogger, stagingLogger)
}

@Test
fun `typed qualifier should resolve the same instance across calls`() {
module {
singleton(qualifier = typed<Environment>()) { Logger() }
}.register()

val first = Stitch.get<Logger>(typed<Environment>())
val second = Stitch.get<Logger>(typed<Environment>())
assertSame(first, second)
}

@Test
fun `default singleton and typed qualified singleton should not clash`() {
module {
singleton { Logger() }
singleton(qualifier = typed<Environment>()) { Logger() }
}.register()

val default = Stitch.get<Logger>()
val typed = Stitch.get<Logger>(typed<Environment>())
assertNotSame(default, typed)
}

@Test
fun `typed and named qualifiers of same type should not clash`() {
module {
singleton(qualifier = typed<Environment>()) { Logger() }
singleton(qualifier = named("Environment")) { Logger() }
}.register()

val typedLogger = Stitch.get<Logger>(typed<Environment>())
val namedLogger = Stitch.get<Logger>(named("Environment"))
assertNotSame(typedLogger, namedLogger)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class C(val a: A, val b: B)
class UsesLazyFactory(val barLazy: Lazy<Bar>)
class Bar

enum class Environment { Prod, Staging }

interface LifecycleTracker
class ViewModelLifecycleTracker : LifecycleTracker
class ActivityLifecycleTracker : LifecycleTracker
Expand Down
Loading