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) {
        final boolean matches = pattern.matcher(item).matches();
        if (!matches) {
            mismatchDescription
                    .appendText("Item ")
                    .appendValue(item)
                    .appendText(" did not match pattern ")
                    .appendValue(pattern);
        }
        return matches;
    }

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

@@ -716,21 +716,20 @@
  .append
-Value
+Text
 (item)%0A 
@@ -813,29 +813,38 @@
  .append
-Value(
+Text(pattern.
 pattern
+()
 );%0A     
@@ -1026,17 +1026,16 @@
 pattern 
-'
 %22)%0A     
@@ -1056,21 +1056,20 @@
 pend
-Value
+Text
 (pattern
 )%0A  
@@ -1068,41 +1068,18 @@
 tern
-)%0A                .appendText(%22'%22
+.pattern()
 );%0A 

adjusted Description