Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleTest.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.maybe;
15
16 import java.util.NoSuchElementException;
17
18 import org.junit.Test;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.TestException;
22 import io.reactivex.rxjava3.functions.Function;
23 import io.reactivex.rxjava3.testsupport.TestHelper;
24
25 public class MaybeFlatMapSingleTest extends RxJavaTest {
26 @Test
27 public void flatMapSingleValue() {
28 Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
29 @Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
30 if (integer == 1) {
31 return Single.just(2);
32 }
33
34 return Single.just(1);
35 }
36 })
37 .toSingle()
38 .test()
39 .assertResult(2);
40 }
41
42 @Test
43 public void flatMapSingleValueDifferentType() {
44 Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<String>>() {
45 @Override public SingleSource<String> apply(final Integer integer) throws Exception {
46 if (integer == 1) {
47 return Single.just("2");
48 }
49
50 return Single.just("1");
51 }
52 })
53 .toSingle()
54 .test()
55 .assertResult("2");
56 }
57
58 @Test
59 public void flatMapSingleValueNull() {
60 Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
61 @Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
62 return null;
63 }
64 })
65 .toSingle()
66 .to(TestHelper.<Integer>testConsumer())
67 .assertNoValues()
68 .assertError(NullPointerException.class)
69 .assertErrorMessage("The mapper returned a null SingleSource");
70 }
71
72 @Test
73 public void flatMapSingleValueErrorThrown() {
74 Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
75 @Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
76 throw new RuntimeException("something went terribly wrong!");
77 }
78 })
79 .toSingle()
80 .to(TestHelper.<Integer>testConsumer())
81 .assertNoValues()
82 .assertError(RuntimeException.class)
83 .assertErrorMessage("something went terribly wrong!");
84 }
85
86 @Test
87 public void flatMapSingleError() {
88 RuntimeException exception = new RuntimeException("test");
89
90 Maybe.error(exception).flatMapSingle(new Function<Object, SingleSource<Object>>() {
91 @Override public SingleSource<Object> apply(final Object integer) throws Exception {
92 return Single.just(new Object());
93 }
94 })
95 .toSingle()
96 .test()
97 .assertError(exception);
98 }
99
100 @Test
101 public void flatMapSingleEmpty() {
102 Maybe.<Integer>empty().flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
103 @Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
104 return Single.just(2);
105 }
106 })
107 .toSingle()
108 .test()
109 .assertNoValues()
110 .assertError(NoSuchElementException.class);
111 }
112
113 @Test
114 public void dispose() {
115 TestHelper.checkDisposed(Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
116 @Override
117 public SingleSource<Integer> apply(final Integer integer) throws Exception {
118 return Single.just(2);
119 }
120 }).toSingle());
121 }
122
123 @Test
124 public void doubleOnSubscribe() {
125 TestHelper.checkDoubleOnSubscribeMaybeToSingle(new Function<Maybe<Integer>, SingleSource<Integer>>() {
126 @Override
127 public SingleSource<Integer> apply(Maybe<Integer> m) throws Exception {
128 return m.flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
129 @Override
130 public SingleSource<Integer> apply(final Integer integer) throws Exception {
131 return Single.just(2);
132 }
133 }).toSingle();
134 }
135 });
136 }
137
138 @Test
139 public void singleErrors() {
140 Maybe.just(1)
141 .flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
142 @Override
143 public SingleSource<Integer> apply(final Integer integer) throws Exception {
144 return Single.error(new TestException());
145 }
146 })
147 .toSingle()
148 .test()
149 .assertFailure(TestException.class);
150 }
151 }