source: josm/trunk/test/unit/org/CustomMatchers.java@ 8811

Last change on this file since 8811 was 8759, checked in by simon04, 9 years ago

fix #11849 - Add NavigationComponentTest (patch by michael2402, modified)

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org;
3
4import java.awt.geom.Point2D;
5import java.util.Collection;
6
7import org.hamcrest.CustomTypeSafeMatcher;
8import org.hamcrest.Description;
9import org.hamcrest.Matcher;
10import org.hamcrest.TypeSafeMatcher;
11import org.junit.Ignore;
12import org.openstreetmap.josm.data.coor.EastNorth;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.tools.Predicate;
15
16@Ignore("no test")
17public final class CustomMatchers {
18
19 private CustomMatchers() {
20 // Hide constructor for utility classes
21 }
22
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
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
95}
Note: See TracBrowser for help on using the repository browser.