org.hamcrest.core.DescribedAs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-all Show documentation
Show all versions of mockito-all Show documentation
Mock objects library for java
/* Copyright (c) 2000-2006 hamcrest.org
*/
package org.hamcrest.core;
import java.util.regex.Pattern;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Factory;
import org.hamcrest.BaseMatcher;
/**
* Provides a custom description to another matcher.
*/
public class DescribedAs extends BaseMatcher {
private final String descriptionTemplate;
private final Matcher matcher;
private final Object[] values;
private final static Pattern ARG_PATTERN = Pattern.compile("%([0-9]+)");
public DescribedAs(String descriptionTemplate, Matcher matcher, Object[] values) {
this.descriptionTemplate = descriptionTemplate;
this.matcher = matcher;
this.values = values.clone();
}
public boolean matches(Object o) {
return matcher.matches(o);
}
public void describeTo(Description description) {
java.util.regex.Matcher arg = ARG_PATTERN.matcher(descriptionTemplate);
int textStart = 0;
while (arg.find()) {
description.appendText(descriptionTemplate.substring(textStart, arg.start()));
int argIndex = Integer.parseInt(arg.group(1));
description.appendValue(values[argIndex]);
textStart = arg.end();
}
if (textStart < descriptionTemplate.length()) {
description.appendText(descriptionTemplate.substring(textStart));
}
}
/**
* Wraps an existing matcher and overrides the description when it fails.
*/
@Factory
public static Matcher describedAs(String description, Matcher matcher, Object... values) {
return new DescribedAs(description, matcher, values);
}
}