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