Skip to content

2.x: Use predicates in BaseTestConsumer assertError(Class/Throwable) to remove duplicate code, tests tweaks to remove few IDE warnings #4627

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

Merged
merged 2 commits into from
Sep 28, 2016
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
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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this change I can't run ./gradlew test locally any longer:

error: incompatible types: BaseTestConsumer cannot be converted to U
return 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