From fb67a1fc7c7a9828b6c74c1d44de22577a3243f9 Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Wed, 4 Oct 2017 15:38:07 +0200 Subject: [PATCH] IterableMatcher factory methods. #23 --- test-utils/build.gradle | 1 + .../testutils/{ => answers}/FailAnswer.java | 2 +- .../testutils/{ => doubles}/TestDoubles.java | 3 +- .../testutils/matchers/IterableMatcher.java | 76 +++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) rename test-utils/src/main/java/org/dmfs/testutils/{ => answers}/FailAnswer.java (96%) rename test-utils/src/main/java/org/dmfs/testutils/{ => doubles}/TestDoubles.java (94%) create mode 100644 test-utils/src/main/java/org/dmfs/testutils/matchers/IterableMatcher.java diff --git a/test-utils/build.gradle b/test-utils/build.gradle index 7118b630..e9abfdc0 100644 --- a/test-utils/build.gradle +++ b/test-utils/build.gradle @@ -10,6 +10,7 @@ configurations { apply from: '../publish.gradle' dependencies { + compile project(':iterators') compile 'junit:junit:' + JUNIT compile 'org.hamcrest:hamcrest-all:' + HAMCREST compile 'org.mockito:mockito-core:' + MOCKITO diff --git a/test-utils/src/main/java/org/dmfs/testutils/FailAnswer.java b/test-utils/src/main/java/org/dmfs/testutils/answers/FailAnswer.java similarity index 96% rename from test-utils/src/main/java/org/dmfs/testutils/FailAnswer.java rename to test-utils/src/main/java/org/dmfs/testutils/answers/FailAnswer.java index 5c2fcb48..708db836 100644 --- a/test-utils/src/main/java/org/dmfs/testutils/FailAnswer.java +++ b/test-utils/src/main/java/org/dmfs/testutils/answers/FailAnswer.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.dmfs.testutils; +package org.dmfs.testutils.answers; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; diff --git a/test-utils/src/main/java/org/dmfs/testutils/TestDoubles.java b/test-utils/src/main/java/org/dmfs/testutils/doubles/TestDoubles.java similarity index 94% rename from test-utils/src/main/java/org/dmfs/testutils/TestDoubles.java rename to test-utils/src/main/java/org/dmfs/testutils/doubles/TestDoubles.java index c38875ac..01c90df5 100644 --- a/test-utils/src/main/java/org/dmfs/testutils/TestDoubles.java +++ b/test-utils/src/main/java/org/dmfs/testutils/doubles/TestDoubles.java @@ -15,8 +15,9 @@ * limitations under the License. */ -package org.dmfs.testutils; +package org.dmfs.testutils.doubles; +import org.dmfs.testutils.answers.FailAnswer; import org.mockito.Mockito; diff --git a/test-utils/src/main/java/org/dmfs/testutils/matchers/IterableMatcher.java b/test-utils/src/main/java/org/dmfs/testutils/matchers/IterableMatcher.java new file mode 100644 index 00000000..b4af43b5 --- /dev/null +++ b/test-utils/src/main/java/org/dmfs/testutils/matchers/IterableMatcher.java @@ -0,0 +1,76 @@ +/* + * Copyright 2017 dmfs GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.testutils.matchers; + +import org.hamcrest.Factory; +import org.hamcrest.Matcher; +import org.hamcrest.collection.IsIterableContainingInOrder; + +import java.util.ArrayList; +import java.util.List; + + +/** + * Factory methods for {@link Iterable} matchers. + * + * @author Gabor Keszthelyi + */ +public final class IterableMatcher +{ + + /** + * {@link Matcher} that matches when the provided matchers match with the actual {@link Iterable}s elements in the same order. + */ + @Factory + public static Matcher> iteratesTo(Iterable> itemMatchers) + { + List> matchers = new ArrayList<>(); + for (Matcher itemMatcher : itemMatchers) + { + matchers.add(itemMatcher); + } + return new IsIterableContainingInOrder(matchers); + } + + + /** + * {@link Matcher} that matches when the actual {@link Iterable} has equal to elements in same order as the provided items. + */ + @SafeVarargs + @Factory + public static Matcher> iteratesTo(E... items) + { + return IsIterableContainingInOrder.contains(items); + } + + + /** + * {@link Matcher} that matches when the provided item matchers match the actual {@link Iterable}s elements in the same order. + */ + @SafeVarargs + @Factory + public static Matcher> iteratesTo(Matcher... itemMatchers) + { + return IsIterableContainingInOrder.contains(itemMatchers); + } + + + private IterableMatcher() + { + } +}