| 1 |
|
| 2 |
package org; |
| 3 |
|
| 4 |
import java.awt.geom.Point2D; |
| 5 |
import java.util.Collection; |
| 6 |
import java.util.Locale; |
| 7 |
import java.util.Objects; |
| 8 |
import java.util.function.Predicate; |
| 9 |
|
| 10 |
import org.hamcrest.CustomTypeSafeMatcher; |
| 11 |
import org.hamcrest.Description; |
| 12 |
import org.hamcrest.Matcher; |
| 13 |
import org.hamcrest.TypeSafeMatcher; |
| 14 |
import org.junit.jupiter.api.Disabled; |
| 15 |
import org.openstreetmap.josm.data.Bounds; |
| 16 |
import org.openstreetmap.josm.data.coor.EastNorth; |
| 17 |
import org.openstreetmap.josm.data.coor.LatLon; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
@Disabled("no test") |
| 23 |
public final class CustomMatchers { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public enum ErrorMode { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
ABSOLUTE, |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
RELATIVE |
| 38 |
} |
| 39 |
|
| 40 |
private CustomMatchers() { |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static <T> Matcher<? extends T> forPredicate(final Predicate<T> predicate) { |
| 51 |
return new TypeSafeMatcher<T>() { |
| 52 |
|
| 53 |
@Override |
| 54 |
protected boolean matchesSafely(T item) { |
| 55 |
return predicate.test(item); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void describeTo(Description description) { |
| 60 |
description.appendValue(predicate); |
| 61 |
} |
| 62 |
}; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static Matcher<Collection<?>> hasSize(final int size) { |
| 71 |
return new TypeSafeMatcher<Collection<?>>() { |
| 72 |
@Override |
| 73 |
protected boolean matchesSafely(Collection<?> collection) { |
| 74 |
return collection != null && collection.size() == size; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void describeTo(Description description) { |
| 79 |
description.appendText("hasSize(").appendValue(size).appendText(")"); |
| 80 |
} |
| 81 |
}; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public static Matcher<Collection<?>> isEmpty() { |
| 89 |
return new TypeSafeMatcher<Collection<?>>() { |
| 90 |
@Override |
| 91 |
protected boolean matchesSafely(Collection<?> collection) { |
| 92 |
return collection != null && collection.isEmpty(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void describeTo(Description description) { |
| 97 |
description.appendText("isEmpty()"); |
| 98 |
} |
| 99 |
}; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static Matcher<? super Point2D> is(final Point2D expected) { |
| 108 |
return new CustomTypeSafeMatcher<Point2D>(Objects.toString(expected)) { |
| 109 |
@Override |
| 110 |
protected boolean matchesSafely(Point2D actual) { |
| 111 |
return expected.distance(actual) <= 0.0000001; |
| 112 |
} |
| 113 |
}; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static Matcher<? super LatLon> is(final LatLon expected) { |
| 122 |
return new CustomTypeSafeMatcher<LatLon>(Objects.toString(expected)) { |
| 123 |
@Override |
| 124 |
protected boolean matchesSafely(LatLon actual) { |
| 125 |
return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION |
| 126 |
&& Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION; |
| 127 |
} |
| 128 |
}; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public static Matcher<? super EastNorth> is(final EastNorth expected) { |
| 137 |
return new CustomTypeSafeMatcher<EastNorth>(Objects.toString(expected)) { |
| 138 |
@Override |
| 139 |
protected boolean matchesSafely(EastNorth actual) { |
| 140 |
return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION |
| 141 |
&& Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION; |
| 142 |
} |
| 143 |
}; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static Matcher<Bounds> is(final Bounds expected, double tolerance) { |
| 153 |
return new TypeSafeMatcher<Bounds>() { |
| 154 |
@Override |
| 155 |
public void describeTo(Description description) { |
| 156 |
description.appendText("is ") |
| 157 |
.appendValue(expected) |
| 158 |
.appendText(" (tolerance: " + tolerance + ")"); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
protected void describeMismatchSafely(Bounds bounds, Description mismatchDescription) { |
| 163 |
mismatchDescription.appendText("was ").appendValue(bounds); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
protected boolean matchesSafely(Bounds bounds) { |
| 168 |
return Math.abs(expected.getMinLon() - bounds.getMinLon()) <= tolerance && |
| 169 |
Math.abs(expected.getMinLat() - bounds.getMinLat()) <= tolerance && |
| 170 |
Math.abs(expected.getMaxLon() - bounds.getMaxLon()) <= tolerance && |
| 171 |
Math.abs(expected.getMaxLat() - bounds.getMaxLat()) <= tolerance; |
| 172 |
} |
| 173 |
}; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public static Matcher<Double> isFP(final double expected, ErrorMode errorMode, double tolerance) { |
| 184 |
return new TypeSafeMatcher<Double>() { |
| 185 |
@Override |
| 186 |
public void describeTo(Description description) { |
| 187 |
description.appendText("is ") |
| 188 |
.appendValue(expected) |
| 189 |
.appendText(" (tolerance") |
| 190 |
.appendText(errorMode == ErrorMode.RELATIVE ? ", relative:" : ":") |
| 191 |
.appendText(Double.toString(tolerance)) |
| 192 |
.appendText(")"); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
protected void describeMismatchSafely(Double was, Description mismatchDescription) { |
| 197 |
mismatchDescription.appendText("was ").appendValue(was); |
| 198 |
if (errorMode == ErrorMode.RELATIVE) { |
| 199 |
mismatchDescription.appendText(" (actual relative error: ") |
| 200 |
.appendText(String.format(Locale.US, "%.2e", Math.abs((was - expected) / expected))) |
| 201 |
.appendText(")"); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
protected boolean matchesSafely(Double x) { |
| 207 |
switch (errorMode) { |
| 208 |
case ABSOLUTE: |
| 209 |
return Math.abs(x - expected) <= tolerance; |
| 210 |
case RELATIVE: |
| 211 |
return Math.abs((x - expected) / expected) <= tolerance; |
| 212 |
default: |
| 213 |
throw new AssertionError(); |
| 214 |
} |
| 215 |
} |
| 216 |
}; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public static Matcher<Double> isFP(final double expected, double tolerance) { |
| 226 |
return isFP(expected, ErrorMode.ABSOLUTE, tolerance); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public static Matcher<Double> isFP(final double expected) { |
| 235 |
return isFP(expected, 1e-8); |
| 236 |
} |
| 237 |
} |