Skip to content

Commit c12a992

Browse files
committed
fix(modelql): assertNotEmpty should throw an IllegalArgumentException
1 parent 4e991cb commit c12a992

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

modelql-core/src/commonTest/kotlin/org/modelix/modelql/core/ModelQLTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import kotlinx.serialization.modules.subclass
2929
import kotlin.test.Ignore
3030
import kotlin.test.Test
3131
import kotlin.test.assertEquals
32+
import kotlin.test.assertFailsWith
3233
import kotlin.test.assertTrue
3334
import kotlin.time.Duration.Companion.seconds
3435

@@ -367,6 +368,15 @@ class ModelQLTest {
367368
assertEquals(listOf("iPhone 9", "iPhone X", "Samsung Universe 9", "OPPOF19", "Huawei P30"), result)
368369
}
369370

371+
@Test
372+
fun assertNotEmpty_throws_IllegalArgumentException() = runTestWithTimeout {
373+
assertFailsWith(IllegalArgumentException::class) {
374+
remoteProductDatabaseQuery<List<String>> { db ->
375+
db.products.filter { false.asMono() }.assertNotEmpty().map { it.title }.toList()
376+
}
377+
}
378+
}
379+
370380
data class MyNonSerializableClass(val id: Int, val title: String, val images: List<MyImage>)
371381
data class MyImage(val url: String)
372382
}

streams/src/commonMain/kotlin/org/modelix/streams/StreamExtensions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ fun <T> Observable<T>.withIndex(): Observable<IndexedValue<T>> {
6767
return map { IndexedValue(index++, it) }
6868
}
6969
fun <T> Observable<T>.assertNotEmpty(message: () -> String): Observable<T> {
70-
return this.switchIfEmpty { throw RuntimeException("Empty stream: " + message()) }
70+
return this.switchIfEmpty { throw IllegalArgumentException("At least one element was expected. " + message()) }
7171
}
7272
fun <T> Maybe<T>.assertEmpty(message: (T) -> String): Completable {
73-
return map { throw RuntimeException(message(it)) }.asCompletable()
73+
return map { throw IllegalArgumentException(message(it)) }.asCompletable()
7474
}
7575
fun <T> Observable<T>.assertEmpty(message: (T) -> String): Completable {
76-
return map { throw RuntimeException(message(it)) }.asCompletable()
76+
return map { throw IllegalArgumentException(message(it)) }.asCompletable()
7777
}
7878
fun <T> Single<T>.cached(): Single<T> {
7979
return this.asObservable().replay(1).autoConnect()
80-
.firstOrError { RuntimeException("Single was empty. Should not happen.") }
80+
.firstOrError { IllegalStateException("Single was empty. Should not happen.") }
8181
}
8282

8383
fun <T> Maybe<T>.orNull(): Single<T?> = defaultIfEmpty(null)

0 commit comments

Comments
 (0)