2424public class LambdaMatchers {
2525
2626 private static class TransformMatcher <T , U > extends TypeSafeMatcher <T > {
27+ private final String transformDescription ;
2728 private final Matcher <U > matcher ;
2829 private final Function <T , U > transform ;
2930
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 ;
3133 this .matcher = matcher ;
3234 this .transform = transform ;
3335 }
@@ -53,25 +55,31 @@ protected void describeMismatchSafely(T item, Description description) {
5355 return ;
5456 }
5557
56- description .appendText ("transformed value " );
58+ description .appendText (transformDescription ). appendText ( " " );
5759 matcher .describeMismatch (u , description );
5860 }
5961
6062 @ Override
6163 public void describeTo (Description description ) {
62- description .appendText ("transformed to match " ).appendDescriptionOf (matcher );
64+ description .appendText (transformDescription ). appendText ( " matches " ).appendDescriptionOf (matcher );
6365 }
6466 }
6567
6668 public static <T , U > Matcher <T > transformedMatch (Function <T , U > function , Matcher <U > matcher ) {
67- return new TransformMatcher <>(matcher , function );
69+ return new TransformMatcher <>("transformed value" , matcher , function );
70+ }
71+
72+ public static <T , U > Matcher <T > transformedMatch (String description , Function <T , U > function , Matcher <U > matcher ) {
73+ return new TransformMatcher <>(description , matcher , function );
6874 }
6975
7076 private static class ListTransformMatcher <T , U > extends TypeSafeMatcher <Iterable <T >> {
77+ private final String transformDescription ;
7178 private final Matcher <Iterable <? extends U >> matcher ;
7279 private final Function <T , U > transform ;
7380
74- private ListTransformMatcher (Matcher <Iterable <? extends U >> matcher , Function <T , U > transform ) {
81+ private ListTransformMatcher (String transformDescription , Matcher <Iterable <? extends U >> matcher , Function <T , U > transform ) {
82+ this .transformDescription = transformDescription ;
7583 this .matcher = matcher ;
7684 this .transform = transform ;
7785 }
@@ -107,25 +115,35 @@ protected void describeMismatchSafely(Iterable<T> item, Description description)
107115 }
108116 }
109117
110- description .appendText ("transformed item " );
118+ description .appendText (transformDescription ). appendText ( " " );
111119 matcher .describeMismatch (us , description );
112120 }
113121
114122 @ Override
115123 public void describeTo (Description description ) {
116- description .appendText ("iterable with transformed items to match " ).appendDescriptionOf (matcher );
124+ description .appendText ("iterable with " ). appendText ( transformDescription ). appendText ( " matching " ).appendDescriptionOf (matcher );
117125 }
118126 }
119127
120128 public static <T , U > Matcher <Iterable <T >> transformedItemsMatch (Function <T , U > function , Matcher <Iterable <? extends U >> matcher ) {
121- return new ListTransformMatcher <>(matcher , function );
129+ return new ListTransformMatcher <>("transformed items" , matcher , function );
130+ }
131+
132+ public static <T , U > Matcher <Iterable <T >> transformedItemsMatch (
133+ String transformDescription ,
134+ Function <T , U > function ,
135+ Matcher <Iterable <? extends U >> matcher
136+ ) {
137+ return new ListTransformMatcher <>(transformDescription , matcher , function );
122138 }
123139
124140 private static class ArrayTransformMatcher <T , U > extends TypeSafeMatcher <T []> {
141+ private final String transformDescription ;
125142 private final Matcher <U []> matcher ;
126143 private final Function <T , U > transform ;
127144
128- private ArrayTransformMatcher (Matcher <U []> matcher , Function <T , U > transform ) {
145+ private ArrayTransformMatcher (String transformDescription , Matcher <U []> matcher , Function <T , U > transform ) {
146+ this .transformDescription = transformDescription ;
129147 this .matcher = matcher ;
130148 this .transform = transform ;
131149 }
@@ -174,18 +192,26 @@ protected void describeMismatchSafely(T[] item, Description description) {
174192 us [i ] = u ;
175193 }
176194
177- description .appendText ("transformed item " );
195+ description .appendText (transformDescription ). appendText ( " " );
178196 matcher .describeMismatch (us , description );
179197 }
180198
181199 @ Override
182200 public void describeTo (Description description ) {
183- description .appendText ("array with transformed items to match " ).appendDescriptionOf (matcher );
201+ description .appendText ("array with " ). appendText ( transformDescription ). appendText ( " matching " ).appendDescriptionOf (matcher );
184202 }
185203 }
186204
187205 public static <T , U > Matcher <T []> transformedArrayItemsMatch (Function <T , U > function , Matcher <U []> matcher ) {
188- return new ArrayTransformMatcher <>(matcher , function );
206+ return new ArrayTransformMatcher <>("transformed items" , matcher , function );
207+ }
208+
209+ public static <T , U > Matcher <T []> transformedArrayItemsMatch (
210+ String transformDescription ,
211+ Function <T , U > function ,
212+ Matcher <U []> matcher
213+ ) {
214+ return new ArrayTransformMatcher <>(transformDescription , matcher , function );
189215 }
190216
191217 private static class PredicateMatcher <T > extends BaseMatcher <Predicate <? super T >> {
0 commit comments