Skip to content

Commit f126d73

Browse files
committed
null edge case tests for Monix extensions
1 parent 839c89d commit f126d73

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

core/src/main/scala/com/avsystem/commons/concurrent/ObservableExtensions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ object ObservableExtensions extends ObservableExtensions {
2323
def headOptL: Task[Opt[T]] = obs.headOptionL.map(_.toOpt)
2424

2525
/**
26-
* Returns a [[monix.eval.Task Task]] which emits the first item for which the predicate holds.
26+
* Returns a [[monix.eval.Task Task]] which emits the first <b>non-null</b> item for which the predicate holds.
2727
*/
28-
def findOptL(p: T => Boolean): Task[Opt[T]] = obs.findL(p).map(_.toOpt)
28+
def findOptL(p: T => Boolean): Task[Opt[T]] = obs.findL(e => e != null && p(e)).map(_.toOpt)
2929

3030
/** Suppress the duplicate elements emitted by the source Observable.
3131
*

core/src/main/scala/com/avsystem/commons/concurrent/TaskExtensions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait TaskExtensions {
1818
object TaskExtensions extends TaskExtensions {
1919
final class TaskOps[T](private val task: Task[T]) extends AnyVal {
2020
/**
21-
* Like regular `timeout` but [[TimeoutException]] is created lazily (for performance).
21+
* Similar to [[Task.timeoutWith]] but exception instance is created lazily (for performance)
2222
*/
2323
def lazyTimeout(after: FiniteDuration, msg: => String): Task[T] =
2424
task.timeoutTo(after, Task.raiseError(new TimeoutException(msg)))

core/src/test/scala/com/avsystem/commons/concurrent/ObservableExtensionsTest.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ class ObservableExtensionsTest extends AnyFunSuite with Matchers
2121
}
2222
}
2323

24+
test("headOptL - null handling") {
25+
Observable.fromIterable(Seq(null, "abc", "xyz")) .headOptL.runToFuture.futureValue shouldBe Opt.Empty
26+
}
27+
2428
test("findOptL") {
2529
forAll { ints: List[Int] =>
2630
Observable.fromIterable(ints).findOptL(_ > 1).runToFuture.futureValue shouldBe ints.findOpt(_ > 1)
2731
}
2832
}
2933

34+
test("findOptL - null handling") {
35+
Observable.fromIterable(Seq(null, "abc", "xyz")).findOptL(_ => true).runToFuture.futureValue shouldBe Opt.some("abc")
36+
Observable.fromIterable(Seq(null, null)).findOptL(_ => true).runToFuture.futureValue shouldBe Opt.Empty
37+
Observable.fromIterable(Seq(null, "abc", "xyz")).findOptL(_.startsWith("x")).runToFuture.futureValue shouldBe Opt.some("xyz")
38+
}
39+
3040
test("distinct") {
3141
forAll { ints: List[Int] =>
3242
Observable.fromIterable(ints).distinct.toListL.runToFuture.futureValue shouldBe ints.distinct

0 commit comments

Comments
 (0)