Skip to content

Commit

Permalink
Test error messages against regex
Browse files Browse the repository at this point in the history
  • Loading branch information
keytronic committed Oct 14, 2021
1 parent 1735d0d commit a97a69b
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions 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 @@ -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

0 comments on commit a97a69b

Please sign in to comment.