| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org;
|
|---|
| 3 |
|
|---|
| 4 | import org.hamcrest.Description;
|
|---|
| 5 | import org.hamcrest.Matcher;
|
|---|
| 6 | import org.hamcrest.TypeSafeMatcher;
|
|---|
| 7 | import org.junit.Ignore;
|
|---|
| 8 | import org.openstreetmap.josm.tools.Predicate;
|
|---|
| 9 |
|
|---|
| 10 | import java.util.Collection;
|
|---|
| 11 |
|
|---|
| 12 | @Ignore("no test")
|
|---|
| 13 | public class CustomMatchers {
|
|---|
| 14 |
|
|---|
| 15 | public static <T> Matcher<? extends T> forPredicate(final Predicate<T> predicate) {
|
|---|
| 16 | return new TypeSafeMatcher<T>() {
|
|---|
| 17 |
|
|---|
| 18 | @Override
|
|---|
| 19 | protected boolean matchesSafely(T item) {
|
|---|
| 20 | return predicate.evaluate(item);
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | @Override
|
|---|
| 24 | public void describeTo(Description description) {
|
|---|
| 25 | description.appendValue(predicate);
|
|---|
| 26 | }
|
|---|
| 27 | };
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | public static Matcher<Collection<?>> hasSize(final int size) {
|
|---|
| 31 | return new TypeSafeMatcher<Collection<?>>() {
|
|---|
| 32 | @Override
|
|---|
| 33 | protected boolean matchesSafely(Collection<?> collection) {
|
|---|
| 34 | return collection != null && collection.size() == size;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | @Override
|
|---|
| 38 | public void describeTo(Description description) {
|
|---|
| 39 | description.appendText("hasSize(").appendValue(size).appendText(")");
|
|---|
| 40 | }
|
|---|
| 41 | };
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | public static Matcher<Collection<?>> isEmpty() {
|
|---|
| 45 | return new TypeSafeMatcher<Collection<?>>() {
|
|---|
| 46 | @Override
|
|---|
| 47 | protected boolean matchesSafely(Collection<?> collection) {
|
|---|
| 48 | return collection != null && collection.isEmpty();
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | @Override
|
|---|
| 52 | public void describeTo(Description description) {
|
|---|
| 53 | description.appendText("isEmpty()");
|
|---|
| 54 | }
|
|---|
| 55 | };
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | }
|
|---|