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 assertValuesOnly to BaseTestConsumer. #5568

Merged
merged 2 commits into from
Aug 25, 2017
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
15 changes: 15 additions & 0 deletions src/main/java/io/reactivex/observers/BaseTestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,21 @@ public final U assertValues(T... values) {
return (U)this;
}

/**
* Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating.
* @param values the values expected
* @return this;
* @since 2.1.4
*/
@SuppressWarnings("unchecked")
@Experimental
public final U assertValuesOnly(T... values) {
return assertSubscribed()
.assertValues(values)
.assertNoErrors()
.assertNotComplete();
}

/**
* 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
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/io/reactivex/observers/TestObserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1439,4 +1439,66 @@ public void withTag() {
assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
}
}

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

to.onNext(5);
to.assertValuesOnly(5);

to.onNext(-1);
to.assertValuesOnly(5, -1);
}

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

to.onNext(5);
to.assertValuesOnly(5);

to.onNext(-1);

try {
to.assertValuesOnly(5);
fail();
} catch (AssertionError ex) {
// expected
}
}

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

to.onComplete();

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

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

to.onError(new TestException());

try {
to.assertValuesOnly();
fail();
} catch (AssertionError ex) {
// expected
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Not super important, but if you have time let's add test that verifies that method fails if expected value was emitted :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You mean was not emitted right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant "unexpected", sorry :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

}
62 changes: 62 additions & 0 deletions src/test/java/io/reactivex/subscribers/TestSubscriberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1997,4 +1997,66 @@ public void waitStrategyRuns() {
ws.run();
}
}

@Test
public void assertValuesOnly() {
TestSubscriber<Integer> ts = TestSubscriber.create();
ts.onSubscribe(new BooleanSubscription());
ts.assertValuesOnly();

ts.onNext(5);
ts.assertValuesOnly(5);

ts.onNext(-1);
ts.assertValuesOnly(5, -1);
}

@Test
public void assertValuesOnlyThrowsOnUnexpectedValue() {
TestSubscriber<Integer> ts = TestSubscriber.create();
ts.onSubscribe(new BooleanSubscription());
ts.assertValuesOnly();

ts.onNext(5);
ts.assertValuesOnly(5);

ts.onNext(-1);

try {
ts.assertValuesOnly(5);
fail();
} catch (AssertionError ex) {
// expected
}
}

@Test
public void assertValuesOnlyThrowsWhenCompleted() {
TestSubscriber<Integer> ts = TestSubscriber.create();
ts.onSubscribe(new BooleanSubscription());

ts.onComplete();

try {
ts.assertValuesOnly();
fail();
} catch (AssertionError ex) {
// expected
}
}

@Test
public void assertValuesOnlyThrowsWhenErrored() {
TestSubscriber<Integer> ts = TestSubscriber.create();
ts.onSubscribe(new BooleanSubscription());

ts.onError(new TestException());

try {
ts.assertValuesOnly();
fail();
} catch (AssertionError ex) {
// expected
}
}
}