Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableForEachTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import static org.junit.Assert.assertEquals;
17
18 import java.util.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.testsupport.TestHelper;
26
27 public class FlowableForEachTest extends RxJavaTest {
28
29 @Test
30 public void forEachWile() {
31 final List<Object> list = new ArrayList<>();
32
33 Flowable.range(1, 5)
34 .doOnNext(new Consumer<Integer>() {
35 @Override
36 public void accept(Integer v) throws Exception {
37 list.add(v);
38 }
39 })
40 .forEachWhile(new Predicate<Integer>() {
41 @Override
42 public boolean test(Integer v) throws Exception {
43 return v < 3;
44 }
45 });
46
47 assertEquals(Arrays.asList(1, 2, 3), list);
48 }
49
50 @Test
51 public void forEachWileWithError() {
52 final List<Object> list = new ArrayList<>();
53
54 Flowable.range(1, 5).concatWith(Flowable.<Integer>error(new TestException()))
55 .doOnNext(new Consumer<Integer>() {
56 @Override
57 public void accept(Integer v) throws Exception {
58 list.add(v);
59 }
60 })
61 .forEachWhile(new Predicate<Integer>() {
62 @Override
63 public boolean test(Integer v) throws Exception {
64 return true;
65 }
66 }, new Consumer<Throwable>() {
67 @Override
68 public void accept(Throwable e) throws Exception {
69 list.add(100);
70 }
71 });
72
73 assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list);
74 }
75
76 @Test
77 public void dispose() {
78 TestHelper.checkDisposed(
79 Flowable.never()
80 .forEachWhile(v -> true)
81 );
82 }
83 }