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: Add assertValueSetOnly and assertValueSequenceOnly to TestObserver + TestSubscriber #6010

Merged
merged 3 commits into from
May 18, 2018
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
30 changes: 30 additions & 0 deletions src/main/java/io/reactivex/observers/BaseTestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,21 @@ public final U assertValueSet(Collection<? extends T> expected) {
return (U)this;
}

/**
* Assert that the TestObserver/TestSubscriber received only the specified values in any order without terminating.
* @param expected the collection of values expected in any order
* @return this;
* @since 2.1.14 - experimental
*/
@SuppressWarnings("unchecked")
@Experimental
public final U assertValueSetOnly(Collection<? extends T> expected) {
return assertSubscribed()
.assertValueSet(expected)
.assertNoErrors()
.assertNotComplete();
}

/**
* Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.
* @param sequence the sequence of expected values in order
Expand Down Expand Up @@ -625,6 +640,21 @@ public final U assertValueSequence(Iterable<? extends T> sequence) {
return (U)this;
}

/**
* Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating.
* @param sequence the sequence of expected values in order
* @return this;
* @since 2.1.14 - experimental
*/
@SuppressWarnings("unchecked")
@Experimental
public final U assertValueSequenceOnly(Iterable<? extends T> sequence) {
return assertSubscribed()
.assertValueSequence(sequence)
.assertNoErrors()
.assertNotComplete();
}

/**
* Assert that the TestObserver/TestSubscriber terminated (i.e., the terminal latch reached zero).
* @return this;
Expand Down
132 changes: 128 additions & 4 deletions src/test/java/io/reactivex/observers/TestObserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ public void withTag() {
.assertResult(1)
;
}
fail("Should have thrown!");
throw new RuntimeException("Should have thrown!");
} catch (AssertionError ex) {
assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
}
Expand Down Expand Up @@ -1466,7 +1466,7 @@ public void assertValuesOnlyThrowsOnUnexpectedValue() {

try {
to.assertValuesOnly(5);
fail();
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
Expand All @@ -1481,7 +1481,7 @@ public void assertValuesOnlyThrowsWhenCompleted() {

try {
to.assertValuesOnly();
fail();
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
Expand All @@ -1496,7 +1496,131 @@ public void assertValuesOnlyThrowsWhenErrored() {

try {
to.assertValuesOnly();
fail();
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
}

@Test
public void assertValueSetOnly() {
TestObserver<Integer> to = TestObserver.create();
to.onSubscribe(Disposables.empty());
to.assertValueSetOnly(Collections.<Integer>emptySet());

to.onNext(5);
to.assertValueSetOnly(Collections.singleton(5));

to.onNext(-1);
to.assertValueSetOnly(new HashSet<Integer>(Arrays.asList(5, -1)));
}

@Test
public void assertValueSetOnlyThrowsOnUnexpectedValue() {
TestObserver<Integer> to = TestObserver.create();
to.onSubscribe(Disposables.empty());
to.assertValueSetOnly(Collections.<Integer>emptySet());

to.onNext(5);
to.assertValueSetOnly(Collections.singleton(5));

to.onNext(-1);

try {
to.assertValueSetOnly(Collections.singleton(5));
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
}

@Test
public void assertValueSetOnlyThrowsWhenCompleted() {
TestObserver<Integer> to = TestObserver.create();
to.onSubscribe(Disposables.empty());

to.onComplete();

try {
to.assertValueSetOnly(Collections.<Integer>emptySet());
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
}

@Test
public void assertValueSetOnlyThrowsWhenErrored() {
TestObserver<Integer> to = TestObserver.create();
to.onSubscribe(Disposables.empty());

to.onError(new TestException());

try {
to.assertValueSetOnly(Collections.<Integer>emptySet());
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
}

@Test
public void assertValueSequenceOnly() {
TestObserver<Integer> to = TestObserver.create();
to.onSubscribe(Disposables.empty());
to.assertValueSequenceOnly(Collections.<Integer>emptyList());

to.onNext(5);
to.assertValueSequenceOnly(Collections.singletonList(5));

to.onNext(-1);
to.assertValueSequenceOnly(Arrays.asList(5, -1));
}

@Test
public void assertValueSequenceOnlyThrowsOnUnexpectedValue() {
TestObserver<Integer> to = TestObserver.create();
to.onSubscribe(Disposables.empty());
to.assertValueSequenceOnly(Collections.<Integer>emptyList());

to.onNext(5);
to.assertValueSequenceOnly(Collections.singletonList(5));

to.onNext(-1);

try {
to.assertValueSequenceOnly(Collections.singletonList(5));
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
}

@Test
public void assertValueSequenceOnlyThrowsWhenCompleted() {
TestObserver<Integer> to = TestObserver.create();
to.onSubscribe(Disposables.empty());

to.onComplete();

try {
to.assertValueSequenceOnly(Collections.<Integer>emptyList());
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
}

@Test
public void assertValueSequenceOnlyThrowsWhenErrored() {
TestObserver<Integer> to = TestObserver.create();
to.onSubscribe(Disposables.empty());

to.onError(new TestException());

try {
to.assertValueSequenceOnly(Collections.<Integer>emptyList());
throw new RuntimeException();
} catch (AssertionError ex) {
// expected
}
Expand Down
Loading