[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;
|
---|
[11867] | 6 | import java.util.Objects;
|
---|
[10691] | 7 | import java.util.function.Predicate;
|
---|
[8514] | 8 |
|
---|
[8759] | 9 | import org.hamcrest.CustomTypeSafeMatcher;
|
---|
[6533] | 10 | import org.hamcrest.Description;
|
---|
| 11 | import org.hamcrest.Matcher;
|
---|
| 12 | import org.hamcrest.TypeSafeMatcher;
|
---|
[6543] | 13 | import org.junit.Ignore;
|
---|
[8759] | 14 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[6533] | 16 |
|
---|
[8857] | 17 | /**
|
---|
| 18 | * Custom matchers for unit tests.
|
---|
| 19 | */
|
---|
[6543] | 20 | @Ignore("no test")
|
---|
[8514] | 21 | public final class CustomMatchers {
|
---|
[6533] | 22 |
|
---|
[8514] | 23 | private CustomMatchers() {
|
---|
| 24 | // Hide constructor for utility classes
|
---|
| 25 | }
|
---|
| 26 |
|
---|
[8857] | 27 | /**
|
---|
| 28 | * Matcher for a predicate.
|
---|
[9246] | 29 | * @param <T> type of elements
|
---|
[8857] | 30 | * @param predicate the predicate
|
---|
| 31 | * @return matcher for a predicate
|
---|
| 32 | */
|
---|
[6533] | 33 | public static <T> Matcher<? extends T> forPredicate(final Predicate<T> predicate) {
|
---|
| 34 | return new TypeSafeMatcher<T>() {
|
---|
| 35 |
|
---|
| 36 | @Override
|
---|
| 37 | protected boolean matchesSafely(T item) {
|
---|
[10691] | 38 | return predicate.test(item);
|
---|
[6533] | 39 | }
|
---|
| 40 |
|
---|
| 41 | @Override
|
---|
| 42 | public void describeTo(Description description) {
|
---|
| 43 | description.appendValue(predicate);
|
---|
| 44 | }
|
---|
| 45 | };
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[8857] | 48 | /**
|
---|
| 49 | * Matcher for a collection of a given size.
|
---|
| 50 | * @param size of collection
|
---|
| 51 | * @return matcher for a collection of a given size
|
---|
| 52 | */
|
---|
[6533] | 53 | public static Matcher<Collection<?>> hasSize(final int size) {
|
---|
| 54 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
| 55 | @Override
|
---|
| 56 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
| 57 | return collection != null && collection.size() == size;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | @Override
|
---|
| 61 | public void describeTo(Description description) {
|
---|
| 62 | description.appendText("hasSize(").appendValue(size).appendText(")");
|
---|
| 63 | }
|
---|
| 64 | };
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[8857] | 67 | /**
|
---|
| 68 | * Matcher for an empty collection.
|
---|
| 69 | * @return matcher for an empty collection
|
---|
| 70 | */
|
---|
[6533] | 71 | public static Matcher<Collection<?>> isEmpty() {
|
---|
| 72 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
| 73 | @Override
|
---|
| 74 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
| 75 | return collection != null && collection.isEmpty();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | @Override
|
---|
| 79 | public void describeTo(Description description) {
|
---|
| 80 | description.appendText("isEmpty()");
|
---|
| 81 | }
|
---|
| 82 | };
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[8857] | 85 | /**
|
---|
| 86 | * Matcher for a point at a given location.
|
---|
| 87 | * @param expected expected location
|
---|
| 88 | * @return matcher for a point at a given location
|
---|
| 89 | */
|
---|
[8759] | 90 | public static Matcher<? super Point2D> is(final Point2D expected) {
|
---|
[11867] | 91 | return new CustomTypeSafeMatcher<Point2D>(Objects.toString(expected)) {
|
---|
[8759] | 92 | @Override
|
---|
| 93 | protected boolean matchesSafely(Point2D actual) {
|
---|
| 94 | return expected.distance(actual) <= 0.0000001;
|
---|
| 95 | }
|
---|
| 96 | };
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[8857] | 99 | /**
|
---|
| 100 | * Matcher for a point at a given location.
|
---|
| 101 | * @param expected expected location
|
---|
| 102 | * @return matcher for a point at a given location
|
---|
| 103 | */
|
---|
[8759] | 104 | public static Matcher<? super LatLon> is(final LatLon expected) {
|
---|
[11867] | 105 | return new CustomTypeSafeMatcher<LatLon>(Objects.toString(expected)) {
|
---|
[8759] | 106 | @Override
|
---|
| 107 | protected boolean matchesSafely(LatLon actual) {
|
---|
| 108 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
| 109 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
| 110 | }
|
---|
| 111 | };
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[8857] | 114 | /**
|
---|
| 115 | * Matcher for a point at a given location.
|
---|
| 116 | * @param expected expected location
|
---|
| 117 | * @return matcher for a point at a given location
|
---|
| 118 | */
|
---|
[8759] | 119 | public static Matcher<? super EastNorth> is(final EastNorth expected) {
|
---|
[11867] | 120 | return new CustomTypeSafeMatcher<EastNorth>(Objects.toString(expected)) {
|
---|
[8759] | 121 | @Override
|
---|
| 122 | protected boolean matchesSafely(EastNorth actual) {
|
---|
| 123 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
| 124 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
| 125 | }
|
---|
| 126 | };
|
---|
| 127 | }
|
---|
[6533] | 128 | }
|
---|