Skip to content

Commit

Permalink
2.x: Use predicates in BaseTestConsumer assertError(Class/Throwable) …
Browse files Browse the repository at this point in the history
…to remove duplicate code, tests tweaks to remove few IDE warnings (#4627)

* BaseTestConsumer use predicates in assertError with class and exception to remove duplicate code

* TestObserver & TestSubscriber tests cleanup to remove IDE warnings
  • Loading branch information
jraska authored and akarnokd committed Sep 28, 2016
1 parent 1c82063 commit a28b558
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 58 deletions.
37 changes: 3 additions & 34 deletions src/main/java/io/reactivex/observers/BaseTestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.CompositeException;
import io.reactivex.functions.Predicate;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.util.ExceptionHelper;

Expand Down Expand Up @@ -227,18 +228,7 @@ public final U assertNoErrors() {
*/
@SuppressWarnings("unchecked")
public final U assertError(Throwable error) {
int s = errors.size();
if (s == 0) {
throw fail("No errors");
}
if (errors.contains(error)) {
if (s != 1) {
throw fail("Error present but other errors as well");
}
} else {
throw fail("Error not present");
}
return (U)this;
return (U)assertError(Functions.equalsWith(error));
}

/**
Expand All @@ -249,28 +239,7 @@ public final U assertError(Throwable error) {
*/
@SuppressWarnings("unchecked")
public final U assertError(Class<? extends Throwable> errorClass) {
int s = errors.size();
if (s == 0) {
throw fail("No errors");
}

boolean found = false;

for (Throwable e : errors) {
if (errorClass.isInstance(e)) {
found = true;
break;
}
}

if (found) {
if (s != 1) {
throw fail("Error present but other errors as well");
}
} else {
throw fail("Error not present");
}
return (U)this;
return (U)assertError((Predicate)Functions.isInstanceOf(errorClass));
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/io/reactivex/observers/TestObserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ public void createDelegate() {
// expected
}

ts.assertValueSequence(Arrays.asList(1));
ts.assertValueSequence(Collections.singletonList(1));

try {
ts.assertValueSequence(Arrays.asList(2));
ts.assertValueSequence(Collections.singletonList(2));
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {
// expected
Expand Down Expand Up @@ -344,21 +344,21 @@ public void assertError() {
ts.assertErrorMessage("");
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {

// expected
}

try {
ts.assertSubscribed();
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {

// expected
}

try {
ts.assertTerminated();
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {

// expected
}

ts.onSubscribe(Disposables.empty());
Expand Down Expand Up @@ -390,7 +390,7 @@ public boolean test(Throwable t) throws Exception {
ts.assertErrorMessage("");
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {

// expected
}

try {
Expand Down Expand Up @@ -671,11 +671,11 @@ public void onNext() {

assertEquals(0, ts.valueCount());

assertEquals(Arrays.asList(), ts.values());
assertEquals(Collections.emptyList(), ts.values());

ts.onNext(1);

assertEquals(Arrays.asList(1), ts.values());
assertEquals(Collections.singletonList(1), ts.values());

ts.cancel();

Expand All @@ -684,11 +684,11 @@ public void onNext() {

ts.assertValue(1);

assertEquals(Arrays.asList(Arrays.asList(1), Collections.emptyList(), Collections.emptyList()), ts.getEvents());
assertEquals(Arrays.asList(Collections.singletonList(1), Collections.emptyList(), Collections.emptyList()), ts.getEvents());

ts.onComplete();

assertEquals(Arrays.asList(Arrays.asList(1), Collections.emptyList(), Collections.singletonList(Notification.createOnComplete())), ts.getEvents());
assertEquals(Arrays.asList(Collections.singletonList(1), Collections.emptyList(), Collections.singletonList(Notification.createOnComplete())), ts.getEvents());
}

@Test
Expand Down Expand Up @@ -896,14 +896,14 @@ public void assertValueSequence() {
ts.onNext(2);

try {
ts.assertValueSequence(Arrays.<Integer>asList());
ts.assertValueSequence(Collections.<Integer>emptyList());
throw new RuntimeException("Should have thrown");
} catch (AssertionError ex) {
// expected
}

try {
ts.assertValueSequence(Arrays.asList(1));
ts.assertValueSequence(Collections.singletonList(1));
throw new RuntimeException("Should have thrown");
} catch (AssertionError ex) {
// expected
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/io/reactivex/subscribers/TestSubscriberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,10 +738,10 @@ public void createDelegate() {
// expected
}

ts.assertValueSequence(Arrays.asList(1));
ts.assertValueSequence(Collections.singletonList(1));

try {
ts.assertValueSequence(Arrays.asList(2));
ts.assertValueSequence(Collections.singletonList(2));
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {
// expected
Expand Down Expand Up @@ -780,7 +780,7 @@ public void assertError() {
ts.assertErrorMessage("");
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {

// expected
}

try {
Expand All @@ -794,14 +794,14 @@ public void assertError() {
ts.assertSubscribed();
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {

// expected
}

try {
ts.assertTerminated();
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {

// expected
}

ts.onSubscribe(new BooleanSubscription());
Expand Down Expand Up @@ -833,7 +833,7 @@ public boolean test(Throwable t) {
ts.assertErrorMessage("");
throw new RuntimeException("Should have thrown");
} catch (AssertionError exc) {

// expected
}

try {
Expand Down Expand Up @@ -1113,11 +1113,11 @@ public void onNext() {

assertEquals(0, ts.valueCount());

assertEquals(Arrays.asList(), ts.values());
assertEquals(Collections.emptyList(), ts.values());

ts.onNext(1);

assertEquals(Arrays.asList(1), ts.values());
assertEquals(Collections.singletonList(1), ts.values());

ts.cancel();

Expand All @@ -1126,11 +1126,11 @@ public void onNext() {

ts.assertValue(1);

assertEquals(Arrays.asList(Arrays.asList(1), Collections.emptyList(), Collections.emptyList()), ts.getEvents());
assertEquals(Arrays.asList(Collections.singletonList(1), Collections.emptyList(), Collections.emptyList()), ts.getEvents());

ts.onComplete();

assertEquals(Arrays.asList(Arrays.asList(1), Collections.emptyList(), Collections.singletonList(Notification.createOnComplete())), ts.getEvents());
assertEquals(Arrays.asList(Collections.singletonList(1), Collections.emptyList(), Collections.singletonList(Notification.createOnComplete())), ts.getEvents());
}

@Test
Expand Down Expand Up @@ -1338,14 +1338,14 @@ public void assertValueSequence() {
ts.onNext(2);

try {
ts.assertValueSequence(Arrays.<Integer>asList());
ts.assertValueSequence(Collections.<Integer>emptyList());
throw new RuntimeException("Should have thrown");
} catch (AssertionError ex) {
// expected
}

try {
ts.assertValueSequence(Arrays.asList(1));
ts.assertValueSequence(Collections.singletonList(1));
throw new RuntimeException("Should have thrown");
} catch (AssertionError ex) {
// expected
Expand Down

0 comments on commit a28b558

Please sign in to comment.