Skip to content

Commit

Permalink
AllOf/AnyOf: Pass matchers to ctor using varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroup authored and tumbarumba committed Feb 2, 2019
1 parent f337a05 commit ef2c6f5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/core/AllOf.java
Expand Up @@ -14,6 +14,11 @@ public class AllOf<T> extends DiagnosingMatcher<T> {

private final Iterable<Matcher<? super T>> matchers;

@SafeVarargs
public AllOf(Matcher<? super T> ... matchers) {
this(Arrays.asList(matchers));
}

public AllOf(Iterable<Matcher<? super T>> matchers) {
this.matchers = matchers;
}
Expand Down
5 changes: 5 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/core/AnyOf.java
Expand Up @@ -11,6 +11,11 @@
*/
public class AnyOf<T> extends ShortcutCombination<T> {

@SafeVarargs
public AnyOf(Matcher<? super T> ... matchers) {
this(Arrays.asList(matchers));
}

public AnyOf(Iterable<Matcher<? super T>> matchers) {
super(matchers);
}
Expand Down
7 changes: 7 additions & 0 deletions hamcrest/src/test/java/org/hamcrest/core/AllOfTest.java
Expand Up @@ -4,10 +4,12 @@
import org.junit.Test;

import static org.hamcrest.AbstractMatcherTest.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.core.StringEndsWith.endsWith;
import static org.hamcrest.core.StringStartsWith.startsWith;

Expand Down Expand Up @@ -60,4 +62,9 @@ public final class AllOfTest {
hasAMismatchDescriptionDescribingTheFirstFailingMatch() {
assertMismatchDescription("\"good\" was \"bad\"", allOf(equalTo("bad"), equalTo("good")), "bad");
}

@Test public void
varargs(){
assertThat("the text!", new AllOf<>(startsWith("the"), containsString("text"), endsWith("!")));
}
}
6 changes: 6 additions & 0 deletions hamcrest/src/test/java/org/hamcrest/core/AnyOfTest.java
Expand Up @@ -4,6 +4,7 @@
import org.junit.Test;

import static org.hamcrest.AbstractMatcherTest.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.StringEndsWith.endsWith;
Expand Down Expand Up @@ -53,4 +54,9 @@ public final class AnyOfTest {
assertDescription("(\"good\" or \"bad\" or \"ugly\")",
anyOf(equalTo("good"), equalTo("bad"), equalTo("ugly")));
}

@Test public void
varargs(){
assertThat("the text!", new AnyOf<>(startsWith("the"), endsWith(".")));
}
}

0 comments on commit ef2c6f5

Please sign in to comment.