From 474e6ab66aaa02dcd869cd3e1f3b8b8761530d78 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Thu, 17 May 2018 12:53:17 +0200 Subject: [PATCH 1/3] 2.x: Add assertValueSetOnly and assertValueSequenceOnly to TestObserver + TestSubscriber. --- .../reactivex/observers/BaseTestConsumer.java | 30 ++++ .../reactivex/observers/TestObserverTest.java | 132 ++++++++++++++- .../subscribers/TestSubscriberTest.java | 160 ++++++++++++++++-- 3 files changed, 300 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/reactivex/observers/BaseTestConsumer.java b/src/main/java/io/reactivex/observers/BaseTestConsumer.java index a1598c5732..260169d96f 100644 --- a/src/main/java/io/reactivex/observers/BaseTestConsumer.java +++ b/src/main/java/io/reactivex/observers/BaseTestConsumer.java @@ -587,6 +587,21 @@ public final U assertValueSet(Collection expected) { return (U)this; } + /** + * Assert that the TestObserver/TestSubscriber received only the specified values in any order without terminating. + * @param expected the collection of values expected in any order + * @return this; + * @since 2.1.14 + */ + @SuppressWarnings("unchecked") + @Experimental + public final U assertValueSetOnly(Collection expected) { + return assertSubscribed() + .assertValueSet(expected) + .assertNoErrors() + .assertNotComplete(); + } + /** * Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order. * @param sequence the sequence of expected values in order @@ -625,6 +640,21 @@ public final U assertValueSequence(Iterable sequence) { return (U)this; } + /** + * Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating. + * @param sequence the sequence of expected values in order + * @return this; + * @since 2.1.14 + */ + @SuppressWarnings("unchecked") + @Experimental + public final U assertValueSequenceOnly(Iterable sequence) { + return assertSubscribed() + .assertValueSequence(sequence) + .assertNoErrors() + .assertNotComplete(); + } + /** * Assert that the TestObserver/TestSubscriber terminated (i.e., the terminal latch reached zero). * @return this; diff --git a/src/test/java/io/reactivex/observers/TestObserverTest.java b/src/test/java/io/reactivex/observers/TestObserverTest.java index cca65c0b72..c183451348 100644 --- a/src/test/java/io/reactivex/observers/TestObserverTest.java +++ b/src/test/java/io/reactivex/observers/TestObserverTest.java @@ -1434,7 +1434,7 @@ public void withTag() { .assertResult(1) ; } - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.toString().contains("testing with item=2")); } @@ -1466,7 +1466,7 @@ public void assertValuesOnlyThrowsOnUnexpectedValue() { try { to.assertValuesOnly(5); - fail(); + throw new RuntimeException(); } catch (AssertionError ex) { // expected } @@ -1481,7 +1481,7 @@ public void assertValuesOnlyThrowsWhenCompleted() { try { to.assertValuesOnly(); - fail(); + throw new RuntimeException(); } catch (AssertionError ex) { // expected } @@ -1496,7 +1496,131 @@ public void assertValuesOnlyThrowsWhenErrored() { try { to.assertValuesOnly(); - fail(); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSetOnly() { + TestObserver to = TestObserver.create(); + to.onSubscribe(Disposables.empty()); + to.assertValueSetOnly(Collections.emptySet()); + + to.onNext(5); + to.assertValueSetOnly(Collections.singleton(5)); + + to.onNext(-1); + to.assertValueSetOnly(new HashSet(Arrays.asList(5, -1))); + } + + @Test + public void assertValueSetOnlyThrowsOnUnexpectedValue() { + TestObserver to = TestObserver.create(); + to.onSubscribe(Disposables.empty()); + to.assertValueSetOnly(Collections.emptySet()); + + to.onNext(5); + to.assertValueSetOnly(Collections.singleton(5)); + + to.onNext(-1); + + try { + to.assertValueSetOnly(Collections.singleton(5)); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSetOnlyThrowsWhenCompleted() { + TestObserver to = TestObserver.create(); + to.onSubscribe(Disposables.empty()); + + to.onComplete(); + + try { + to.assertValueSetOnly(Collections.emptySet()); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSetOnlyThrowsWhenErrored() { + TestObserver to = TestObserver.create(); + to.onSubscribe(Disposables.empty()); + + to.onError(new TestException()); + + try { + to.assertValueSetOnly(Collections.emptySet()); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSequenceOnly() { + TestObserver to = TestObserver.create(); + to.onSubscribe(Disposables.empty()); + to.assertValueSequenceOnly(Collections.emptyList()); + + to.onNext(5); + to.assertValueSequenceOnly(Collections.singletonList(5)); + + to.onNext(-1); + to.assertValueSequenceOnly(Arrays.asList(5, -1)); + } + + @Test + public void assertValueSequenceOnlyThrowsOnUnexpectedValue() { + TestObserver to = TestObserver.create(); + to.onSubscribe(Disposables.empty()); + to.assertValueSequenceOnly(Collections.emptyList()); + + to.onNext(5); + to.assertValueSequenceOnly(Collections.singletonList(5)); + + to.onNext(-1); + + try { + to.assertValueSequenceOnly(Collections.singletonList(5)); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSequenceOnlyThrowsWhenCompleted() { + TestObserver to = TestObserver.create(); + to.onSubscribe(Disposables.empty()); + + to.onComplete(); + + try { + to.assertValueSequenceOnly(Collections.emptyList()); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSequenceOnlyThrowsWhenErrored() { + TestObserver to = TestObserver.create(); + to.onSubscribe(Disposables.empty()); + + to.onError(new TestException()); + + try { + to.assertValueSequenceOnly(Collections.emptyList()); + throw new RuntimeException(); } catch (AssertionError ex) { // expected } diff --git a/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java b/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java index 5e4082c399..98657d5883 100644 --- a/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java +++ b/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java @@ -624,7 +624,7 @@ public void testNoTerminalEventBut1Completed() { try { ts.assertNotTerminated(); - fail("Failed to report there were terminal event(s)!"); + throw new RuntimeException("Failed to report there were terminal event(s)!"); } catch (AssertionError ex) { // expected } @@ -638,7 +638,7 @@ public void testNoTerminalEventBut1Error() { try { ts.assertNotTerminated(); - fail("Failed to report there were terminal event(s)!"); + throw new RuntimeException("Failed to report there were terminal event(s)!"); } catch (AssertionError ex) { // expected } @@ -653,7 +653,7 @@ public void testNoTerminalEventBut1Error1Completed() { try { ts.assertNotTerminated(); - fail("Failed to report there were terminal event(s)!"); + throw new RuntimeException("Failed to report there were terminal event(s)!"); } catch (AssertionError ex) { // expected } @@ -669,7 +669,7 @@ public void testNoTerminalEventBut2Errors() { try { ts.assertNotTerminated(); - fail("Failed to report there were terminal event(s)!"); + throw new RuntimeException("Failed to report there were terminal event(s)!"); } catch (AssertionError ex) { // expected Throwable e = ex.getCause(); @@ -688,7 +688,7 @@ public void testNoValues() { try { ts.assertNoValues(); - fail("Failed to report there were values!"); + throw new RuntimeException("Failed to report there were values!"); } catch (AssertionError ex) { // expected } @@ -702,7 +702,7 @@ public void testValueCount() { try { ts.assertValueCount(3); - fail("Failed to report there were values!"); + throw new RuntimeException("Failed to report there were values!"); } catch (AssertionError ex) { // expected } @@ -1790,7 +1790,7 @@ public void withTag() { .assertResult(1) ; } - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.toString().contains("testing with item=2")); } @@ -1806,7 +1806,7 @@ public void timeoutIndicated() throws InterruptedException { try { ts.assertResult(1); - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.toString().contains("timeout!")); } @@ -1820,7 +1820,7 @@ public void timeoutIndicated2() throws InterruptedException { .awaitDone(1, TimeUnit.MILLISECONDS) .assertResult(1); - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.toString().contains("timeout!")); } @@ -1835,7 +1835,7 @@ public void timeoutIndicated3() throws InterruptedException { try { ts.assertResult(1); - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.toString().contains("timeout!")); } @@ -1848,7 +1848,7 @@ public void disposeIndicated() { try { ts.assertResult(1); - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (Throwable ex) { assertTrue(ex.toString(), ex.toString().contains("disposed!")); } @@ -1930,7 +1930,7 @@ public void assertTimeout2() { .test() .awaitCount(1, TestWaitStrategy.SLEEP_1MS, 50) .assertTimeout(); - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.getMessage().contains("No timeout?!")); } @@ -1951,7 +1951,7 @@ public void assertNoTimeout2() { .test() .awaitCount(1, TestWaitStrategy.SLEEP_1MS, 50) .assertNoTimeout(); - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (AssertionError ex) { assertTrue(ex.toString(), ex.getMessage().contains("Timeout?!")); } @@ -1968,7 +1968,7 @@ public boolean test(Integer t) throws Exception { throw new IllegalArgumentException(); } }); - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (IllegalArgumentException ex) { // expected } @@ -1985,7 +1985,7 @@ public boolean test(Integer t) throws Exception { throw new IllegalArgumentException(); } }); - fail("Should have thrown!"); + throw new RuntimeException("Should have thrown!"); } catch (IllegalArgumentException ex) { // expected } @@ -2024,7 +2024,7 @@ public void assertValuesOnlyThrowsOnUnexpectedValue() { try { ts.assertValuesOnly(5); - fail(); + throw new RuntimeException(); } catch (AssertionError ex) { // expected } @@ -2039,7 +2039,7 @@ public void assertValuesOnlyThrowsWhenCompleted() { try { ts.assertValuesOnly(); - fail(); + throw new RuntimeException(); } catch (AssertionError ex) { // expected } @@ -2054,7 +2054,131 @@ public void assertValuesOnlyThrowsWhenErrored() { try { ts.assertValuesOnly(); - fail(); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSetOnly() { + TestSubscriber ts = TestSubscriber.create(); + ts.onSubscribe(new BooleanSubscription()); + ts.assertValueSetOnly(Collections.emptySet()); + + ts.onNext(5); + ts.assertValueSetOnly(Collections.singleton(5)); + + ts.onNext(-1); + ts.assertValueSetOnly(new HashSet(Arrays.asList(5, -1))); + } + + @Test + public void assertValueSetOnlyThrowsOnUnexpectedValue() { + TestSubscriber ts = TestSubscriber.create(); + ts.onSubscribe(new BooleanSubscription()); + ts.assertValueSetOnly(Collections.emptySet()); + + ts.onNext(5); + ts.assertValueSetOnly(Collections.singleton(5)); + + ts.onNext(-1); + + try { + ts.assertValueSetOnly(Collections.singleton(5)); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSetOnlyThrowsWhenCompleted() { + TestSubscriber ts = TestSubscriber.create(); + ts.onSubscribe(new BooleanSubscription()); + + ts.onComplete(); + + try { + ts.assertValueSetOnly(Collections.emptySet()); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSetOnlyThrowsWhenErrored() { + TestSubscriber ts = TestSubscriber.create(); + ts.onSubscribe(new BooleanSubscription()); + + ts.onError(new TestException()); + + try { + ts.assertValueSetOnly(Collections.emptySet()); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSequenceOnly() { + TestSubscriber ts = TestSubscriber.create(); + ts.onSubscribe(new BooleanSubscription()); + ts.assertValueSequenceOnly(Collections.emptyList()); + + ts.onNext(5); + ts.assertValueSequenceOnly(Collections.singletonList(5)); + + ts.onNext(-1); + ts.assertValueSequenceOnly(Arrays.asList(5, -1)); + } + + @Test + public void assertValueSequenceOnlyThrowsOnUnexpectedValue() { + TestSubscriber ts = TestSubscriber.create(); + ts.onSubscribe(new BooleanSubscription()); + ts.assertValueSequenceOnly(Collections.emptyList()); + + ts.onNext(5); + ts.assertValueSequenceOnly(Collections.singletonList(5)); + + ts.onNext(-1); + + try { + ts.assertValueSequenceOnly(Collections.singletonList(5)); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSequenceOnlyThrowsWhenCompleted() { + TestSubscriber ts = TestSubscriber.create(); + ts.onSubscribe(new BooleanSubscription()); + + ts.onComplete(); + + try { + ts.assertValueSequenceOnly(Collections.emptyList()); + throw new RuntimeException(); + } catch (AssertionError ex) { + // expected + } + } + + @Test + public void assertValueSequenceOnlyThrowsWhenErrored() { + TestSubscriber ts = TestSubscriber.create(); + ts.onSubscribe(new BooleanSubscription()); + + ts.onError(new TestException()); + + try { + ts.assertValueSequenceOnly(Collections.emptyList()); + throw new RuntimeException(); } catch (AssertionError ex) { // expected } From bbe97c9f4af9c135065db4a0313b24ec164442b3 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Fri, 18 May 2018 10:39:06 +0200 Subject: [PATCH 2/3] Experimental --- src/main/java/io/reactivex/observers/BaseTestConsumer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/reactivex/observers/BaseTestConsumer.java b/src/main/java/io/reactivex/observers/BaseTestConsumer.java index 260169d96f..7028bb6622 100644 --- a/src/main/java/io/reactivex/observers/BaseTestConsumer.java +++ b/src/main/java/io/reactivex/observers/BaseTestConsumer.java @@ -591,7 +591,7 @@ public final U assertValueSet(Collection expected) { * Assert that the TestObserver/TestSubscriber received only the specified values in any order without terminating. * @param expected the collection of values expected in any order * @return this; - * @since 2.1.14 + * @since 2.1.14 - Experimental */ @SuppressWarnings("unchecked") @Experimental @@ -644,7 +644,7 @@ public final U assertValueSequence(Iterable sequence) { * Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating. * @param sequence the sequence of expected values in order * @return this; - * @since 2.1.14 + * @since 2.1.14 - Experimental */ @SuppressWarnings("unchecked") @Experimental From cd99d70385116be3384835b9141337a41b94d721 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Fri, 18 May 2018 10:40:02 +0200 Subject: [PATCH 3/3] Lowercase --- src/main/java/io/reactivex/observers/BaseTestConsumer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/reactivex/observers/BaseTestConsumer.java b/src/main/java/io/reactivex/observers/BaseTestConsumer.java index 7028bb6622..84a6fe889d 100644 --- a/src/main/java/io/reactivex/observers/BaseTestConsumer.java +++ b/src/main/java/io/reactivex/observers/BaseTestConsumer.java @@ -591,7 +591,7 @@ public final U assertValueSet(Collection expected) { * Assert that the TestObserver/TestSubscriber received only the specified values in any order without terminating. * @param expected the collection of values expected in any order * @return this; - * @since 2.1.14 - Experimental + * @since 2.1.14 - experimental */ @SuppressWarnings("unchecked") @Experimental @@ -644,7 +644,7 @@ public final U assertValueSequence(Iterable sequence) { * Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating. * @param sequence the sequence of expected values in order * @return this; - * @since 2.1.14 - Experimental + * @since 2.1.14 - experimental */ @SuppressWarnings("unchecked") @Experimental