File: CustomMatchers.java

package info (click to toggle)
josm 0.0.svn17428%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 204,760 kB
  • sloc: java: 360,765; xml: 198,972; perl: 10,138; jsp: 250; sh: 123; makefile: 112; javascript: 74; python: 29
file content (237 lines) | stat: -rw-r--r-- 8,325 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// License: GPL. For details, see LICENSE file.
package org;

import java.awt.geom.Point2D;
import java.util.Collection;
import java.util.Locale;
import java.util.Objects;
import java.util.function.Predicate;

import org.hamcrest.CustomTypeSafeMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.jupiter.api.Disabled;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.coor.EastNorth;
import org.openstreetmap.josm.data.coor.LatLon;

/**
 * Custom matchers for unit tests.
 */
@Disabled("no test")
public final class CustomMatchers {

    /**
     * Error mode, denoting different ways to calculate the error of a number relative to an expected value.
     */
    public enum ErrorMode {
        /**
         * absolute error (difference of actual and expected value)
         */
        ABSOLUTE,

        /**
         * relative error (difference divided by the expected value)
         */
        RELATIVE
    }

    private CustomMatchers() {
        // Hide constructor for utility classes
    }

    /**
     * Matcher for a predicate.
     * @param <T> type of elements
     * @param predicate the predicate
     * @return matcher for a predicate
     */
    public static <T> Matcher<? extends T> forPredicate(final Predicate<T> predicate) {
        return new TypeSafeMatcher<T>() {

            @Override
            protected boolean matchesSafely(T item) {
                return predicate.test(item);
            }

            @Override
            public void describeTo(Description description) {
                description.appendValue(predicate);
            }
        };
    }

    /**
     * Matcher for a collection of a given size.
     * @param size of collection
     * @return matcher for a collection of a given size
     */
    public static Matcher<Collection<?>> hasSize(final int size) {
        return new TypeSafeMatcher<Collection<?>>() {
            @Override
            protected boolean matchesSafely(Collection<?> collection) {
                return collection != null && collection.size() == size;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("hasSize(").appendValue(size).appendText(")");
            }
        };
    }

    /**
     * Matcher for an empty collection.
     * @return matcher for an empty collection
     */
    public static Matcher<Collection<?>> isEmpty() {
        return new TypeSafeMatcher<Collection<?>>() {
            @Override
            protected boolean matchesSafely(Collection<?> collection) {
                return collection != null && collection.isEmpty();
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("isEmpty()");
            }
        };
    }

    /**
     * Matcher for a point at a given location.
     * @param expected expected location
     * @return matcher for a point at a given location
     */
    public static Matcher<? super Point2D> is(final Point2D expected) {
        return new CustomTypeSafeMatcher<Point2D>(Objects.toString(expected)) {
            @Override
            protected boolean matchesSafely(Point2D actual) {
                return expected.distance(actual) <= 0.0000001;
            }
        };
    }

    /**
     * Matcher for a point at a given location.
     * @param expected expected location
     * @return matcher for a point at a given location
     */
    public static Matcher<? super LatLon> is(final LatLon expected) {
        return new CustomTypeSafeMatcher<LatLon>(Objects.toString(expected)) {
            @Override
            protected boolean matchesSafely(LatLon actual) {
                return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
                        && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
            }
        };
    }

    /**
     * Matcher for a point at a given location.
     * @param expected expected location
     * @return matcher for a point at a given location
     */
    public static Matcher<? super EastNorth> is(final EastNorth expected) {
        return new CustomTypeSafeMatcher<EastNorth>(Objects.toString(expected)) {
            @Override
            protected boolean matchesSafely(EastNorth actual) {
                return Math.abs(expected.getX() - actual.getX()) <= LatLon.MAX_SERVER_PRECISION
                        && Math.abs(expected.getY() - actual.getY()) <= LatLon.MAX_SERVER_PRECISION;
            }
        };
    }

    /**
     * Matcher for a {@link Bounds} object
     * @param expected expected bounds
     * @param tolerance acceptable deviation (epsilon)
     * @return Matcher for a {@link Bounds} object
     */
    public static Matcher<Bounds> is(final Bounds expected, double tolerance) {
        return new TypeSafeMatcher<Bounds>() {
           @Override
           public void describeTo(Description description) {
              description.appendText("is ")
                      .appendValue(expected)
                      .appendText(" (tolerance: " + tolerance + ")");
           }

           @Override
           protected void describeMismatchSafely(Bounds bounds, Description mismatchDescription) {
              mismatchDescription.appendText("was ").appendValue(bounds);
           }

           @Override
           protected boolean matchesSafely(Bounds bounds) {
              return Math.abs(expected.getMinLon() - bounds.getMinLon()) <= tolerance &&
                    Math.abs(expected.getMinLat() - bounds.getMinLat()) <= tolerance &&
                    Math.abs(expected.getMaxLon() - bounds.getMaxLon()) <= tolerance &&
                    Math.abs(expected.getMaxLat() - bounds.getMaxLat()) <= tolerance;
           }
        };
    }

    /**
     * Matcher for a floating point number.
     * @param expected expected value
     * @param errorMode the error mode
     * @param tolerance admissible error
     * @return Matcher for a floating point number
     */
    public static Matcher<Double> isFP(final double expected, ErrorMode errorMode, double tolerance) {
        return new TypeSafeMatcher<Double>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("is ")
                        .appendValue(expected)
                        .appendText(" (tolerance")
                        .appendText(errorMode == ErrorMode.RELATIVE ? ", relative:" : ":")
                        .appendText(Double.toString(tolerance))
                        .appendText(")");
            }

            @Override
            protected void describeMismatchSafely(Double was, Description mismatchDescription) {
                mismatchDescription.appendText("was ").appendValue(was);
                if (errorMode == ErrorMode.RELATIVE) {
                    mismatchDescription.appendText(" (actual relative error: ")
                            .appendText(String.format(Locale.US, "%.2e", Math.abs((was - expected) / expected)))
                            .appendText(")");
                }
            }

            @Override
            protected boolean matchesSafely(Double x) {
                switch (errorMode) {
                    case ABSOLUTE:
                        return Math.abs(x - expected) <= tolerance;
                    case RELATIVE:
                        return Math.abs((x - expected) / expected) <= tolerance;
                    default:
                        throw new AssertionError();
                }
            }
        };
    }

    /**
     * Matcher for a floating point number.
     * @param expected expected value
     * @param tolerance admissible error (absolute)
     * @return Matcher for a floating point number
     */
    public static Matcher<Double> isFP(final double expected, double tolerance) {
        return isFP(expected, ErrorMode.ABSOLUTE, tolerance);
    }

    /**
     * Matcher for a floating point number.
     * @param expected expected value
     * @return Matcher for a floating point number
     */
    public static Matcher<Double> isFP(final double expected) {
        return isFP(expected, 1e-8);
    }
}