Skip to content

Commit

Permalink
2.x: Clarify TestObserver.assertValueSet in docs and via tests (#6152)
Browse files Browse the repository at this point in the history
* 2.x: Clarify TestObserver.assertValueSet in docs and via tests

* Grammar.
  • Loading branch information
akarnokd authored Aug 10, 2018
1 parent 0e7b8ea commit a16f63f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
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.
* <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()}.
*
* @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));
}
}
}

0 comments on commit a16f63f

Please sign in to comment.