-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from makeevrserg/fix-tests
Fix tests
- Loading branch information
Showing
15 changed files
with
342 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ makeevrserg.java.ktarget=17 | |
# Project | ||
makeevrserg.project.name=KStorage | ||
makeevrserg.project.group=ru.astrainteractive.klibs | ||
makeevrserg.project.version.string=2.4.0 | ||
makeevrserg.project.version.string=2.4.1 | ||
makeevrserg.project.description=Kotlin wrapper for key-value storage libraries | ||
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected] | ||
makeevrserg.project.url=https://github.com/makeevrserg/klibs.kstorage/ | ||
|
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
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
102 changes: 79 additions & 23 deletions
102
kstorage/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/MutableKrateTest.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 |
---|---|---|
@@ -1,47 +1,103 @@ | ||
package ru.astrainteractive.klibs.kstorage | ||
|
||
import ru.astrainteractive.klibs.kstorage.test.StoreMutableKrate | ||
import ru.astrainteractive.klibs.kstorage.util.KrateDefaultExt.withDefault | ||
import kotlinx.coroutines.test.runTest | ||
import ru.astrainteractive.klibs.kstorage.api.impl.DefaultMutableKrate | ||
import ru.astrainteractive.klibs.kstorage.test.SampleStore | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
/** | ||
* Test cases: | ||
* 1. Factory value not null, loader is null. | ||
* 2. Factory value is null, loader not null | ||
* 3. Factory value is one, the loader is another | ||
* 4. Factory value is null,m the loader is null | ||
*/ | ||
internal class MutableKrateTest { | ||
@Test | ||
fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() { | ||
val expectValue = 10 | ||
val krate = StoreMutableKrate(factory = { expectValue }) | ||
assertEquals(expectValue, krate.cachedValue) | ||
assertEquals(expectValue, krate.loadAndGet()) | ||
fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() = runTest { | ||
val factoryValue = 10 | ||
val store = SampleStore() | ||
val krate = DefaultMutableKrate( | ||
factory = { factoryValue }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { null } | ||
) | ||
assertEquals(factoryValue, krate.cachedValue) | ||
assertEquals(factoryValue, krate.loadAndGet()) | ||
assertEquals(factoryValue, krate.cachedValue) | ||
} | ||
|
||
@Test | ||
fun GIVEN_10_as_loader_value_and_default_null_WHEN_load_THEN_return_loader() { | ||
val defaultValue = 10 | ||
val krate = StoreMutableKrate(factory = { defaultValue }) | ||
assertEquals(defaultValue, krate.cachedValue) | ||
assertEquals(defaultValue, krate.loadAndGet()) | ||
fun GIVEN_null_as_default_10_as_loader_WHEN_load_THEN_return_loader() = runTest { | ||
val loaderValue = 10 | ||
val store = SampleStore() | ||
val krate = DefaultMutableKrate( | ||
factory = { null }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { loaderValue } | ||
) | ||
assertEquals(loaderValue, krate.cachedValue) | ||
assertEquals(loaderValue, krate.loadAndGet()) | ||
assertEquals(loaderValue, krate.cachedValue) | ||
} | ||
|
||
@Test | ||
fun GIVEN_saved_and_resed_WHEN_save_and_reset_THEN_saved_and_reset() { | ||
val defaultValue = 10 | ||
val krate = StoreMutableKrate(factory = { defaultValue }) | ||
fun GIVEN_one_as_default_another_as_loader_WHEN_load_THEN_return_loader() = runTest { | ||
val loaderValue = 10 | ||
val factoryValue = 15 | ||
val store = SampleStore() | ||
val krate = DefaultMutableKrate( | ||
factory = { factoryValue }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { loaderValue } | ||
) | ||
assertEquals(loaderValue, krate.cachedValue) | ||
assertEquals(loaderValue, krate.loadAndGet()) | ||
assertEquals(loaderValue, krate.cachedValue) | ||
} | ||
|
||
@Test | ||
fun GIVEN_empty_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest { | ||
val factoryValue = 10 | ||
val store = SampleStore() | ||
val krate = DefaultMutableKrate( | ||
factory = { factoryValue }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { store.get("KEY") } | ||
) | ||
assertEquals(factoryValue, krate.cachedValue) | ||
assertEquals(factoryValue, krate.loadAndGet()) | ||
11.let { newValue -> | ||
krate.save(newValue) | ||
assertEquals(newValue, krate.cachedValue) | ||
assertEquals(newValue, krate.loadAndGet()) | ||
} | ||
krate.reset() | ||
assertEquals(defaultValue, krate.cachedValue) | ||
assertEquals(defaultValue, krate.loadAndGet()) | ||
assertEquals(factoryValue, krate.cachedValue) | ||
assertEquals(factoryValue, krate.loadAndGet()) | ||
} | ||
|
||
@Test | ||
fun GIVEN_null_then_with_default_WHEN_get_THEN_default() { | ||
val defaultValue = 10 | ||
val krate = StoreMutableKrate<Int?>(factory = { null }) | ||
.withDefault(factory = { defaultValue }) | ||
assertEquals(defaultValue, krate.cachedValue) | ||
assertEquals(defaultValue, krate.loadAndGet()) | ||
fun GIVEN_prefilled_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest { | ||
val factoryValue = 10 | ||
val defaultStoreValue = 15 | ||
val store = SampleStore(mapOf("KEY" to defaultStoreValue)) | ||
val krate = DefaultMutableKrate( | ||
factory = { factoryValue }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { store.get("KEY") } | ||
) | ||
assertEquals(defaultStoreValue, krate.cachedValue) | ||
assertEquals(defaultStoreValue, krate.loadAndGet()) | ||
11.let { newValue -> | ||
krate.save(newValue) | ||
assertEquals(newValue, krate.cachedValue) | ||
assertEquals(newValue, krate.loadAndGet()) | ||
} | ||
store.put("KEY", null) | ||
krate.reset() | ||
assertEquals(factoryValue, krate.cachedValue) | ||
assertEquals(factoryValue, krate.loadAndGet()) | ||
} | ||
} |
109 changes: 84 additions & 25 deletions
109
...age/src/commonTest/kotlin/ru/astrainteractive/klibs/kstorage/StateFlowMutableKrateTest.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 |
---|---|---|
@@ -1,44 +1,103 @@ | ||
package ru.astrainteractive.klibs.kstorage | ||
|
||
import kotlinx.coroutines.test.runTest | ||
import ru.astrainteractive.klibs.kstorage.api.impl.DefaultStateFlowMutableKrate | ||
import ru.astrainteractive.klibs.kstorage.test.SampleStore | ||
import ru.astrainteractive.klibs.kstorage.test.StoreStateFlowMutableKrate | ||
import ru.astrainteractive.klibs.kstorage.util.KrateDefaultExt.withDefault | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
/** | ||
* Test cases: | ||
* 1. Factory value not null, loader is null. | ||
* 2. Factory value is null, loader not null | ||
* 3. Factory value is one, the loader is another | ||
* 4. Factory value is null,m the loader is null | ||
*/ | ||
internal class StateFlowMutableKrateTest { | ||
|
||
@Test | ||
fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() { | ||
val expectValue = 10 | ||
val krate = StoreStateFlowMutableKrate( | ||
factory = { expectValue } | ||
fun GIVEN_10_as_default_value_and_loader_null_WHEN_load_THEN_return_default() = runTest { | ||
val factoryValue = 10 | ||
val store = SampleStore() | ||
val krate = DefaultStateFlowMutableKrate( | ||
factory = { factoryValue }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { null } | ||
) | ||
assertEquals(expectValue, krate.cachedValue) | ||
assertEquals(expectValue, krate.loadAndGet()) | ||
assertEquals(expectValue, krate.cachedStateFlow.value) | ||
assertEquals(factoryValue, krate.cachedValue) | ||
assertEquals(factoryValue, krate.loadAndGet()) | ||
assertEquals(factoryValue, krate.cachedValue) | ||
} | ||
|
||
@Test | ||
fun GIVEN_10_as_loader_value_and_default_null_WHEN_load_THEN_return_loader() { | ||
val expectValue = 10 | ||
val krate = StoreStateFlowMutableKrate<Int?>( | ||
fun GIVEN_null_as_default_10_as_loader_WHEN_load_THEN_return_loader() = runTest { | ||
val loaderValue = 10 | ||
val store = SampleStore() | ||
val krate = DefaultStateFlowMutableKrate( | ||
factory = { null }, | ||
key = "key", | ||
store = SampleStore(mapOf("key" to expectValue)) | ||
saver = { store.put("KEY", it) }, | ||
loader = { loaderValue } | ||
) | ||
assertEquals(loaderValue, krate.cachedValue) | ||
assertEquals(loaderValue, krate.loadAndGet()) | ||
assertEquals(loaderValue, krate.cachedValue) | ||
} | ||
|
||
@Test | ||
fun GIVEN_one_as_default_another_as_loader_WHEN_load_THEN_return_loader() = runTest { | ||
val loaderValue = 10 | ||
val factoryValue = 15 | ||
val store = SampleStore() | ||
val krate = DefaultStateFlowMutableKrate( | ||
factory = { factoryValue }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { loaderValue } | ||
) | ||
assertEquals(expectValue, krate.cachedValue) | ||
assertEquals(expectValue, krate.loadAndGet()) | ||
assertEquals(expectValue, krate.cachedStateFlow.value) | ||
assertEquals(loaderValue, krate.cachedValue) | ||
assertEquals(loaderValue, krate.loadAndGet()) | ||
assertEquals(loaderValue, krate.cachedValue) | ||
} | ||
|
||
@Test | ||
fun GIVEN_null_then_with_default_WHEN_get_THEN_default() { | ||
val defaultValue = 10 | ||
val krate = StoreStateFlowMutableKrate<Int?>(factory = { null }) | ||
.withDefault(factory = { defaultValue }) | ||
assertEquals(defaultValue, krate.cachedValue) | ||
assertEquals(defaultValue, krate.loadAndGet()) | ||
assertEquals(defaultValue, krate.cachedStateFlow.value) | ||
fun GIVEN_empty_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest { | ||
val factoryValue = 10 | ||
val store = SampleStore() | ||
val krate = DefaultStateFlowMutableKrate( | ||
factory = { factoryValue }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { store.get("KEY") } | ||
) | ||
assertEquals(factoryValue, krate.cachedValue) | ||
assertEquals(factoryValue, krate.loadAndGet()) | ||
11.let { newValue -> | ||
krate.save(newValue) | ||
assertEquals(newValue, krate.cachedValue) | ||
assertEquals(newValue, krate.loadAndGet()) | ||
} | ||
krate.reset() | ||
assertEquals(factoryValue, krate.cachedValue) | ||
assertEquals(factoryValue, krate.loadAndGet()) | ||
} | ||
|
||
@Test | ||
fun GIVEN_prefilled_store_WHEN_save_and_reset_THEN_saved_and_reset() = runTest { | ||
val factoryValue = 10 | ||
val defaultStoreValue = 15 | ||
val store = SampleStore(mapOf("KEY" to defaultStoreValue)) | ||
val krate = DefaultStateFlowMutableKrate( | ||
factory = { factoryValue }, | ||
saver = { store.put("KEY", it) }, | ||
loader = { store.get("KEY") } | ||
) | ||
assertEquals(defaultStoreValue, krate.cachedValue) | ||
assertEquals(defaultStoreValue, krate.loadAndGet()) | ||
11.let { newValue -> | ||
krate.save(newValue) | ||
assertEquals(newValue, krate.cachedValue) | ||
assertEquals(newValue, krate.loadAndGet()) | ||
} | ||
store.put("KEY", null) | ||
krate.reset() | ||
assertEquals(factoryValue, krate.cachedValue) | ||
assertEquals(factoryValue, krate.loadAndGet()) | ||
} | ||
} |
Oops, something went wrong.