[6543] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[6533] | 2 | package org;
|
---|
| 3 |
|
---|
[8759] | 4 | import java.awt.geom.Point2D;
|
---|
[8514] | 5 | import java.util.Collection;
|
---|
| 6 |
|
---|
[8759] | 7 | import org.hamcrest.CustomTypeSafeMatcher;
|
---|
[6533] | 8 | import org.hamcrest.Description;
|
---|
| 9 | import org.hamcrest.Matcher;
|
---|
| 10 | import org.hamcrest.TypeSafeMatcher;
|
---|
[6543] | 11 | import org.junit.Ignore;
|
---|
[8759] | 12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[6533] | 14 | import org.openstreetmap.josm.tools.Predicate;
|
---|
| 15 |
|
---|
[6543] | 16 | @Ignore("no test")
|
---|
[8514] | 17 | public final class CustomMatchers {
|
---|
[6533] | 18 |
|
---|
[8514] | 19 | private CustomMatchers() {
|
---|
| 20 | // Hide constructor for utility classes
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[6533] | 23 | public static <T> Matcher<? extends T> forPredicate(final Predicate<T> predicate) {
|
---|
| 24 | return new TypeSafeMatcher<T>() {
|
---|
| 25 |
|
---|
| 26 | @Override
|
---|
| 27 | protected boolean matchesSafely(T item) {
|
---|
| 28 | return predicate.evaluate(item);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | @Override
|
---|
| 32 | public void describeTo(Description description) {
|
---|
| 33 | description.appendValue(predicate);
|
---|
| 34 | }
|
---|
| 35 | };
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public static Matcher<Collection<?>> hasSize(final int size) {
|
---|
| 39 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
| 40 | @Override
|
---|
| 41 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
| 42 | return collection != null && collection.size() == size;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | @Override
|
---|
| 46 | public void describeTo(Description description) {
|
---|
| 47 | description.appendText("hasSize(").appendValue(size).appendText(")");
|
---|
| 48 | }
|
---|
| 49 | };
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public static Matcher<Collection<?>> isEmpty() {
|
---|
| 53 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
| 54 | @Override
|
---|
| 55 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
| 56 | return collection != null && collection.isEmpty();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | @Override
|
---|
| 60 | public void describeTo(Description description) {
|
---|
| 61 | description.appendText("isEmpty()");
|
---|
| 62 | }
|
---|
| 63 | };
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[8759] | 66 | public static Matcher<? super Point2D> is(final Point2D expected) {
|
---|
| 67 | return new CustomTypeSafeMatcher<Point2D>("the same Point2D") {
|
---|
| 68 | @Override
|
---|
| 69 | protected boolean matchesSafely(Point2D actual) {
|
---|
| 70 | return expected.distance(actual) <= 0.0000001;
|
---|
| 71 | }
|
---|
| 72 | };
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public static Matcher<? super LatLon> is(final LatLon expected) {
|
---|
| 76 | return new CustomTypeSafeMatcher<LatLon>("the same LatLon") {
|
---|
| 77 | @Override
|
---|
| 78 | protected boolean matchesSafely(LatLon actual) {
|
---|
| 79 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
| 80 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
| 81 | }
|
---|
| 82 | };
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public static Matcher<? super EastNorth> is(final EastNorth expected) {
|
---|
| 86 | return new CustomTypeSafeMatcher<EastNorth>("the same EastNorth") {
|
---|
| 87 | @Override
|
---|
| 88 | protected boolean matchesSafely(EastNorth actual) {
|
---|
| 89 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
| 90 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
| 91 | }
|
---|
| 92 | };
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[6533] | 95 | }
|
---|