Skip to content

Commit 3356444

Browse files
authored
2.x: add TestSubscriber.withTag (ReactiveX#5137)
1 parent a03bf90 commit 3356444

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/main/java/io/reactivex/observers/BaseTestConsumer.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public abstract class BaseTestConsumer<T, U extends BaseTestConsumer<T, U>> impl
4848

4949
protected int establishedFusionMode;
5050

51+
protected CharSequence tag;
52+
5153
public BaseTestConsumer() {
5254
this.values = new ArrayList<T>();
5355
this.errors = new ArrayList<Throwable>();
@@ -129,6 +131,15 @@ protected final AssertionError fail(String message) {
129131
.append("values = ").append(values.size()).append(", ")
130132
.append("errors = ").append(errors.size()).append(", ")
131133
.append("completions = ").append(completions)
134+
;
135+
136+
CharSequence tag = this.tag;
137+
if (tag != null) {
138+
b.append(", tag = ")
139+
.append(tag);
140+
}
141+
142+
b
132143
.append(')')
133144
;
134145

@@ -747,4 +758,18 @@ public final U assertEmpty() {
747758
.assertNoErrors()
748759
.assertNotComplete();
749760
}
761+
762+
/**
763+
* Set the tag displayed along with an assertion failure's
764+
* other state information.
765+
* @param tag the string to display (null won't print any tag)
766+
* @return this
767+
* @since 2.0.7 - experimental
768+
*/
769+
@SuppressWarnings("unchecked")
770+
@Experimental
771+
public final U withTag(CharSequence tag) {
772+
this.tag = tag;
773+
return (U)this;
774+
}
750775
}

src/test/java/io/reactivex/observers/TestObserverTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,4 +1381,20 @@ public void assertValueAtInvalidIndex() {
13811381
}
13821382
});
13831383
}
1384+
1385+
@Test
1386+
public void withTag() {
1387+
try {
1388+
for (int i = 1; i < 3; i++) {
1389+
Observable.just(i)
1390+
.test()
1391+
.withTag("testing with item=" + i)
1392+
.assertResult(1)
1393+
;
1394+
}
1395+
fail("Should have thrown!");
1396+
} catch (AssertionError ex) {
1397+
assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
1398+
}
1399+
}
13841400
}

src/test/java/io/reactivex/subscribers/TestSubscriberTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,4 +1777,20 @@ public void requestMore() {
17771777
.requestMore(3)
17781778
.assertResult(1, 2, 3, 4, 5);
17791779
}
1780+
1781+
@Test
1782+
public void withTag() {
1783+
try {
1784+
for (int i = 1; i < 3; i++) {
1785+
Flowable.just(i)
1786+
.test()
1787+
.withTag("testing with item=" + i)
1788+
.assertResult(1)
1789+
;
1790+
}
1791+
fail("Should have thrown!");
1792+
} catch (AssertionError ex) {
1793+
assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
1794+
}
1795+
}
17801796
}

0 commit comments

Comments
 (0)