Skip to content

Commit 5047dbc

Browse files
committed
#7 Add more tests for for comprehension
1 parent daaf825 commit 5047dbc

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/test/scala/com/github/yruslan/channel/ChannelSuite.scala

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
899899
assert(lst == List(2, 6))
900900
}
901901

902-
"test for comprehension" in {
902+
"test for comprehension with yield" in {
903903
val ch1 = Channel.make[Int](3)
904904

905905
val ch2 = ch1
@@ -918,6 +918,56 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
918918

919919
assert(outputChannel.recv() == 6)
920920
}
921+
922+
"test for comprehension with foreach" in {
923+
val ch1 = Channel.make[Int](3)
924+
925+
val ch2 = ch1
926+
.map(v => v * 2)
927+
.filter(v => v != 4)
928+
929+
ch1.send(1)
930+
ch1.send(2)
931+
ch1.send(3)
932+
ch1.close()
933+
934+
var out = 0
935+
for {
936+
a <- ch2
937+
if a > 5
938+
} out = a
939+
940+
assert(out == 6)
941+
}
942+
943+
"test for comprehension with 2 channels" in {
944+
val ch1 = Channel.make[Int](3)
945+
val ch2 = Channel.make[Int](3)
946+
947+
val ch3 = ch1
948+
.map(v => v * 2)
949+
.filter(v => v != 4)
950+
951+
ch1.send(1)
952+
ch1.send(2)
953+
ch1.send(3)
954+
ch1.close()
955+
956+
ch2.send(100)
957+
ch2.send(200)
958+
ch2.send(300)
959+
ch2.close()
960+
961+
var out = 0
962+
for {
963+
a <- ch2
964+
b <- ch3
965+
if a > 5
966+
if b > 5
967+
} out = a + b
968+
969+
assert(out == 106)
970+
}
921971
}
922972

923973
"master/worker model" should {

0 commit comments

Comments
 (0)