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 type signature of ExpectedException#except() #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/main/java/org/hamcrest/junit/ExpectedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public Statement apply(Statement base,
* throw e;
* }</pre>
*/
public void expect(Matcher<?> matcher) {
public void expect(Matcher<? super Throwable> matcher) {
matcherBuilder.add(matcher);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.hamcrest.junit;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.junit.JUnitMatchers.isThrowable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -13,34 +12,18 @@
*/
class ExpectedExceptionMatcherBuilder {

private final List<Matcher<?>> matchers = new ArrayList<Matcher<?>>();
private final List<Matcher<? super Throwable>> matchers = new ArrayList<>();

void add(Matcher<?> matcher) {
void add(Matcher<? super Throwable> matcher) {
matchers.add(matcher);
}

boolean expectsThrowable() {
return !matchers.isEmpty();
}

Matcher<Throwable> build() {
return isThrowable(allOfTheMatchers());
Matcher<? super Throwable> build() {
return allOf(matchers);
}

private Matcher<Throwable> allOfTheMatchers() {
if (matchers.size() == 1) {
return cast(matchers.get(0));
}
return allOf(castedMatchers());
}

@SuppressWarnings({"unchecked", "rawtypes"})
private List<Matcher<? super Throwable>> castedMatchers() {
return new ArrayList<Matcher<? super Throwable>>((List) matchers);
}

@SuppressWarnings("unchecked")
private Matcher<Throwable> cast(Matcher<?> singleMatcher) {
return (Matcher<Throwable>) singleMatcher;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static class ExpectsMatcher {

@Test
public void throwsMore() {
thrown.expect(any(Exception.class));
thrown.expect(instanceOf(Exception.class));
throw new NullPointerException("Ack!");
}
}
Expand Down