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

Fix assert message format #7351

Merged
merged 2 commits into from
Oct 14, 2021
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
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, () -> {
akarnokd marked this conversation as resolved.
Show resolved Hide resolved
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