| 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.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.NoSuchElementException; |
| 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.observers.TestObserver; |
| 25 |
import io.reactivex.rxjava3.subjects.MaybeSubject; |
| 26 |
|
| 27 |
public class SingleFromMaybeTest extends RxJavaTest { |
| 28 |
|
| 29 |
@Test |
| 30 |
public void success() { |
| 31 |
Single.fromMaybe(Maybe.just(1).hide()) |
| 32 |
.test() |
| 33 |
.assertResult(1); |
| 34 |
} |
| 35 |
|
| 36 |
@Test |
| 37 |
public void empty() { |
| 38 |
Single.fromMaybe(Maybe.empty().hide()) |
| 39 |
.test() |
| 40 |
.assertFailure(NoSuchElementException.class); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void emptyDefault() { |
| 45 |
Single.fromMaybe(Maybe.empty().hide(), 1) |
| 46 |
.test() |
| 47 |
.assertResult(1); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void error() { |
| 52 |
Single.fromMaybe(Maybe.error(new TestException()).hide()) |
| 53 |
.test() |
| 54 |
.assertFailure(TestException.class); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void cancelComposes() { |
| 59 |
MaybeSubject<Integer> ms = MaybeSubject.create(); |
| 60 |
|
| 61 |
TestObserver<Integer> to = Single.fromMaybe(ms) |
| 62 |
.test(); |
| 63 |
|
| 64 |
to.assertEmpty(); |
| 65 |
|
| 66 |
assertTrue(ms.hasObservers()); |
| 67 |
|
| 68 |
to.dispose(); |
| 69 |
|
| 70 |
assertFalse(ms.hasObservers()); |
| 71 |
} |
| 72 |
} |