Skip to content

Commit

Permalink
kotlin flow 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
kimdohun0104 committed May 20, 2021
1 parent 4ce2886 commit 7de859e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 98 deletions.
16 changes: 0 additions & 16 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package dohun.kim.gatabuta.gatabuta_livedata
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue

fun LiveDataTest<Boolean>.isTrue() {
assertTrue(value)
suspend fun LiveDataTest<Boolean>.isTrue() {
assertTrue(value() == true)
}

fun LiveDataTest<Boolean>.isFalse() {
assertFalse(value)
suspend fun LiveDataTest<Boolean>.isFalse() {
assertFalse(value() == true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package dohun.kim.gatabuta.gatabuta_livedata
import org.junit.Assert.assertEquals
import org.junit.Assert.fail

fun <T: CharSequence> LiveDataTest<T>.isBlank() {
if (!value.isBlank()) {
suspend fun <T: CharSequence> LiveDataTest<T>.isBlank() {
if (value()?.isNotBlank() == true) {
fail()
}
}

infix fun <T : CharSequence> LiveDataTest<T>.hasLength(expected: Int?) {
assertEquals(expected, value.length)
suspend infix fun <T : CharSequence> LiveDataTest<T>.hasLength(expected: Int?) {
assertEquals(expected, value()?.length)
}

fun <T: CharSequence> LiveDataTest<T>.isEmpty() {
if (value.isNotEmpty()) {
suspend fun <T: CharSequence> LiveDataTest<T>.isEmpty() {
if (value()?.isNotEmpty() == true) {
fail()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package dohun.kim.gatabuta.gatabuta_livedata
import org.junit.Assert
import org.junit.Assert.fail

infix fun <T : Collection<*>> LiveDataTest<T>.hasSize(expected: Int?) {
Assert.assertEquals(expected, value.size)
suspend infix fun <T : Collection<*>> LiveDataTest<T>.hasSize(expected: Int?) {
Assert.assertEquals(expected, value()?.size)
}

fun <T : Collection<*>> LiveDataTest<T>.isEmpty() {
if (!value.isEmpty()) {
suspend fun <T : Collection<*>> LiveDataTest<T>.isEmpty() {
if (value()?.isNotEmpty() == true) {
fail()
}
}

fun <T: Collection<*>> LiveDataTest<T>.isNotEmpty() {
if (value.isEmpty()) {
suspend fun <T: Collection<*>> LiveDataTest<T>.isNotEmpty() {
if (value()?.isEmpty() == true) {
fail()
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package dohun.kim.gatabuta.gatabuta_livedata

import androidx.lifecycle.LiveData
import dohun.kim.gatabuta.gatabuta_livedata.exception.NullLiveDataException
import org.junit.Assert.*
import java.util.concurrent.TimeoutException

class LiveDataTest<T>(private val liveData: LiveData<T>) {
val value: T
get() = liveData.getOrAwaitValue()
suspend fun value(): T? = liveData.getOrAwaitValue()
}

/**
Expand All @@ -23,43 +20,20 @@ infix fun <T> LiveData<T>.test(block: LiveDataTest<T>.() -> Unit) {
/**
* Assertion extensions
*/
infix fun <T> LiveDataTest<T>.equalTo(expected: Any?) {
assertEquals(expected, value)
suspend infix fun <T> LiveDataTest<T>.equalTo(expected: Any?) {
assertEquals(expected, value())
}

infix fun <T> LiveDataTest<T>.notEqualTo(expected: Any?) {
assertNotEquals(expected, value)
suspend infix fun <T> LiveDataTest<T>.notEqualTo(expected: Any?) {
assertNotEquals(expected, value())
}

fun <T> LiveDataTest<T>.hasValue() {
try {
value
} catch (e: TimeoutException) {
fail()
}
suspend fun <T> LiveDataTest<T>.isNull() {
assertNull(value())
}

fun <T> LiveDataTest<T>.hasNoValue() {
try {
value
fail()
} catch (e: TimeoutException) {
}
}

fun <T> LiveDataTest<T>.isNull() {
try {
value
} catch (e: NullLiveDataException) {
}
}

fun <T> LiveDataTest<T>.isNotNull() {
try {
value
} catch (e: NullLiveDataException) {
fail()
}
suspend fun <T> LiveDataTest<T>.isNotNull() {
assertNotNull(value())
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
package dohun.kim.gatabuta.gatabuta_livedata

import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import dohun.kim.gatabuta.gatabuta_livedata.exception.NullLiveDataException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import androidx.lifecycle.asFlow
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withTimeout

/**
* If a LiveData is transformed, the result of the transformation must be observed.
* Otherwise the transformation will never be evaluated.
*
* @link https://medium.com/androiddevelopers/unit-testing-livedata-and-other-common-observability-problems-bb477262eb04
*/
internal fun <T> LiveData<T>.getOrAwaitValue(): T {
var data: T? = null
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(t: T) {
data = t
latch.countDown()
this@getOrAwaitValue.removeObserver(this)
internal suspend fun <T> LiveData<T>.getOrAwaitValue(): T? =
try {
withTimeout(100) {
this@getOrAwaitValue.asFlow().first()
}
} catch (e: TimeoutCancellationException) {
null
}

observeForever(observer)

if (!latch.await(100, TimeUnit.MILLISECONDS)) {
throw TimeoutException("LiveData value was never set. Consider using hasNoValue() instead.")
}

if (data == null) {
throw NullLiveDataException(
"Data is null, expected non-null"
)
}

return data!!
}

0 comments on commit 7de859e

Please sign in to comment.