Skip to content

Commit

Permalink
Fix Resource bugs, add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Orgaz committed Aug 8, 2019
1 parent b8b0c33 commit df79566
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 23 deletions.
13 changes: 6 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
kotlin_version = '1.3.41'
mini_version = '4.0.1'
kotlin_version = "1.3.41"
mini_version = "4.0.2"
}

repositories {
Expand All @@ -11,11 +11,10 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "com.android.tools.build:gradle:3.4.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
classpath 'com.github.triplet.gradle:play-publisher:1.2.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.0"
classpath "com.github.dcendents:android-maven-gradle-plugin:2.1"
}
}

Expand All @@ -26,7 +25,7 @@ allprojects {
repositories {
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
maven { url "https://jitpack.io" }
google()
}
}
Expand Down
32 changes: 16 additions & 16 deletions mini-common/src/main/java/mini/Resource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ open class Resource<out T> @PublishedApi internal constructor(val value: Any?) {
val isFailure: Boolean get() = value is Failure
val isLoading: Boolean get() = value is Loading<*>

internal class Empty
internal class Failure(val exception: Throwable?)
internal class Loading<U>(val value: U? = null)
internal class Empty {
override fun toString(): String = "Empty()"
}

@PublishedApi
internal data class Failure(val exception: Throwable?)

@PublishedApi
internal data class Loading<U>(val value: U? = null)

/**
* Get the current value if successful, or null for other cases.
Expand All @@ -33,12 +39,6 @@ open class Resource<out T> @PublishedApi internal constructor(val value: Any?) {
else -> null
}

override fun toString(): String =
when (value) {
is Failure -> value.toString() // "Failure($exception)"
else -> "Success($value)"
}

companion object {
fun <T> success(value: T): Resource<T> = Resource(value)
fun <T> failure(exception: Throwable? = null): Resource<T> = Resource(Failure(exception))
Expand Down Expand Up @@ -67,23 +67,23 @@ inline fun <T> Resource<T>.onSuccess(crossinline action: (data: T) -> Unit): Res
return this
}

inline fun <T> Resource<T>.onFailure(crossinline action: (data: T) -> Unit): Resource<T> {
if (isFailure) action(value as T)
inline fun <T> Resource<T>.onFailure(crossinline action: (error: Throwable?) -> Unit): Resource<T> {
if (isFailure) action((value as Resource.Failure).exception)
return this
}

inline fun <T> Resource<T>.onLoading(crossinline action: (data: T) -> Unit): Resource<T> {
if (isLoading) action(value as T)
inline fun <T> Resource<T>.onLoading(crossinline action: (data: T?) -> Unit): Resource<T> {
if (isLoading) action((value as Resource.Loading<T>).value)
return this
}

inline fun Task.onIdle(crossinline action: () -> Unit): Task {
onEmpty { action() }
if (isEmpty) action()
return this
}

inline fun <T> Resource<T>.onEmpty(crossinline action: (data: T) -> Unit): Resource<T> {
if (isEmpty) action(value as T)
inline fun <T> Resource<T>.onEmpty(crossinline action: () -> Unit): Resource<T> {
if (isEmpty) action()
return this
}

Expand Down
91 changes: 91 additions & 0 deletions mini-common/src/test/kotlin/mini/ResourceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package mini

import org.amshove.kluent.`should be equal to`
import org.amshove.kluent.`should be null`
import org.amshove.kluent.`should equal`
import org.amshove.kluent.`should not be null`
import org.junit.Before
import org.junit.Test

class ResourceTest {

private var successValue: Any? = null
private var errorValue: Any? = null
private var loadingValue: Any? = null
private var emptyValue: Any? = null

@Before
fun before() {
successValue = null
errorValue = null
loadingValue = null
emptyValue = null
}

private fun <T> check(resource: Resource<T>) {
var called = 0
resource
.onSuccess {
called++
successValue = it
}.onFailure {
called++
errorValue = it
}.onLoading {
called++
loadingValue = it
}.onEmpty {
called++
emptyValue = true
}
called `should be equal to` 1
}

@Test
fun `success calls`() {
check(Resource.success("abc"))
successValue `should equal` "abc"
}

@Test
fun isEmpty() {
check(Resource.empty<Any>())
emptyValue `should equal` true
}

@Test
fun isFailure() {
val ex = RuntimeException("ABC")
check(Resource.failure<Any>(ex))
errorValue `should equal` ex
}

@Test
fun isLoading() {
check(Resource.loading<Any>("abc"))
loadingValue `should equal` "abc"
}

@Test
fun getOrNull() {
Resource.empty<Any>().getOrNull().`should be null`()
Resource.success("abc").getOrNull().`should not be null`()
}

@Test
fun exceptionOrNull() {
Resource.failure<Any>(RuntimeException()).exceptionOrNull().`should not be null`()
Resource.success<Any>("abc").exceptionOrNull(). `should be null` ()
}

@Test
fun map() {
Resource.success("abc")
.map { 0 }
.getOrNull()?.`should be equal to`(0)

Resource.failure<Any>()
.map { 0 }
.getOrNull()?.`should be null`()
}
}

0 comments on commit df79566

Please sign in to comment.