Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/jdk8/SingleMapOptionalTest.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.jdk8;
15
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18
19 import java.util.Optional;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.Function;
26 import io.reactivex.rxjava3.testsupport.TestHelper;
27
28 public class SingleMapOptionalTest extends RxJavaTest {
29
30 @Test
31 public void successSuccess() {
32 Single.just(1)
33 .mapOptional(Optional::of)
34 .test()
35 .assertResult(1);
36 }
37
38 @Test
39 public void successEmpty() {
40 Single.just(1)
41 .mapOptional(v -> Optional.empty())
42 .test()
43 .assertResult();
44 }
45
46 @Test
47 public void error() throws Throwable {
48 @SuppressWarnings("unchecked")
49 Function<? super Integer, Optional<? extends Integer>> f = mock(Function.class);
50
51 Single.<Integer>error(new TestException())
52 .mapOptional(f)
53 .test()
54 .assertFailure(TestException.class);
55
56 verify(f, never()).apply(any());
57 }
58
59 @Test
60 public void mapperCrash() {
61 Single.just(1)
62 .mapOptional(v -> { throw new TestException(); })
63 .test()
64 .assertFailure(TestException.class);
65 }
66
67 @Test
68 public void dispose() {
69 TestHelper.checkDisposed(Single.never().mapOptional(Optional::of));
70 }
71
72 @Test
73 public void doubleOnSubscribe() {
74 TestHelper.checkDoubleOnSubscribeSingleToMaybe(m -> m.mapOptional(Optional::of));
75 }
76 }