Skip to content

Commit df79566

Browse files
author
Pablo Orgaz
committed
Fix Resource bugs, add missing tests
1 parent b8b0c33 commit df79566

File tree

3 files changed

+113
-23
lines changed

3 files changed

+113
-23
lines changed

build.gradle

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
ext {
3-
kotlin_version = '1.3.41'
4-
mini_version = '4.0.1'
3+
kotlin_version = "1.3.41"
4+
mini_version = "4.0.2"
55
}
66

77
repositories {
@@ -11,11 +11,10 @@ buildscript {
1111
}
1212

1313
dependencies {
14-
classpath 'com.android.tools.build:gradle:3.4.2'
14+
classpath "com.android.tools.build:gradle:3.4.2"
1515
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
16-
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
17-
classpath 'com.github.triplet.gradle:play-publisher:1.2.0'
18-
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
16+
classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.0"
17+
classpath "com.github.dcendents:android-maven-gradle-plugin:2.1"
1918
}
2019
}
2120

@@ -26,7 +25,7 @@ allprojects {
2625
repositories {
2726
jcenter()
2827
mavenCentral()
29-
maven { url 'https://jitpack.io' }
28+
maven { url "https://jitpack.io" }
3029
google()
3130
}
3231
}

mini-common/src/main/java/mini/Resource.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ open class Resource<out T> @PublishedApi internal constructor(val value: Any?) {
1414
val isFailure: Boolean get() = value is Failure
1515
val isLoading: Boolean get() = value is Loading<*>
1616

17-
internal class Empty
18-
internal class Failure(val exception: Throwable?)
19-
internal class Loading<U>(val value: U? = null)
17+
internal class Empty {
18+
override fun toString(): String = "Empty()"
19+
}
20+
21+
@PublishedApi
22+
internal data class Failure(val exception: Throwable?)
23+
24+
@PublishedApi
25+
internal data class Loading<U>(val value: U? = null)
2026

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

36-
override fun toString(): String =
37-
when (value) {
38-
is Failure -> value.toString() // "Failure($exception)"
39-
else -> "Success($value)"
40-
}
41-
4242
companion object {
4343
fun <T> success(value: T): Resource<T> = Resource(value)
4444
fun <T> failure(exception: Throwable? = null): Resource<T> = Resource(Failure(exception))
@@ -67,23 +67,23 @@ inline fun <T> Resource<T>.onSuccess(crossinline action: (data: T) -> Unit): Res
6767
return this
6868
}
6969

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

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

8080
inline fun Task.onIdle(crossinline action: () -> Unit): Task {
81-
onEmpty { action() }
81+
if (isEmpty) action()
8282
return this
8383
}
8484

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package mini
2+
3+
import org.amshove.kluent.`should be equal to`
4+
import org.amshove.kluent.`should be null`
5+
import org.amshove.kluent.`should equal`
6+
import org.amshove.kluent.`should not be null`
7+
import org.junit.Before
8+
import org.junit.Test
9+
10+
class ResourceTest {
11+
12+
private var successValue: Any? = null
13+
private var errorValue: Any? = null
14+
private var loadingValue: Any? = null
15+
private var emptyValue: Any? = null
16+
17+
@Before
18+
fun before() {
19+
successValue = null
20+
errorValue = null
21+
loadingValue = null
22+
emptyValue = null
23+
}
24+
25+
private fun <T> check(resource: Resource<T>) {
26+
var called = 0
27+
resource
28+
.onSuccess {
29+
called++
30+
successValue = it
31+
}.onFailure {
32+
called++
33+
errorValue = it
34+
}.onLoading {
35+
called++
36+
loadingValue = it
37+
}.onEmpty {
38+
called++
39+
emptyValue = true
40+
}
41+
called `should be equal to` 1
42+
}
43+
44+
@Test
45+
fun `success calls`() {
46+
check(Resource.success("abc"))
47+
successValue `should equal` "abc"
48+
}
49+
50+
@Test
51+
fun isEmpty() {
52+
check(Resource.empty<Any>())
53+
emptyValue `should equal` true
54+
}
55+
56+
@Test
57+
fun isFailure() {
58+
val ex = RuntimeException("ABC")
59+
check(Resource.failure<Any>(ex))
60+
errorValue `should equal` ex
61+
}
62+
63+
@Test
64+
fun isLoading() {
65+
check(Resource.loading<Any>("abc"))
66+
loadingValue `should equal` "abc"
67+
}
68+
69+
@Test
70+
fun getOrNull() {
71+
Resource.empty<Any>().getOrNull().`should be null`()
72+
Resource.success("abc").getOrNull().`should not be null`()
73+
}
74+
75+
@Test
76+
fun exceptionOrNull() {
77+
Resource.failure<Any>(RuntimeException()).exceptionOrNull().`should not be null`()
78+
Resource.success<Any>("abc").exceptionOrNull(). `should be null` ()
79+
}
80+
81+
@Test
82+
fun map() {
83+
Resource.success("abc")
84+
.map { 0 }
85+
.getOrNull()?.`should be equal to`(0)
86+
87+
Resource.failure<Any>()
88+
.map { 0 }
89+
.getOrNull()?.`should be null`()
90+
}
91+
}

0 commit comments

Comments
 (0)