aboutsummaryrefslogtreecommitdiff
path: root/core/src/androidTest
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-07-01 14:39:46 -0700
committerAllan Wang <me@allanwang.ca>2019-07-01 14:39:46 -0700
commit1f9198a1c05223edc7abb095f483d02cda5f1122 (patch)
tree727a7dba67bc157c064358c17baa88af4b479960 /core/src/androidTest
parent273430e0fb1b884826382349b46a917ce42ebe0b (diff)
downloadkau-1f9198a1c05223edc7abb095f483d02cda5f1122.tar.gz
kau-1f9198a1c05223edc7abb095f483d02cda5f1122.tar.bz2
kau-1f9198a1c05223edc7abb095f483d02cda5f1122.zip
Add in memory kpref variant
Diffstat (limited to 'core/src/androidTest')
-rw-r--r--core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt75
1 files changed, 42 insertions, 33 deletions
diff --git a/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt b/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt
index 29f5af1..735302c 100644
--- a/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt
+++ b/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt
@@ -24,8 +24,6 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals
-import kotlin.test.assertFalse
-import kotlin.test.assertTrue
/**
* Created by Allan Wang on 2017-08-01.
@@ -34,9 +32,10 @@ import kotlin.test.assertTrue
@MediumTest
class KPrefTest {
- lateinit var pref: TestPref
+ lateinit var androidPref: TestPref
+ lateinit var memPref: TestPref
- class TestPref : KPref() {
+ class TestPref(builder: KPrefBuilder) : KPref(builder) {
init {
initialize(ApplicationProvider.getApplicationContext<Context>(), "kpref_test_${System.currentTimeMillis()}")
@@ -57,51 +56,61 @@ class KPrefTest {
@Before
fun init() {
- pref = TestPref()
- pref.sp.edit().clear().commit()
+ androidPref = TestPref(KPrefBuilderAndroid)
+ androidPref.sp.edit().clear().commit()
+ memPref = TestPref(KPrefBuilderInMemory)
+ }
+
+ private fun pref(action: TestPref.() -> Unit) {
+ androidPref.action()
+ memPref.action()
+ }
+
+ private fun <T> assertPrefEquals(expected: T, actual: TestPref.() -> T, message: String? = null) {
+ assertEquals(expected, androidPref.actual(), "Android KPrefs: $message")
+ assertEquals(expected, memPref.actual(), "In Mem KPrefs: $message")
}
@Test
fun getDefaults() {
- assertEquals(1, pref.one)
- assertEquals(2f, pref.two)
- assertEquals(true, pref.`true`)
- assertEquals("hello", pref.hello)
- assertEquals(3, pref.set.size)
- assertTrue(pref.set.contains("po"))
- assertTrue(pref.set.contains("ta"))
- assertTrue(pref.set.contains("to"))
- assertEquals(0, pref.sp.all.size, "Defaults should not be set automatically")
+ assertPrefEquals(1, { one })
+ assertPrefEquals(2f, { two })
+ assertPrefEquals(true, { `true` })
+ assertPrefEquals("hello", { hello })
+ assertPrefEquals(3, { set.size })
+ assertPrefEquals(setOf("po", "ta", "to"), { set })
+ assertEquals(0, androidPref.sp.all.size, "Defaults should not be set automatically")
}
@Test
fun setter() {
- assertEquals(1, pref.one)
- pref.one = 2
- assertEquals(2, pref.one)
- pref.hello = "goodbye"
- assertEquals("goodbye", pref.hello)
- assertEquals(pref.hello, pref.sp.getString("hello", "hello"))
- assertEquals(2, pref.sp.all.size)
+ assertPrefEquals(1, { one })
+ pref { one = 2 }
+ assertPrefEquals(2, { one })
+ pref { hello = "goodbye" }
+ assertPrefEquals("goodbye", { hello })
+ assertEquals(androidPref.hello, androidPref.sp.getString("hello", "badfallback"))
+ assertEquals(2, androidPref.sp.all.size)
}
@SuppressLint("CommitPrefEdits")
@Test
fun reset() {
- pref.one = 2
- assertEquals(2, pref.one)
- assertEquals(6, pref.prefMap.size, "Prefmap does not have all elements")
- pref.reset() //only invalidates our lazy delegate; doesn't change the actual pref
- assertEquals(2, pref.one, "Kpref did not properly fetch from shared prefs")
- pref.sp.edit().putInt("one", -1).commit()
- assertEquals(2, pref.one, "Lazy kpref should still retain old value")
- pref.reset()
- assertEquals(-1, pref.one, "Kpref did not refetch from shared prefs upon reset")
+ pref { one = 2 }
+ assertPrefEquals(2, { one })
+ assertPrefEquals(6, { prefMap.size }, "Prefmap does not have all elements")
+ pref { reset() } //only invalidates our lazy delegate; doesn't change the actual pref
+ assertPrefEquals(2, { one }, "Kpref did not properly fetch from shared prefs")
+ // Android pref only
+ androidPref.sp.edit().putInt("one", -1).commit()
+ assertEquals(2, androidPref.one, "Lazy kpref should still retain old value")
+ androidPref.reset()
+ assertEquals(-1, androidPref.one, "Kpref did not refetch from shared prefs upon reset")
}
@Test
fun single() {
- assertTrue(pref.oneShot)
- assertFalse(pref.oneShot)
+ assertPrefEquals(true, { oneShot })
+ assertPrefEquals(false, { androidPref.oneShot })
}
}