From ef2c6f5e1c30e4ac64d34f5e5027dcb29e1b4b59 Mon Sep 17 00:00:00 2001 From: Yurii Dubinka Date: Thu, 10 Jan 2019 22:28:21 +0530 Subject: [PATCH] AllOf/AnyOf: Pass matchers to ctor using varargs --- hamcrest/src/main/java/org/hamcrest/core/AllOf.java | 5 +++++ hamcrest/src/main/java/org/hamcrest/core/AnyOf.java | 5 +++++ hamcrest/src/test/java/org/hamcrest/core/AllOfTest.java | 7 +++++++ hamcrest/src/test/java/org/hamcrest/core/AnyOfTest.java | 6 ++++++ 4 files changed, 23 insertions(+) diff --git a/hamcrest/src/main/java/org/hamcrest/core/AllOf.java b/hamcrest/src/main/java/org/hamcrest/core/AllOf.java index 06e999ad..b8c3faad 100644 --- a/hamcrest/src/main/java/org/hamcrest/core/AllOf.java +++ b/hamcrest/src/main/java/org/hamcrest/core/AllOf.java @@ -14,6 +14,11 @@ public class AllOf extends DiagnosingMatcher { private final Iterable> matchers; + @SafeVarargs + public AllOf(Matcher ... matchers) { + this(Arrays.asList(matchers)); + } + public AllOf(Iterable> matchers) { this.matchers = matchers; } diff --git a/hamcrest/src/main/java/org/hamcrest/core/AnyOf.java b/hamcrest/src/main/java/org/hamcrest/core/AnyOf.java index 03cc210e..7a22c22e 100644 --- a/hamcrest/src/main/java/org/hamcrest/core/AnyOf.java +++ b/hamcrest/src/main/java/org/hamcrest/core/AnyOf.java @@ -11,6 +11,11 @@ */ public class AnyOf extends ShortcutCombination { + @SafeVarargs + public AnyOf(Matcher ... matchers) { + this(Arrays.asList(matchers)); + } + public AnyOf(Iterable> matchers) { super(matchers); } diff --git a/hamcrest/src/test/java/org/hamcrest/core/AllOfTest.java b/hamcrest/src/test/java/org/hamcrest/core/AllOfTest.java index 48150020..0387207c 100644 --- a/hamcrest/src/test/java/org/hamcrest/core/AllOfTest.java +++ b/hamcrest/src/test/java/org/hamcrest/core/AllOfTest.java @@ -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; @@ -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("!"))); + } } diff --git a/hamcrest/src/test/java/org/hamcrest/core/AnyOfTest.java b/hamcrest/src/test/java/org/hamcrest/core/AnyOfTest.java index 11c053cf..e2d9a9ee 100644 --- a/hamcrest/src/test/java/org/hamcrest/core/AnyOfTest.java +++ b/hamcrest/src/test/java/org/hamcrest/core/AnyOfTest.java @@ -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; @@ -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("."))); + } }