Skip to content

Commit 9c34eb1

Browse files
sergiomarqmouraakarnokd
authored andcommitted
2.x: assertNever(T value) / assertNever(Predicate<T> valuePredicate) (#4994)
* 2.x: assertNever(T value) / assertNever(Predicate<T> valuePredicate) * fixed Java compatibility issuesJ * fixed Java type compatibility issues * changes according to PR review * changes according second PR review
1 parent 2e6f94c commit 9c34eb1

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

src/main/java/io/reactivex/observers/BaseTestConsumer.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.concurrent.*;
1818

1919
import io.reactivex.Notification;
20+
import io.reactivex.annotations.Experimental;
2021
import io.reactivex.disposables.Disposable;
2122
import io.reactivex.exceptions.CompositeException;
2223
import io.reactivex.functions.Predicate;
@@ -303,6 +304,28 @@ public final U assertValue(T value) {
303304
return (U)this;
304305
}
305306

307+
/**
308+
* Assert that this TestObserver/TestSubscriber did not receive an onNext value which is equal to
309+
* the given value with respect to Objects.equals.
310+
*
311+
* @since 2.0.5 - experimental
312+
* @param value the value to expect not being received
313+
* @return this;
314+
*/
315+
@Experimental
316+
@SuppressWarnings("unchecked")
317+
public final U assertNever(T value) {
318+
int s = values.size();
319+
320+
for (int i = 0; i < s; i++) {
321+
T v = this.values.get(i);
322+
if (ObjectHelper.equals(v, value)) {
323+
throw fail("Value at position " + i + " is equal to " + valueAndClass(value) + "; Expected them to be different");
324+
}
325+
}
326+
return (U) this;
327+
}
328+
306329
/**
307330
* Asserts that this TestObserver/TestSubscriber received exactly one onNext value for which
308331
* the provided predicate returns true.
@@ -322,6 +345,33 @@ public final U assertValue(Predicate<T> valuePredicate) {
322345
return (U)this;
323346
}
324347

348+
/**
349+
* Asserts that this TestObserver/TestSubscriber did not receive any onNext value for which
350+
* the provided predicate returns true.
351+
*
352+
* @since 2.0.5 - experimental
353+
* @param valuePredicate the predicate that receives the onNext value
354+
* and should return true for the expected value.
355+
* @return this
356+
*/
357+
@Experimental
358+
@SuppressWarnings("unchecked")
359+
public final U assertNever(Predicate<? super T> valuePredicate) {
360+
int s = values.size();
361+
362+
for (int i = 0; i < s; i++) {
363+
T v = this.values.get(i);
364+
try {
365+
if (valuePredicate.test(v)) {
366+
throw fail("Value at position " + i + " matches predicate " + valuePredicate.toString() + ", which was not expected.");
367+
}
368+
} catch (Exception ex) {
369+
throw ExceptionHelper.wrapOrThrow(ex);
370+
}
371+
}
372+
return (U)this;
373+
}
374+
325375
/**
326376
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
327377
* for the provided predicate returns true.

src/test/java/io/reactivex/observers/TestObserverTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,64 @@ public void testAssertNotMatchValue() {
8686
o.assertTerminated();
8787
}
8888

89+
@Test
90+
public void assertNeverAtNotMatchingValue() {
91+
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
92+
TestSubscriber<Integer> o = new TestSubscriber<Integer>();
93+
oi.subscribe(o);
94+
95+
o.assertNever(3);
96+
o.assertValueCount(2);
97+
o.assertTerminated();
98+
}
99+
100+
@Test
101+
public void assertNeverAtMatchingValue() {
102+
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
103+
TestSubscriber<Integer> o = new TestSubscriber<Integer>();
104+
oi.subscribe(o);
105+
106+
o.assertValues(1, 2);
107+
108+
thrown.expect(AssertionError.class);
109+
110+
o.assertNever(2);
111+
o.assertValueCount(2);
112+
o.assertTerminated();
113+
}
114+
115+
@Test
116+
public void assertNeverAtMatchingPredicate() {
117+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
118+
119+
Flowable.just(1, 2).subscribe(ts);
120+
121+
ts.assertValues(1, 2);
122+
123+
thrown.expect(AssertionError.class);
124+
125+
ts.assertNever(new Predicate<Integer>() {
126+
@Override
127+
public boolean test(final Integer o) throws Exception {
128+
return o == 1;
129+
}
130+
});
131+
}
132+
133+
@Test
134+
public void assertNeverAtNotMatchingPredicate() {
135+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
136+
137+
Flowable.just(2, 3).subscribe(ts);
138+
139+
ts.assertNever(new Predicate<Integer>() {
140+
@Override
141+
public boolean test(final Integer o) throws Exception {
142+
return o == 1;
143+
}
144+
});
145+
}
146+
89147
@Test
90148
public void testAssertTerminalEventNotReceived() {
91149
PublishProcessor<Integer> p = PublishProcessor.create();

src/test/java/io/reactivex/subscribers/TestSubscriberTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,64 @@ public void testAssertNotMatchValue() {
8383
o.assertTerminated();
8484
}
8585

86+
@Test
87+
public void assertNeverAtNotMatchingValue() {
88+
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
89+
TestSubscriber<Integer> o = new TestSubscriber<Integer>();
90+
oi.subscribe(o);
91+
92+
o.assertNever(3);
93+
o.assertValueCount(2);
94+
o.assertTerminated();
95+
}
96+
97+
@Test
98+
public void assertNeverAtMatchingValue() {
99+
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
100+
TestSubscriber<Integer> o = new TestSubscriber<Integer>();
101+
oi.subscribe(o);
102+
103+
o.assertValues(1, 2);
104+
105+
thrown.expect(AssertionError.class);
106+
107+
o.assertNever(2);
108+
o.assertValueCount(2);
109+
o.assertTerminated();
110+
}
111+
112+
@Test
113+
public void assertNeverAtMatchingPredicate() {
114+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
115+
116+
Flowable.just(1, 2).subscribe(ts);
117+
118+
ts.assertValues(1, 2);
119+
120+
thrown.expect(AssertionError.class);
121+
122+
ts.assertNever(new Predicate<Integer>() {
123+
@Override
124+
public boolean test(final Integer o) throws Exception {
125+
return o == 1;
126+
}
127+
});
128+
}
129+
130+
@Test
131+
public void assertNeverAtNotMatchingPredicate() {
132+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
133+
134+
Flowable.just(2, 3).subscribe(ts);
135+
136+
ts.assertNever(new Predicate<Integer>() {
137+
@Override
138+
public boolean test(final Integer o) throws Exception {
139+
return o == 1;
140+
}
141+
});
142+
}
143+
86144
@Test
87145
public void testAssertTerminalEventNotReceived() {
88146
PublishProcessor<Integer> p = PublishProcessor.create();

0 commit comments

Comments
 (0)