Skip to content

Commit

Permalink
Make simple like other watchers; increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
GladeDiviney committed Apr 27, 2019
1 parent ae3798a commit be34472
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ You may not really care about the details of a change, or just want to respond t

```kotlin
val map = watchableMapOf(1 to "2")
simple(map) { println("at $key remove $remove add $add") }.start()
simple(map) {
with(it) {
println("at $key remove $remove add $add")
}
}.start()
map.put(1, "3")

// Prints:
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/gladed/watchable/operations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun <M, C : Change> CoroutineScope.bind(
*/
fun <S, C : HasSimpleChange<S>> CoroutineScope.simple(
watchable: SimpleWatchable<S, C>,
func: suspend S.() -> Unit
func: suspend (S) -> Unit
) = watchable.simple(this@simple, func)

/**
Expand Down
4 changes: 1 addition & 3 deletions src/test/kotlin/ReadmeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ import io.gladed.watchable.watchableListOf
import io.gladed.watchable.watchableMapOf
import io.gladed.watchable.watchableSetOf
import io.gladed.watchable.watchableValueOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestCoroutineScope
import org.junit.Assert.assertEquals
import org.junit.Test
import java.net.URI
Expand Down Expand Up @@ -140,7 +138,7 @@ class ReadmeTest {

@Test fun `Simple Watches`() = runTest {
val map = watchableMapOf(1 to "2")
simple(map) { println("at $key remove $remove add $add") }.start()
simple(map) { with(it) { println("at $key remove $remove add $add") } }.start()
map.put(1, "3")

outputIs("""
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/SimpleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SimpleTest {
@Test fun `watch list simply`() = runBlocking {
val list = watchableListOf(1, 2)
val channel = Channel<ListChange.Simple<Int>>(Channel.UNLIMITED)
simple(list) { channel.send(this) }
simple(list) { channel.send(it) }

channel.mustBe(
ListChange.Simple(index = 0, add = 1),
Expand All @@ -45,7 +45,7 @@ class SimpleTest {
@Test fun `watch map simply`() = runBlocking {
val map = watchableMapOf(1 to "1")
val channel = Channel<MapChange.Simple<Int, String>>(Channel.UNLIMITED)
simple(map) { channel.send(this) }
simple(map) { channel.send(it) }
channel.mustBe(MapChange.Simple(key = 1, add = "1"))
map { put(2, "2"); remove(1); put(2, "22") }
channel.mustBe(
Expand All @@ -57,7 +57,7 @@ class SimpleTest {
@Test fun `watch set simply`() = runBlocking {
val set = watchableSetOf(1, 2)
val channel = Channel<SetChange.Simple<Int>>(Channel.UNLIMITED)
simple(set) { channel.send(this) }
simple(set) { channel.send(it) }
channel.mustBe(SetChange.Simple(add = 1), SetChange.Simple(add = 2))
set { remove(1); add(3) }
channel.mustBe(
Expand Down
78 changes: 78 additions & 0 deletions src/test/kotlin/store/HoldTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* (c) Copyright 2019 Glade Diviney.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package store

import io.gladed.watchable.Watcher
import io.gladed.watchable.store.Hold
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import org.junit.Test
import runTest

class HoldTest {
private val watcher = mockk<Watcher>(relaxUnitFun = true)
private val mockHold = mockk<Hold>(relaxUnitFun = true)
private val hold = Hold(onCancel = { mockHold.cancel() },
onRemove = { mockHold.remove() },
onStart = { mockHold.start() },
onStop = { mockHold.stop() })

@Test fun `plus combines cancel`() = runTest {
(hold + hold).cancel()
verify(exactly = 2) { mockHold.cancel() }
}

@Test fun `plus combines remove`() = runTest {
(hold + hold).remove()
coVerify(exactly = 2) { mockHold.remove() }
}

@Test fun `plus combines start`() = runTest {
(hold + hold).start()
coVerify(exactly = 2) { mockHold.start() }
}

@Test fun `plus combines stop`() = runTest {
(hold + hold).stop()
coVerify(exactly = 2) { mockHold.stop() }
}

@Test fun `plus watcher combines cancel`() = runTest {
(hold + watcher).cancel()
verify(exactly = 1) { mockHold.cancel() }
verify(exactly = 1) { watcher.cancel() }
}

@Test fun `plus watcher allows remove`() = runTest {
(hold + watcher).remove()
coVerify(exactly = 1) { mockHold.remove() }
// Watcher has no remove
}

@Test fun `plus watcher combines start`() = runTest {
(hold + watcher).start()
coVerify(exactly = 1) { mockHold.start() }
coVerify(exactly = 1) { watcher.start() }
}

@Test fun `plus watcher combines stop`() = runTest {
(hold + watcher).stop()
coVerify(exactly = 1) { mockHold.stop() }
coVerify(exactly = 1) { watcher.stop() }
}
}
9 changes: 9 additions & 0 deletions src/test/kotlin/store/HoldingStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import io.gladed.watchable.store.Hold
import io.gladed.watchable.store.HoldingStore
import io.gladed.watchable.store.Store
import io.gladed.watchable.store.holding
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.toList
import runTest
import java.util.UUID

Expand Down Expand Up @@ -217,4 +220,10 @@ class HoldingStoreTest {
// Released even though scope still active
coVerify { hold.stop() }
}

@UseExperimental(FlowPreview::class)
@Test fun `keys goes to back`() = test {
coEvery { rootStore.keys() } returns listOf(robin.id).asFlow()
assertEquals(listOf(robin.id), scopeStore.create(this).keys().toList())
}
}
37 changes: 37 additions & 0 deletions src/test/kotlin/store/InflaterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* (c) Copyright 2019 Glade Diviney.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package store

import io.gladed.watchable.store.Inflater
import org.junit.Assert.assertEquals
import org.junit.Test

class InflaterTest {
private val stringToInt = object : Inflater<String, Int> {
override fun inflate(value: String): Int = value.toInt()
override fun deflate(value: Int): String = value.toString()
}

private val intToString = object : Inflater<Int, String> {
override fun inflate(value: Int): String = value.toString()
override fun deflate(value: String): Int = value.toInt()
}

@Test fun cycle() {
assertEquals(5, (intToString + stringToInt).inflate(5))
}
}

0 comments on commit be34472

Please sign in to comment.