Skip to content

Commit 9074410

Browse files
authored
1.x: add TestSubscriber.assertValuesAndClear (#4322)
* 1.x: add TestSubscriber.assertAndConsume * Rename to assertValuesAndClear() * Update javadoc example * Tag as experimental
1 parent cd0301b commit 9074410

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

src/main/java/rx/observers/TestSubscriber.java

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -328,19 +328,22 @@ public void assertReceivedOnNext(List<T> items) {
328328
}
329329

330330
for (int i = 0; i < items.size(); i++) {
331-
T expected = items.get(i);
332-
T actual = values.get(i);
333-
if (expected == null) {
334-
// check for null equality
335-
if (actual != null) {
336-
assertionError("Value at index: " + i + " expected to be [null] but was: [" + actual + "]\n");
337-
}
338-
} else if (!expected.equals(actual)) {
339-
assertionError("Value at index: " + i
340-
+ " expected to be [" + expected + "] (" + expected.getClass().getSimpleName()
341-
+ ") but was: [" + actual + "] (" + (actual != null ? actual.getClass().getSimpleName() : "null") + ")\n");
342-
331+
assertItem(items.get(i), i);
332+
}
333+
}
334+
335+
private void assertItem(T expected, int i) {
336+
T actual = values.get(i);
337+
if (expected == null) {
338+
// check for null equality
339+
if (actual != null) {
340+
assertionError("Value at index: " + i + " expected to be [null] but was: [" + actual + "]\n");
343341
}
342+
} else if (!expected.equals(actual)) {
343+
assertionError("Value at index: " + i
344+
+ " expected to be [" + expected + "] (" + expected.getClass().getSimpleName()
345+
+ ") but was: [" + actual + "] (" + (actual != null ? actual.getClass().getSimpleName() : "null") + ")\n");
346+
344347
}
345348
}
346349

@@ -670,4 +673,35 @@ final void assertionError(String message) {
670673
}
671674
throw ae;
672675
}
676+
677+
/**
678+
* Assert that the TestSubscriber contains the given first and optional rest values exactly
679+
* and if so, clears the internal list of values.
680+
* <p>
681+
* <code><pre>
682+
* TestSubscriber ts = new TestSubscriber();
683+
*
684+
* ts.onNext(1);
685+
*
686+
* ts.assertValuesAndClear(1);
687+
*
688+
* ts.onNext(2);
689+
* ts.onNext(3);
690+
*
691+
* ts.assertValuesAndClear(2, 3); // no mention of 1
692+
* </pre></code>
693+
* @param expectedFirstValue the expected first value
694+
* @param expectedRestValues the optional rest values
695+
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number)
696+
*/
697+
@Experimental
698+
public final void assertValuesAndClear(T expectedFirstValue, T... expectedRestValues) {
699+
int n = 1 + expectedRestValues.length;
700+
assertValueCount(n);
701+
assertItem(expectedFirstValue, 0);
702+
for (int i = 0; i < expectedRestValues.length; i++) {
703+
assertItem(expectedRestValues[i], i + 1);
704+
}
705+
values.clear();
706+
}
673707
}

src/test/java/rx/observers/TestSubscriberTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,4 +772,47 @@ public void awaitValueCountFails() throws Exception {
772772
Assert.assertFalse(ts.awaitValueCount(5, 1, TimeUnit.SECONDS));
773773

774774
}
775+
776+
@Test
777+
public void assertAndConsume() {
778+
TestSubscriber<Integer> ts = TestSubscriber.create();
779+
780+
ts.assertNoValues();
781+
782+
ts.onNext(1);
783+
784+
ts.assertValuesAndClear(1);
785+
786+
ts.assertNoValues();
787+
788+
ts.onNext(2);
789+
ts.onNext(3);
790+
791+
ts.assertValueCount(2);
792+
793+
ts.assertValuesAndClear(2, 3);
794+
795+
ts.onNext(4);
796+
ts.onNext(5);
797+
798+
try {
799+
ts.assertValuesAndClear(4);
800+
Assert.fail("Should have thrown AssertionError");
801+
} catch (AssertionError ex) {
802+
// expected
803+
}
804+
805+
ts.assertValueCount(2);
806+
807+
try {
808+
ts.assertValuesAndClear(4, 5, 6);
809+
Assert.fail("Should have thrown AssertionError");
810+
} catch (AssertionError ex) {
811+
// expected
812+
}
813+
814+
ts.assertValuesAndClear(4, 5);
815+
816+
ts.assertNoValues();
817+
}
775818
}

0 commit comments

Comments
 (0)