Skip to content

Commit 4ec1ef5

Browse files
committed
add unit test for KVS
1 parent d781028 commit 4ec1ef5

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/utilities/KVS.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface KVS {
1717
fun get(key: String, defaultVal: String?): String?
1818
fun put(key: String, value: String): Boolean
1919
fun remove(key: String): Boolean
20+
fun contains(key: String): Boolean
2021
}
2122

2223
class InMemoryPrefs: KVS {
@@ -45,4 +46,6 @@ class InMemoryPrefs: KVS {
4546
return true
4647
}
4748

49+
override fun contains(key: String) = cache.containsKey(key)
50+
4851
}

core/src/main/java/com/segment/analytics/kotlin/core/utilities/PropertiesFile.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,6 @@ class PropertiesFile(val file: File) : KVS {
7272
save()
7373
return true
7474
}
75+
76+
override fun contains(key: String) = properties.containsKey(key)
7577
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.segment.analytics.kotlin.core.utilities
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.api.Nested
6+
import org.junit.jupiter.api.Test
7+
8+
class KVSTest {
9+
@Nested
10+
inner class InMemoryPrefsTest {
11+
private lateinit var prefs: KVS
12+
13+
@BeforeEach
14+
fun setup(){
15+
prefs = InMemoryPrefs()
16+
prefs.put("int", 1)
17+
prefs.put("string", "string")
18+
}
19+
20+
@Test
21+
fun getTest() {
22+
val keyNotExists = "keyNotExists"
23+
val expectedInt = 100
24+
val expectedString = "string"
25+
26+
assertEquals(1, prefs.get("int", 0))
27+
assertEquals("string", prefs.get("string", null))
28+
assertEquals(0, prefs.get("keyNotExists", 0))
29+
assertEquals(null, prefs.get("keyNotExists", null))
30+
}
31+
32+
@Test
33+
fun putTest() {
34+
prefs.put("int", 2)
35+
prefs.put("string", "stringstring")
36+
37+
assertEquals(2, prefs.get("int", 0))
38+
assertEquals("stringstring", prefs.get("string", null))
39+
}
40+
41+
@Test
42+
fun containsAndRemoveTest() {
43+
assertTrue(prefs.contains("int"))
44+
prefs.remove("int")
45+
assertFalse(prefs.contains("int"))
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)