package retromock.matchers;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import java.util.regex.Pattern;

public class IsRegex extends TypeSafeDiagnosingMatcher<String> {

    private final Pattern pattern;

    public static IsRegex matchesRegex(String regex) {
        return new IsRegex(Pattern.compile(regex));
    }

    public IsRegex(Pattern pattern) {
        this.pattern = pattern;
    }

    @Override
    protected boolean matchesSafely(String item, Description mismatchDescription) {
        return pattern.matcher(item).matches();
    }

    @Override
    public void describeTo(Description description) {
        description
                .appendText("a string matching the pattern '")
                .appendValue(pattern)
                .appendText("'");
    }
}

@@ -535,22 +535,39 @@
         
-return
+final boolean matches =
  pattern
@@ -592,16 +592,288 @@
 ches();%0A
+        if (!matches) %7B%0A            mismatchDescription%0A                    .appendText(%22Item %22)%0A                    .appendValue(item)%0A                    .appendText(%22 did not match pattern %22)%0A                    .appendValue(pattern);%0A        %7D%0A        return matches;%0A
     %7D%0A%0A 

Add describing text to mismatchDescription in case on mismatch