Skip to content

Commit

Permalink
fix some bug
Browse files Browse the repository at this point in the history
  • Loading branch information
MeilCli committed Aug 20, 2019
1 parent f6aa9d6 commit 5ce9e0b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
33 changes: 22 additions & 11 deletions lib/src/main/kotlin/mindy/Mindy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Mindy : IReadOnlyMindy {

private val entries = mutableMapOf<String, Entry<*>>()
private val transactionEntries = mutableMapOf<String, Entry<*>>()
internal val transactionEntryCount get() = transactionEntries.size
private var isTransaction = false

fun beginTransaction() {
Expand All @@ -51,6 +52,7 @@ class Mindy : IReadOnlyMindy {
fun commitTransaction() {
isTransaction = false
entries.putAll(transactionEntries)
transactionEntries.clear()
}

fun rollbackTransaction() {
Expand All @@ -75,16 +77,9 @@ class Mindy : IReadOnlyMindy {
}
}

private fun findEntry(
typeIdentifier: String,
name: String = "",
additionalResolver: (Mindy.() -> Unit)? = null
): Entry<*> {
beginTransaction()
additionalResolver?.invoke(this)

private fun findEntry(typeIdentifier: String, name: String = ""): Entry<*> {
val identifier = identifier(typeIdentifier, name)
return transactionEntries[identifier]?.also { rollbackTransaction() }
return transactionEntries[identifier]
?: entries[identifier]
?: throw IllegalStateException("$identifier is not registered")
}
Expand All @@ -95,7 +90,15 @@ class Mindy : IReadOnlyMindy {
name: String,
additionalResolver: (Mindy.() -> Unit)?
): T {
return findEntry(typeIdentifier, name, additionalResolver).resolve(this) as T
beginTransaction()
additionalResolver?.invoke(this)

val result = findEntry(typeIdentifier, name).resolve(this) as T

if (additionalResolver != null) {
rollbackTransaction()
}
return result
}

@Suppress("UNCHECKED_CAST")
Expand All @@ -104,7 +107,15 @@ class Mindy : IReadOnlyMindy {
name: String,
additionalResolver: (Mindy.() -> Unit)?
): T {
return findEntry(typeIdentifier, name, additionalResolver).create(this) as T
beginTransaction()
additionalResolver?.invoke(this)

val result = findEntry(typeIdentifier, name).create(this) as T

if (additionalResolver != null) {
rollbackTransaction()
}
return result
}
}

Expand Down
73 changes: 70 additions & 3 deletions lib/src/test/kotlin/mindy/MindyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class MindyTest {
register<IValue<String>> { StringValue("b") }
}

assertEquals(0, mindy.transactionEntryCount)

assertEquals("a", a.value.value)
assertEquals("a", b.value.value) // because resolve() is singleton
}
Expand All @@ -137,13 +139,44 @@ class MindyTest {
register<IValue<Int>> { Value(1) }
}

assertEquals(0, mindy.transactionEntryCount)

assertEquals("a", a.value.value)

assertThrows<IllegalStateException> {
mindy.resolve<IValue<IValue<Int>>>()
}
}

@Test
fun testResolve_parameter_nestedDependency() {
val mindy = Mindy()
mindy.register<Pair<String, Pair<Int, Boolean>>> { Pair(resolve(), resolve()) }
mindy.register<Pair<Int, Boolean>> { Pair(resolve(), resolve()) }

val a = mindy.resolve<Pair<String, Pair<Int, Boolean>>> {
register { "a" }
register { 1 }
register { true }
}
val b = mindy.resolve<Pair<String, Pair<Int, Boolean>>>() {
register { "b" }
register { 2 }
register { false }
}

assertEquals(0, mindy.transactionEntryCount)

assertEquals("a", a.first)
assertEquals(1, a.second.first)
assertEquals(true, a.second.second)

// because resolve() is singleton
assertEquals("a", b.first)
assertEquals(1, b.second.first)
assertEquals(true, b.second.second)
}

@Test
fun testCreate_simple() {
val mindy = Mindy()
Expand Down Expand Up @@ -245,6 +278,8 @@ class MindyTest {
register<IValue<String>> { StringValue("b") }
}

assertEquals(0, mindy.transactionEntryCount)

assertEquals("a", a.value.value)
assertEquals("b", b.value.value)
}
Expand All @@ -261,13 +296,46 @@ class MindyTest {
register<IValue<Int>> { Value(1) }
}

assertEquals(0, mindy.transactionEntryCount)

assertEquals("a", a.value.value)

assertThrows<IllegalStateException> {
mindy.create<IValue<IValue<Int>>>()
}
}

@Test
fun testCreate_parameter_nestedDependency() {
val mindy = Mindy()
mindy.register<Pair<String, Pair<Int, Boolean>>> { Pair(resolve(), create()) }
mindy.register<Pair<Int, Boolean>> { Pair(resolve(), resolve()) }

val a = mindy.create<Pair<String, Pair<Int, Boolean>>> {
register { "a" }
register { 1 }
register { true }
}

assertEquals(0, mindy.transactionEntryCount)

val b = mindy.create<Pair<String, Pair<Int, Boolean>>>() {
register { "b" }
register { 2 }
register { false }
}

assertEquals(0, mindy.transactionEntryCount)

assertEquals("a", a.first)
assertEquals(1, a.second.first)
assertEquals(true, a.second.second)

assertEquals("b", b.first)
assertEquals(2, b.second.first)
assertEquals(false, b.second.second)
}

@Test
fun testCreate_resolveSingleton() {
val mindy = Mindy()
Expand Down Expand Up @@ -319,6 +387,7 @@ class MindyTest {
mindy.commitTransaction()

assertEquals("", mindy.resolve<String>())
assertEquals(0, mindy.transactionEntryCount)
}

@Test
Expand All @@ -329,9 +398,7 @@ class MindyTest {
mindy.register { "" }
mindy.rollbackTransaction()

assertThrows<IllegalStateException> {
mindy.resolve<String>()
}
assertEquals(0, mindy.transactionEntryCount)
}

@Test
Expand Down

0 comments on commit 5ce9e0b

Please sign in to comment.