-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from softwaremill/source-interleave-all
Add Source.interleaveAll combinator
- Loading branch information
Showing
3 changed files
with
162 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsEmptyTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Source.empty" | ||
|
||
it should "be done" in scoped { | ||
Source.empty.isDone shouldBe true | ||
} | ||
|
||
it should "be empty" in scoped { | ||
Source.empty.toList shouldBe empty | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
core/src/test/scala/ox/channels/SourceOpsInterleaveAllTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ox.channels | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsInterleaveAllTest extends AnyFlatSpec with Matchers { | ||
|
||
behavior of "Source.interleaveAll" | ||
|
||
it should "interleave no sources" in scoped { | ||
val s = Source.interleaveAll(List.empty) | ||
|
||
s.toList shouldBe empty | ||
} | ||
|
||
it should "interleave a single source" in scoped { | ||
val c = Source.fromValues(1, 2, 3) | ||
|
||
val s = Source.interleaveAll(List(c)) | ||
|
||
s.toList shouldBe List(1, 2, 3) | ||
} | ||
|
||
it should "interleave multiple sources" in scoped { | ||
val c1 = Source.fromValues(1, 2, 3, 4, 5, 6, 7, 8) | ||
val c2 = Source.fromValues(10, 20, 30) | ||
val c3 = Source.fromValues(100, 200, 300, 400, 500) | ||
|
||
val s = Source.interleaveAll(List(c1, c2, c3)) | ||
|
||
s.toList shouldBe List(1, 10, 100, 2, 20, 200, 3, 30, 300, 4, 400, 5, 500, 6, 7, 8) | ||
} | ||
|
||
it should "interleave multiple sources using custom segment size" in scoped { | ||
val c1 = Source.fromValues(1, 2, 3, 4, 5, 6, 7, 8) | ||
val c2 = Source.fromValues(10, 20, 30) | ||
val c3 = Source.fromValues(100, 200, 300, 400, 500) | ||
|
||
val s = Source.interleaveAll(List(c1, c2, c3), segmentSize = 2) | ||
|
||
s.toList shouldBe List(1, 2, 10, 20, 100, 200, 3, 4, 30, 300, 400, 5, 6, 500, 7, 8) | ||
} | ||
|
||
it should "interleave multiple sources using custom segment size and complete eagerly" in scoped { | ||
val c1 = Source.fromValues(1, 2, 3, 4, 5, 6, 7, 8) | ||
val c2 = Source.fromValues(10, 20, 30) | ||
val c3 = Source.fromValues(100, 200, 300, 400, 500) | ||
|
||
val s = Source.interleaveAll(List(c1, c2, c3), segmentSize = 2, eagerComplete = true) | ||
|
||
s.toList shouldBe List(1, 2, 10, 20, 100, 200, 3, 4, 30) | ||
} | ||
} |