Skip to content

2.x: add TestSubscriber.withTag #5137

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 1 commit into from
Feb 27, 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
25 changes: 25 additions & 0 deletions src/main/java/io/reactivex/observers/BaseTestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public abstract class BaseTestConsumer<T, U extends BaseTestConsumer<T, U>> impl

protected int establishedFusionMode;

protected CharSequence tag;

public BaseTestConsumer() {
this.values = new ArrayList<T>();
this.errors = new ArrayList<Throwable>();
Expand Down Expand Up @@ -129,6 +131,15 @@ protected final AssertionError fail(String message) {
.append("values = ").append(values.size()).append(", ")
.append("errors = ").append(errors.size()).append(", ")
.append("completions = ").append(completions)
;

CharSequence tag = this.tag;
if (tag != null) {
b.append(", tag = ")
.append(tag);
}

b
.append(')')
;

Expand Down Expand Up @@ -747,4 +758,18 @@ public final U assertEmpty() {
.assertNoErrors()
.assertNotComplete();
}

/**
* Set the tag displayed along with an assertion failure's
* other state information.
* @param tag the string to display (null won't print any tag)
* @return this
* @since 2.0.7 - experimental
*/
@SuppressWarnings("unchecked")
@Experimental
public final U withTag(CharSequence tag) {
this.tag = tag;
return (U)this;
}
}
16 changes: 16 additions & 0 deletions src/test/java/io/reactivex/observers/TestObserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1381,4 +1381,20 @@ public void assertValueAtInvalidIndex() {
}
});
}

@Test
public void withTag() {
try {
for (int i = 1; i < 3; i++) {
Observable.just(i)
.test()
.withTag("testing with item=" + i)
.assertResult(1)
;
}
fail("Should have thrown!");
} catch (AssertionError ex) {
assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
}
}
}
16 changes: 16 additions & 0 deletions src/test/java/io/reactivex/subscribers/TestSubscriberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1777,4 +1777,20 @@ public void requestMore() {
.requestMore(3)
.assertResult(1, 2, 3, 4, 5);
}

@Test
public void withTag() {
try {
for (int i = 1; i < 3; i++) {
Flowable.just(i)
.test()
.withTag("testing with item=" + i)
.assertResult(1)
;
}
fail("Should have thrown!");
} catch (AssertionError ex) {
assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
}
}
}