1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org;
|
---|
3 |
|
---|
4 | import java.awt.geom.Point2D;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.Objects;
|
---|
7 | import java.util.function.Predicate;
|
---|
8 |
|
---|
9 | import org.hamcrest.CustomTypeSafeMatcher;
|
---|
10 | import org.hamcrest.Description;
|
---|
11 | import org.hamcrest.Matcher;
|
---|
12 | import org.hamcrest.TypeSafeMatcher;
|
---|
13 | import org.junit.Ignore;
|
---|
14 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Custom matchers for unit tests.
|
---|
19 | */
|
---|
20 | @Ignore("no test")
|
---|
21 | public final class CustomMatchers {
|
---|
22 |
|
---|
23 | private CustomMatchers() {
|
---|
24 | // Hide constructor for utility classes
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Matcher for a predicate.
|
---|
29 | * @param <T> type of elements
|
---|
30 | * @param predicate the predicate
|
---|
31 | * @return matcher for a predicate
|
---|
32 | */
|
---|
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) {
|
---|
38 | return predicate.test(item);
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void describeTo(Description description) {
|
---|
43 | description.appendValue(predicate);
|
---|
44 | }
|
---|
45 | };
|
---|
46 | }
|
---|
47 |
|
---|
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 | */
|
---|
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 |
|
---|
67 | /**
|
---|
68 | * Matcher for an empty collection.
|
---|
69 | * @return matcher for an empty collection
|
---|
70 | */
|
---|
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 |
|
---|
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 | */
|
---|
90 | public static Matcher<? super Point2D> is(final Point2D expected) {
|
---|
91 | return new CustomTypeSafeMatcher<Point2D>(Objects.toString(expected)) {
|
---|
92 | @Override
|
---|
93 | protected boolean matchesSafely(Point2D actual) {
|
---|
94 | return expected.distance(actual) <= 0.0000001;
|
---|
95 | }
|
---|
96 | };
|
---|
97 | }
|
---|
98 |
|
---|
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 | */
|
---|
104 | public static Matcher<? super LatLon> is(final LatLon expected) {
|
---|
105 | return new CustomTypeSafeMatcher<LatLon>(Objects.toString(expected)) {
|
---|
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 |
|
---|
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 | */
|
---|
119 | public static Matcher<? super EastNorth> is(final EastNorth expected) {
|
---|
120 | return new CustomTypeSafeMatcher<EastNorth>(Objects.toString(expected)) {
|
---|
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 | }
|
---|
128 | }
|
---|