Skip to content

1.x: add TestSubscriber.assertValuesAndClear #4322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions src/main/java/rx/observers/TestSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,22 @@ public void assertReceivedOnNext(List<T> items) {
}

for (int i = 0; i < items.size(); i++) {
T expected = items.get(i);
T actual = values.get(i);
if (expected == null) {
// check for null equality
if (actual != null) {
assertionError("Value at index: " + i + " expected to be [null] but was: [" + actual + "]\n");
}
} else if (!expected.equals(actual)) {
assertionError("Value at index: " + i
+ " expected to be [" + expected + "] (" + expected.getClass().getSimpleName()
+ ") but was: [" + actual + "] (" + (actual != null ? actual.getClass().getSimpleName() : "null") + ")\n");

assertItem(items.get(i), i);
}
}

private void assertItem(T expected, int i) {
T actual = values.get(i);
if (expected == null) {
// check for null equality
if (actual != null) {
assertionError("Value at index: " + i + " expected to be [null] but was: [" + actual + "]\n");
}
} else if (!expected.equals(actual)) {
assertionError("Value at index: " + i
+ " expected to be [" + expected + "] (" + expected.getClass().getSimpleName()
+ ") but was: [" + actual + "] (" + (actual != null ? actual.getClass().getSimpleName() : "null") + ")\n");

}
}

Expand Down Expand Up @@ -670,4 +673,35 @@ final void assertionError(String message) {
}
throw ae;
}

/**
* Assert that the TestSubscriber contains the given first and optional rest values exactly
* and if so, clears the internal list of values.
* <p>
* <code><pre>
* TestSubscriber ts = new TestSubscriber();
*
* ts.onNext(1);
*
* ts.assertValuesAndClear(1);
*
* ts.onNext(2);
* ts.onNext(3);
*
* ts.assertValuesAndClear(2, 3); // no mention of 1
* </pre></code>
* @param expectedFirstValue the expected first value
* @param expectedRestValues the optional rest values
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number)
*/
@Experimental
public final void assertValuesAndClear(T expectedFirstValue, T... expectedRestValues) {
int n = 1 + expectedRestValues.length;
assertValueCount(n);
assertItem(expectedFirstValue, 0);
for (int i = 0; i < expectedRestValues.length; i++) {
assertItem(expectedRestValues[i], i + 1);
}
values.clear();
}
}
43 changes: 43 additions & 0 deletions src/test/java/rx/observers/TestSubscriberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,4 +772,47 @@ public void awaitValueCountFails() throws Exception {
Assert.assertFalse(ts.awaitValueCount(5, 1, TimeUnit.SECONDS));

}

@Test
public void assertAndConsume() {
TestSubscriber<Integer> ts = TestSubscriber.create();

ts.assertNoValues();

ts.onNext(1);

ts.assertValuesAndClear(1);

ts.assertNoValues();

ts.onNext(2);
ts.onNext(3);

ts.assertValueCount(2);

ts.assertValuesAndClear(2, 3);

ts.onNext(4);
ts.onNext(5);

try {
ts.assertValuesAndClear(4);
Assert.fail("Should have thrown AssertionError");
} catch (AssertionError ex) {
// expected
}

ts.assertValueCount(2);

try {
ts.assertValuesAndClear(4, 5, 6);
Assert.fail("Should have thrown AssertionError");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: static import

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does IntelliJ do that automatically? Eclipse doesn't so I have to go and manually type in the import; writing out Assert is more convenient for me. If you want, you can turn all of these into static imports in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In IntelliJ I usually just write fail( and hit ALT + ENTER to apply static import, IntellIJ applies it for whole file then

} catch (AssertionError ex) {
// expected
}

ts.assertValuesAndClear(4, 5);

ts.assertNoValues();
}
}