From 23ef07a2c7855092b7cc4e9ca17d2a581dfe2717 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Mon, 26 Sep 2016 14:35:22 +0200 Subject: [PATCH 1/2] 2.x: TestSubscriber & TestObserver add assertValue(Predicate) --- .../io/reactivex/observers/TestObserver.java | 37 +++++++++++ .../reactivex/subscribers/TestSubscriber.java | 37 +++++++++++ .../reactivex/observers/TestObserverTest.java | 62 ++++++++++++++++++- .../subscribers/TestSubscriberTest.java | 59 +++++++++++++++++- 4 files changed, 192 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/reactivex/observers/TestObserver.java b/src/main/java/io/reactivex/observers/TestObserver.java index 1f615dca3d..47e5a18cd0 100644 --- a/src/main/java/io/reactivex/observers/TestObserver.java +++ b/src/main/java/io/reactivex/observers/TestObserver.java @@ -533,6 +533,43 @@ public final TestObserver assertValue(T value) { return this; } + /** + * Asserts that this TestObserver received exactly one onNext value for which + * the provided predicate returns true. + * @param valuePredicate + * the predicate that receives the onNext value + * and should return true for the expected value. + * @return this + */ + public final TestObserver assertValue(Predicate valuePredicate) { + int s = values.size(); + if (s == 0) { + throw fail("No values"); + } + + boolean found = false; + + for (T value : values) { + try { + if (valuePredicate.test(value)) { + found = true; + break; + } + } catch (Exception ex) { + throw ExceptionHelper.wrapOrThrow(ex); + } + } + + if (found) { + if (s != 1) { + throw fail("Value present but other values as well"); + } + } else { + throw fail("Value not present"); + } + return this; + } + /** Appends the class name to a non-null value. */ static String valueAndClass(Object o) { if (o != null) { diff --git a/src/main/java/io/reactivex/subscribers/TestSubscriber.java b/src/main/java/io/reactivex/subscribers/TestSubscriber.java index 670e2d9ad7..063d27799b 100644 --- a/src/main/java/io/reactivex/subscribers/TestSubscriber.java +++ b/src/main/java/io/reactivex/subscribers/TestSubscriber.java @@ -577,6 +577,43 @@ public final TestSubscriber assertValue(T value) { return this; } + /** + * Asserts that this TestSubscriber received exactly one onNext value for which + * the provided predicate returns true. + * @param valuePredicate + * the predicate that receives the onNext value + * and should return true for the expected value. + * @return this + */ + public final TestSubscriber assertValue(Predicate valuePredicate) { + int s = values.size(); + if (s == 0) { + throw fail("No values"); + } + + boolean found = false; + + for (T value : values) { + try { + if (valuePredicate.test(value)) { + found = true; + break; + } + } catch (Exception ex) { + throw ExceptionHelper.wrapOrThrow(ex); + } + } + + if (found) { + if (s != 1) { + throw fail("Value present but other values as well"); + } + } else { + throw fail("Value not present"); + } + return this; + } + /** Appends the class name to a non-null value. */ static String valueAndClass(Object o) { if (o != null) { diff --git a/src/test/java/io/reactivex/observers/TestObserverTest.java b/src/test/java/io/reactivex/observers/TestObserverTest.java index 5f32528cff..6e72632c4c 100644 --- a/src/test/java/io/reactivex/observers/TestObserverTest.java +++ b/src/test/java/io/reactivex/observers/TestObserverTest.java @@ -178,7 +178,7 @@ public void testNullExpected() { to.onNext(1); try { - to.assertValue(null); + to.assertValue((Integer) null); } catch (AssertionError ex) { // this is expected return; @@ -1205,4 +1205,62 @@ public void asyncFusion() { .assertFusionMode(QueueDisposable.ASYNC) .assertResult(1); } -} \ No newline at end of file + + @Test + public void assertValuePredicateEmpty() { + TestObserver ts = new TestObserver(); + + Observable.empty().subscribe(ts); + + thrown.expect(AssertionError.class); + thrown.expectMessage("No values"); + ts.assertValue(new Predicate() { + @Override public boolean test(final Object o) throws Exception { + return false; + } + }); + } + + @Test + public void assertValuePredicateMatch() { + TestObserver ts = new TestObserver(); + + Observable.just(1).subscribe(ts); + + ts.assertValue(new Predicate() { + @Override public boolean test(final Integer o) throws Exception { + return o == 1; + } + }); + } + + @Test + public void assertValuePredicateNoMatch() { + TestObserver ts = new TestObserver(); + + Observable.just(1).subscribe(ts); + + thrown.expect(AssertionError.class); + thrown.expectMessage("Value not present"); + ts.assertValue(new Predicate() { + @Override public boolean test(final Integer o) throws Exception { + return o != 1; + } + }); + } + + @Test + public void assertValuePredicateMatchButMore() { + TestObserver ts = new TestObserver(); + + Observable.just(1, 2).subscribe(ts); + + thrown.expect(AssertionError.class); + thrown.expectMessage("Value present but other values as well"); + ts.assertValue(new Predicate() { + @Override public boolean test(final Integer o) throws Exception { + return o == 1; + } + }); + } +} diff --git a/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java b/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java index 75591fd95c..4878247b99 100644 --- a/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java +++ b/src/test/java/io/reactivex/subscribers/TestSubscriberTest.java @@ -1592,4 +1592,61 @@ public void asyncQueueThrows() { .assertFailure(TestException.class); } -} \ No newline at end of file + @Test + public void assertValuePredicateEmpty() { + TestSubscriber ts = new TestSubscriber(); + + Flowable.empty().subscribe(ts); + + thrown.expect(AssertionError.class); + thrown.expectMessage("No values"); + ts.assertValue(new Predicate() { + @Override public boolean test(final Object o) throws Exception { + return false; + } + }); + } + + @Test + public void assertValuePredicateMatch() { + TestSubscriber ts = new TestSubscriber(); + + Flowable.just(1).subscribe(ts); + + ts.assertValue(new Predicate() { + @Override public boolean test(final Integer o) throws Exception { + return o == 1; + } + }); + } + + @Test + public void assertValuePredicateNoMatch() { + TestSubscriber ts = new TestSubscriber(); + + Flowable.just(1).subscribe(ts); + + thrown.expect(AssertionError.class); + thrown.expectMessage("Value not present"); + ts.assertValue(new Predicate() { + @Override public boolean test(final Integer o) throws Exception { + return o != 1; + } + }); + } + + @Test + public void assertValuePredicateMatchButMore() { + TestSubscriber ts = new TestSubscriber(); + + Flowable.just(1, 2).subscribe(ts); + + thrown.expect(AssertionError.class); + thrown.expectMessage("Value present but other values as well"); + ts.assertValue(new Predicate() { + @Override public boolean test(final Integer o) throws Exception { + return o == 1; + } + }); + } +} From 79e692f84ae394dc9a08d94f8e2382c3b08e7b5c Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Mon, 26 Sep 2016 14:57:53 +0200 Subject: [PATCH 2/2] Adjust --- .../java/io/reactivex/observers/TestObserver.java | 13 +++++-------- .../io/reactivex/subscribers/TestSubscriber.java | 13 +++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/reactivex/observers/TestObserver.java b/src/main/java/io/reactivex/observers/TestObserver.java index 47e5a18cd0..723eab46b2 100644 --- a/src/main/java/io/reactivex/observers/TestObserver.java +++ b/src/main/java/io/reactivex/observers/TestObserver.java @@ -549,15 +549,12 @@ public final TestObserver assertValue(Predicate valuePredicate) { boolean found = false; - for (T value : values) { - try { - if (valuePredicate.test(value)) { - found = true; - break; - } - } catch (Exception ex) { - throw ExceptionHelper.wrapOrThrow(ex); + try { + if (valuePredicate.test(values.get(0))) { + found = true; } + } catch (Exception ex) { + throw ExceptionHelper.wrapOrThrow(ex); } if (found) { diff --git a/src/main/java/io/reactivex/subscribers/TestSubscriber.java b/src/main/java/io/reactivex/subscribers/TestSubscriber.java index 063d27799b..c8217e4fcc 100644 --- a/src/main/java/io/reactivex/subscribers/TestSubscriber.java +++ b/src/main/java/io/reactivex/subscribers/TestSubscriber.java @@ -593,15 +593,12 @@ public final TestSubscriber assertValue(Predicate valuePredicate) { boolean found = false; - for (T value : values) { - try { - if (valuePredicate.test(value)) { - found = true; - break; - } - } catch (Exception ex) { - throw ExceptionHelper.wrapOrThrow(ex); + try { + if (valuePredicate.test(values.get(0))) { + found = true; } + } catch (Exception ex) { + throw ExceptionHelper.wrapOrThrow(ex); } if (found) {