Skip to content

Commit

Permalink
2.x: add TestSubscriber.withTag (#5137)
Browse files Browse the repository at this point in the history
  • Loading branch information
akarnokd authored Feb 27, 2017
1 parent a03bf90 commit 3356444
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
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"));
}
}
}

0 comments on commit 3356444

Please sign in to comment.