Skip to content

Commit

Permalink
Fix assert message format (#7351)
Browse files Browse the repository at this point in the history
* Fix assert message format

* Test error messages against regex
  • Loading branch information
keytronic committed Oct 14, 2021
1 parent 1f87a43 commit c2bf514
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ public final U assertValueAt(int index, @NonNull T value) {

T v = values.get(index);
if (!Objects.equals(value, v)) {
throw fail("Values at position " + index + " differ;\nexpected: " + valueAndClass(value) + "\ngot: " + valueAndClass(v));
throw fail("\nexpected: " + valueAndClass(value) + "\ngot: " + valueAndClass(v)
+ "; Value at position " + index + " differ");
}
return (U)this;
}
Expand Down Expand Up @@ -425,7 +426,7 @@ public static String valueAndClass(@Nullable Object o) {
public final U assertValueCount(int count) {
int s = values.size();
if (s != count) {
throw fail("Value counts differ;\nexpected: " + count + "\ngot: " + s);
throw fail("\nexpected: " + count + "\ngot: " + s + "; Value counts differ");
}
return (U)this;
}
Expand All @@ -450,14 +451,15 @@ public final U assertNoValues() {
public final U assertValues(@NonNull T... values) {
int s = this.values.size();
if (s != values.length) {
throw fail("Value count differs;\nexpected: " + values.length + " " + Arrays.toString(values)
+ "\ngot: " + s + " " + this.values);
throw fail("\nexpected: " + values.length + " " + Arrays.toString(values)
+ "\ngot: " + s + " " + this.values + "; Value count differs");
}
for (int i = 0; i < s; i++) {
T v = this.values.get(i);
T u = values[i];
if (!Objects.equals(u, v)) {
throw fail("Values at position " + i + " differ;\nexpected: " + valueAndClass(u) + "\ngot: " + valueAndClass(v));
throw fail("\nexpected: " + valueAndClass(u) + "\ngot: " + valueAndClass(v)
+ "; Value at position " + i + " differ");
}
}
return (U)this;
Expand Down Expand Up @@ -504,7 +506,8 @@ public final U assertValueSequence(@NonNull Iterable<? extends T> sequence) {
T v = actualIterator.next();

if (!Objects.equals(u, v)) {
throw fail("Values at position " + i + " differ;\nexpected: " + valueAndClass(u) + "\ngot: " + valueAndClass(v));
throw fail("\nexpected: " + valueAndClass(u) + "\ngot: " + valueAndClass(v)
+ "; Value at position " + i + " differ");
}
i++;
}
Expand Down
107 changes: 106 additions & 1 deletion src/test/java/io/reactivex/rxjava3/observers/TestObserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ static void assertThrowsWithMessage(String message, Class<? extends Throwable> c
assertEquals(message, assertThrows(clazz, run).getMessage());
}

static void assertThrowsWithMessageMatchRegex(String regex, Class<? extends Throwable> clazz, ThrowingRunnable run) {
assertTrue(assertThrows(clazz, run).getMessage().matches(regex));
}

private static final String ASSERT_MESSAGE_REGEX = "\nexpected: (.*)\n\\s*got: (.*)";

@Test
public void assertTestObserver() {
Flowable<Integer> oi = Flowable.fromIterable(Arrays.asList(1, 2));
Expand Down Expand Up @@ -999,7 +1005,7 @@ public void assertValueAtIndexMatch() {

@Test
public void assertValueAtIndexNoMatch() {
assertThrowsWithMessage("Values at position 2 differ;\nexpected: b (class: String)\ngot: c (class: String) (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> {
assertThrowsWithMessage("\nexpected: b (class: String)\ngot: c (class: String); Value at position 2 differ (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);
Expand All @@ -1008,6 +1014,105 @@ public void assertValueAtIndexNoMatch() {
});
}

@Test
public void assertValueAtIndexThrowsMessageMatchRegex() {
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValueAt(2, "b");
});
}

@Test
public void assertValuesCountNoMatch() {
assertThrowsWithMessage("\nexpected: 2 [a, b]\ngot: 3 [a, b, c]; Value count differs (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValues("a", "b");
});
}

@Test
public void assertValuesCountThrowsMessageMatchRegex() {
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValues("a", "b");
});
}

@Test
public void assertValuesNoMatch() {
assertThrowsWithMessage("\nexpected: d (class: String)\ngot: c (class: String); Value at position 2 differ (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValues("a", "b", "d");
});
}

@Test
public void assertValuesThrowsMessageMatchRegex() {
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValues("a", "b", "d");
});
}

@Test
public void assertValueCountNoMatch() {
assertThrowsWithMessage("\nexpected: 2\ngot: 3; Value counts differ (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValueCount(2);
});
}

@Test
public void assertValueCountThrowsMessageMatchRegex() {
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValueCount(2);
});
}

@Test
public void assertValueSequenceNoMatch() {
assertThrowsWithMessage("\nexpected: d (class: String)\ngot: c (class: String); Value at position 2 differ (latch = 0, values = 3, errors = 0, completions = 1)", AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValueSequence(Arrays.asList("a", "b", "d"));
});
}

@Test
public void assertValueSequenceThrowsMessageMatchRegex() {
assertThrowsWithMessageMatchRegex(ASSERT_MESSAGE_REGEX, AssertionError.class, () -> {
TestObserver<String> to = new TestObserver<>();

Observable.just("a", "b", "c").subscribe(to);

to.assertValueSequence(Arrays.asList("a", "b", "d"));
});
}

@Test
public void assertValueAtIndexInvalidIndex() {
assertThrowsWithMessage("Index 2 is out of range [0, 2) (latch = 0, values = 2, errors = 0, completions = 1)", AssertionError.class, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public final U assertErrorMessage(String message) {
Throwable e = errors.get(0);
String errorMessage = e.getMessage();
if (!Objects.equals(message, errorMessage)) {
throw fail("Error message differs;\nexpected: " + message + "\ngot: " + errorMessage);
throw fail("\nexpected: " + message + "\ngot: " + errorMessage
+ "; Error message differs");
}
} else {
throw fail("Multiple errors");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ public final TestObserverEx<T> assertFusionMode(int mode) {
int m = establishedFusionMode;
if (m != mode) {
if (qd != null) {
throw new AssertionError("Fusion mode different.\nexpected: " + fusionModeToString(mode)
+ "\ngot: " + fusionModeToString(m));
throw new AssertionError("\nexpected: " + fusionModeToString(mode)
+ "\ngot: " + fusionModeToString(m) + "; Fusion mode different");
} else {
throw fail("Upstream is not fuseable");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ public final TestSubscriberEx<T> assertFusionMode(int mode) {
int m = establishedFusionMode;
if (m != mode) {
if (qs != null) {
throw new AssertionError("Fusion mode different. Expected: " + fusionModeToString(mode)
+ ", actual: " + fusionModeToString(m));
throw new AssertionError("\nexpected: " + fusionModeToString(mode)
+ "\ngot: " + fusionModeToString(m) + "; Fusion mode different");
} else {
throw fail("Upstream is not fuseable");
}
Expand Down

0 comments on commit c2bf514

Please sign in to comment.