From df79566ed1c87fd5e79a68e8ca983cfeeaea630c Mon Sep 17 00:00:00 2001 From: Pablo Orgaz Date: Thu, 8 Aug 2019 16:21:56 +0200 Subject: [PATCH] Fix Resource bugs, add missing tests --- build.gradle | 13 ++- mini-common/src/main/java/mini/Resource.kt | 32 +++---- .../src/test/kotlin/mini/ResourceTest.kt | 91 +++++++++++++++++++ 3 files changed, 113 insertions(+), 23 deletions(-) create mode 100644 mini-common/src/test/kotlin/mini/ResourceTest.kt diff --git a/build.gradle b/build.gradle index 407bf9b..4e44c2f 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { @@ -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" } } @@ -26,7 +25,7 @@ allprojects { repositories { jcenter() mavenCentral() - maven { url 'https://jitpack.io' } + maven { url "https://jitpack.io" } google() } } diff --git a/mini-common/src/main/java/mini/Resource.kt b/mini-common/src/main/java/mini/Resource.kt index 4a7bd0c..5c6044e 100644 --- a/mini-common/src/main/java/mini/Resource.kt +++ b/mini-common/src/main/java/mini/Resource.kt @@ -14,9 +14,15 @@ open class Resource @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(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(val value: U? = null) /** * Get the current value if successful, or null for other cases. @@ -33,12 +39,6 @@ open class Resource @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 success(value: T): Resource = Resource(value) fun failure(exception: Throwable? = null): Resource = Resource(Failure(exception)) @@ -67,23 +67,23 @@ inline fun Resource.onSuccess(crossinline action: (data: T) -> Unit): Res return this } -inline fun Resource.onFailure(crossinline action: (data: T) -> Unit): Resource { - if (isFailure) action(value as T) +inline fun Resource.onFailure(crossinline action: (error: Throwable?) -> Unit): Resource { + if (isFailure) action((value as Resource.Failure).exception) return this } -inline fun Resource.onLoading(crossinline action: (data: T) -> Unit): Resource { - if (isLoading) action(value as T) +inline fun Resource.onLoading(crossinline action: (data: T?) -> Unit): Resource { + if (isLoading) action((value as Resource.Loading).value) return this } inline fun Task.onIdle(crossinline action: () -> Unit): Task { - onEmpty { action() } + if (isEmpty) action() return this } -inline fun Resource.onEmpty(crossinline action: (data: T) -> Unit): Resource { - if (isEmpty) action(value as T) +inline fun Resource.onEmpty(crossinline action: () -> Unit): Resource { + if (isEmpty) action() return this } diff --git a/mini-common/src/test/kotlin/mini/ResourceTest.kt b/mini-common/src/test/kotlin/mini/ResourceTest.kt new file mode 100644 index 0000000..a8acd0e --- /dev/null +++ b/mini-common/src/test/kotlin/mini/ResourceTest.kt @@ -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 check(resource: Resource) { + 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()) + emptyValue `should equal` true + } + + @Test + fun isFailure() { + val ex = RuntimeException("ABC") + check(Resource.failure(ex)) + errorValue `should equal` ex + } + + @Test + fun isLoading() { + check(Resource.loading("abc")) + loadingValue `should equal` "abc" + } + + @Test + fun getOrNull() { + Resource.empty().getOrNull().`should be null`() + Resource.success("abc").getOrNull().`should not be null`() + } + + @Test + fun exceptionOrNull() { + Resource.failure(RuntimeException()).exceptionOrNull().`should not be null`() + Resource.success("abc").exceptionOrNull(). `should be null` () + } + + @Test + fun map() { + Resource.success("abc") + .map { 0 } + .getOrNull()?.`should be equal to`(0) + + Resource.failure() + .map { 0 } + .getOrNull()?.`should be null`() + } +} \ No newline at end of file