Skip to content
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

2.x: Clarify TestObserver.assertValueSet in docs and via tests #6152

Merged
merged 2 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions src/main/java/io/reactivex/observers/BaseTestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,14 @@ public final U assertValuesOnly(T... values) {
}

/**
* Assert that the TestObserver/TestSubscriber received only the specified values in any order.
* <p>This helps asserting when the order of the values is not guaranteed, i.e., when merging
* Assert that the TestObserver/TestSubscriber received only items that are in the specified
* collection as well irrespective of the order they were received.
Copy link
Contributor

Choose a reason for hiding this comment

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

collection as well irrespective of the order they were received.

->

collection as well, irrespective of the order in which they were received.

* <p>
* This helps asserting when the order of the values is not guaranteed, i.e., when merging
* asynchronous streams.
* <p>
* To ensure that only the expected items have been received, no more and no less, in any order,
* apply {@link #assertValueCount(int)} with {@code expected.size()}.
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess ideally method name would indicate that by using a word like contains, maybe add it to 3.x list of changes?

Copy link
Contributor

Choose a reason for hiding this comment

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

assertValuesIn(Collection<? extends T> expected)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Posted #6153.

Copy link
Contributor

Choose a reason for hiding this comment

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

As-is this method "ignores" duplicates, while assertValueCount doesn't. If a user wishes to assert that all values were received while allowing for duplicates, they could to .distinct().test(). Is this the preferred approach, and if so, is it worth documenting that too? (Maybe have an example of that in the test below?)

Copy link
Member Author

Choose a reason for hiding this comment

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

The aim is to provide some typical set of assertions with TestObserver. You can always modulate the input flow or simply enumerate contents of the TestObserver manually. I usually try to minimize the cases where additional operators are mentioned in the javadocs because it triggers "what about when using X" type of comments.

*
* @param expected the collection of values expected in any order
* @return this
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/io/reactivex/observers/TestObserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1626,4 +1626,38 @@ public void assertValueSequenceOnlyThrowsWhenErrored() {
// expected
}
}

@Test
public void assertValueSetWiderSet() {
Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7));

Observable.just(4, 5, 1, 3, 2)
.test()
.assertValueSet(set);
}

@Test
public void assertValueSetExact() {
Set<Integer> set = new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5));

Observable.just(4, 5, 1, 3, 2)
.test()
.assertValueSet(set)
.assertValueCount(set.size());
}

@Test
public void assertValueSetMissing() {
Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 4, 5, 6, 7));

try {
Observable.range(1, 5)
.test()
.assertValueSet(set);

throw new RuntimeException("Should have failed");
} catch (AssertionError ex) {
assertTrue(ex.getMessage(), ex.getMessage().contains("Value not in the expected collection: " + 3));
}
}
}
34 changes: 34 additions & 0 deletions src/test/java/io/reactivex/subscribers/TestSubscriberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2186,4 +2186,38 @@ public void awaitCount0() {
TestSubscriber<Integer> ts = TestSubscriber.create();
ts.awaitCount(0, TestWaitStrategy.SLEEP_1MS, 0);
}

@Test
public void assertValueSetWiderSet() {
Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7));

Flowable.just(4, 5, 1, 3, 2)
.test()
.assertValueSet(set);
}

@Test
public void assertValueSetExact() {
Set<Integer> set = new HashSet<Integer>(Arrays.asList(1, 2, 3, 4, 5));

Flowable.just(4, 5, 1, 3, 2)
.test()
.assertValueSet(set)
.assertValueCount(set.size());
}

@Test
public void assertValueSetMissing() {
Set<Integer> set = new HashSet<Integer>(Arrays.asList(0, 1, 2, 4, 5, 6, 7));

try {
Flowable.range(1, 5)
.test()
.assertValueSet(set);

throw new RuntimeException("Should have failed");
} catch (AssertionError ex) {
assertTrue(ex.getMessage(), ex.getMessage().contains("Value not in the expected collection: " + 3));
}
}
}