|
24 | 24 | public class LambdaMatchers { |
25 | 25 |
|
26 | 26 | private static class TransformMatcher<T, U> extends BaseMatcher<T> { |
| 27 | + private final String transformDescription; |
27 | 28 | private final Matcher<U> matcher; |
28 | 29 | private final Function<T, U> transform; |
29 | 30 |
|
30 | | - private TransformMatcher(Matcher<U> matcher, Function<T, U> transform) { |
| 31 | + private TransformMatcher(String transformDescription, Matcher<U> matcher, Function<T, U> transform) { |
| 32 | + this.transformDescription = transformDescription; |
31 | 33 | this.matcher = matcher; |
32 | 34 | this.transform = transform; |
33 | 35 | } |
@@ -56,25 +58,31 @@ public void describeMismatch(Object item, Description description) { |
56 | 58 | return; |
57 | 59 | } |
58 | 60 |
|
59 | | - description.appendText("transformed value "); |
| 61 | + description.appendText(transformDescription).appendText(" "); |
60 | 62 | matcher.describeMismatch(u, description); |
61 | 63 | } |
62 | 64 |
|
63 | 65 | @Override |
64 | 66 | public void describeTo(Description description) { |
65 | | - description.appendText("transformed to match ").appendDescriptionOf(matcher); |
| 67 | + description.appendText(transformDescription).appendText(" matches ").appendDescriptionOf(matcher); |
66 | 68 | } |
67 | 69 | } |
68 | 70 |
|
69 | 71 | public static <T, U> Matcher<T> transformedMatch(Function<T, U> function, Matcher<U> matcher) { |
70 | | - return new TransformMatcher<>(matcher, function); |
| 72 | + return new TransformMatcher<>("transformed value", matcher, function); |
| 73 | + } |
| 74 | + |
| 75 | + public static <T, U> Matcher<T> transformedMatch(String description, Function<T, U> function, Matcher<U> matcher) { |
| 76 | + return new TransformMatcher<>(description, matcher, function); |
71 | 77 | } |
72 | 78 |
|
73 | 79 | private static class ListTransformMatcher<T, U> extends TypeSafeMatcher<Iterable<T>> { |
| 80 | + private final String transformDescription; |
74 | 81 | private final Matcher<Iterable<? extends U>> matcher; |
75 | 82 | private final Function<T, U> transform; |
76 | 83 |
|
77 | | - private ListTransformMatcher(Matcher<Iterable<? extends U>> matcher, Function<T, U> transform) { |
| 84 | + private ListTransformMatcher(String transformDescription, Matcher<Iterable<? extends U>> matcher, Function<T, U> transform) { |
| 85 | + this.transformDescription = transformDescription; |
78 | 86 | this.matcher = matcher; |
79 | 87 | this.transform = transform; |
80 | 88 | } |
@@ -110,25 +118,35 @@ protected void describeMismatchSafely(Iterable<T> item, Description description) |
110 | 118 | } |
111 | 119 | } |
112 | 120 |
|
113 | | - description.appendText("transformed item "); |
| 121 | + description.appendText(transformDescription).appendText(" "); |
114 | 122 | matcher.describeMismatch(us, description); |
115 | 123 | } |
116 | 124 |
|
117 | 125 | @Override |
118 | 126 | public void describeTo(Description description) { |
119 | | - description.appendText("iterable with transformed items to match ").appendDescriptionOf(matcher); |
| 127 | + description.appendText("iterable with ").appendText(transformDescription).appendText(" matching ").appendDescriptionOf(matcher); |
120 | 128 | } |
121 | 129 | } |
122 | 130 |
|
123 | 131 | public static <T, U> Matcher<Iterable<T>> transformedItemsMatch(Function<T, U> function, Matcher<Iterable<? extends U>> matcher) { |
124 | | - return new ListTransformMatcher<>(matcher, function); |
| 132 | + return new ListTransformMatcher<>("transformed items", matcher, function); |
| 133 | + } |
| 134 | + |
| 135 | + public static <T, U> Matcher<Iterable<T>> transformedItemsMatch( |
| 136 | + String transformDescription, |
| 137 | + Function<T, U> function, |
| 138 | + Matcher<Iterable<? extends U>> matcher |
| 139 | + ) { |
| 140 | + return new ListTransformMatcher<>(transformDescription, matcher, function); |
125 | 141 | } |
126 | 142 |
|
127 | 143 | private static class ArrayTransformMatcher<T, U> extends TypeSafeMatcher<T[]> { |
| 144 | + private final String transformDescription; |
128 | 145 | private final Matcher<U[]> matcher; |
129 | 146 | private final Function<T, U> transform; |
130 | 147 |
|
131 | | - private ArrayTransformMatcher(Matcher<U[]> matcher, Function<T, U> transform) { |
| 148 | + private ArrayTransformMatcher(String transformDescription, Matcher<U[]> matcher, Function<T, U> transform) { |
| 149 | + this.transformDescription = transformDescription; |
132 | 150 | this.matcher = matcher; |
133 | 151 | this.transform = transform; |
134 | 152 | } |
@@ -177,18 +195,26 @@ protected void describeMismatchSafely(T[] item, Description description) { |
177 | 195 | us[i] = u; |
178 | 196 | } |
179 | 197 |
|
180 | | - description.appendText("transformed item "); |
| 198 | + description.appendText(transformDescription).appendText(" "); |
181 | 199 | matcher.describeMismatch(us, description); |
182 | 200 | } |
183 | 201 |
|
184 | 202 | @Override |
185 | 203 | public void describeTo(Description description) { |
186 | | - description.appendText("array with transformed items to match ").appendDescriptionOf(matcher); |
| 204 | + description.appendText("array with ").appendText(transformDescription).appendText(" matching ").appendDescriptionOf(matcher); |
187 | 205 | } |
188 | 206 | } |
189 | 207 |
|
190 | 208 | public static <T, U> Matcher<T[]> transformedArrayItemsMatch(Function<T, U> function, Matcher<U[]> matcher) { |
191 | | - return new ArrayTransformMatcher<>(matcher, function); |
| 209 | + return new ArrayTransformMatcher<>("transformed items", matcher, function); |
| 210 | + } |
| 211 | + |
| 212 | + public static <T, U> Matcher<T[]> transformedArrayItemsMatch( |
| 213 | + String transformDescription, |
| 214 | + Function<T, U> function, |
| 215 | + Matcher<U[]> matcher |
| 216 | + ) { |
| 217 | + return new ArrayTransformMatcher<>(transformDescription, matcher, function); |
192 | 218 | } |
193 | 219 |
|
194 | 220 | private static class PredicateMatcher<T> extends BaseMatcher<Predicate<? super T>> { |
|
0 commit comments