[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 |
|
---|
[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.
|
---|
| 28 | * @param predicate the predicate
|
---|
| 29 | * @return matcher for a predicate
|
---|
| 30 | */
|
---|
[6533] | 31 | public static <T> Matcher<? extends T> forPredicate(final Predicate<T> predicate) {
|
---|
| 32 | return new TypeSafeMatcher<T>() {
|
---|
| 33 |
|
---|
| 34 | @Override
|
---|
| 35 | protected boolean matchesSafely(T item) {
|
---|
| 36 | return predicate.evaluate(item);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | @Override
|
---|
| 40 | public void describeTo(Description description) {
|
---|
| 41 | description.appendValue(predicate);
|
---|
| 42 | }
|
---|
| 43 | };
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[8857] | 46 | /**
|
---|
| 47 | * Matcher for a collection of a given size.
|
---|
| 48 | * @param size of collection
|
---|
| 49 | * @return matcher for a collection of a given size
|
---|
| 50 | */
|
---|
[6533] | 51 | public static Matcher<Collection<?>> hasSize(final int size) {
|
---|
| 52 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
| 53 | @Override
|
---|
| 54 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
| 55 | return collection != null && collection.size() == size;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | public void describeTo(Description description) {
|
---|
| 60 | description.appendText("hasSize(").appendValue(size).appendText(")");
|
---|
| 61 | }
|
---|
| 62 | };
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[8857] | 65 | /**
|
---|
| 66 | * Matcher for an empty collection.
|
---|
| 67 | * @return matcher for an empty collection
|
---|
| 68 | */
|
---|
[6533] | 69 | public static Matcher<Collection<?>> isEmpty() {
|
---|
| 70 | return new TypeSafeMatcher<Collection<?>>() {
|
---|
| 71 | @Override
|
---|
| 72 | protected boolean matchesSafely(Collection<?> collection) {
|
---|
| 73 | return collection != null && collection.isEmpty();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Override
|
---|
| 77 | public void describeTo(Description description) {
|
---|
| 78 | description.appendText("isEmpty()");
|
---|
| 79 | }
|
---|
| 80 | };
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[8857] | 83 | /**
|
---|
| 84 | * Matcher for a point at a given location.
|
---|
| 85 | * @param expected expected location
|
---|
| 86 | * @return matcher for a point at a given location
|
---|
| 87 | */
|
---|
[8759] | 88 | public static Matcher<? super Point2D> is(final Point2D expected) {
|
---|
| 89 | return new CustomTypeSafeMatcher<Point2D>("the same Point2D") {
|
---|
| 90 | @Override
|
---|
| 91 | protected boolean matchesSafely(Point2D actual) {
|
---|
| 92 | return expected.distance(actual) <= 0.0000001;
|
---|
| 93 | }
|
---|
| 94 | };
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[8857] | 97 | /**
|
---|
| 98 | * Matcher for a point at a given location.
|
---|
| 99 | * @param expected expected location
|
---|
| 100 | * @return matcher for a point at a given location
|
---|
| 101 | */
|
---|
[8759] | 102 | public static Matcher<? super LatLon> is(final LatLon expected) {
|
---|
| 103 | return new CustomTypeSafeMatcher<LatLon>("the same LatLon") {
|
---|
| 104 | @Override
|
---|
| 105 | protected boolean matchesSafely(LatLon actual) {
|
---|
| 106 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
| 107 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
| 108 | }
|
---|
| 109 | };
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[8857] | 112 | /**
|
---|
| 113 | * Matcher for a point at a given location.
|
---|
| 114 | * @param expected expected location
|
---|
| 115 | * @return matcher for a point at a given location
|
---|
| 116 | */
|
---|
[8759] | 117 | public static Matcher<? super EastNorth> is(final EastNorth expected) {
|
---|
| 118 | return new CustomTypeSafeMatcher<EastNorth>("the same EastNorth") {
|
---|
| 119 | @Override
|
---|
| 120 | protected boolean matchesSafely(EastNorth actual) {
|
---|
| 121 | return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
|
---|
| 122 | && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
|
---|
| 123 | }
|
---|
| 124 | };
|
---|
| 125 | }
|
---|
[6533] | 126 | }
|
---|