下面列出了org.hamcrest.Description#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public String toString() {
if (invalidMatcherList.isEmpty()) {
String validMatchers =
featureMatcherList.stream()
.map(Pair::getLeft)
.map(BaseMatcher::toString)
.collect(Collectors.joining(","));
return "All matchers suceeded: " + validMatchers;
}
Description description = new StringDescription();
// result.append("A ClientCall with the following properties:");
for (Pair<FeatureMatcher, Object> pair : invalidMatcherList) {
FeatureMatcher invalidMatcher = pair.getLeft();
description.appendText("Expecting '");
invalidMatcher.describeTo(description);
description.appendText("', but got values:").appendValue(pair.getRight());
}
return description.toString();
}
public static <T> boolean verifyThat(String reason, T actual, Matcher<? super T> matcher) {
boolean result = matcher.matches(actual);
Description description = new StringDescription();
description.appendText(reason).appendText("\nExpected: ").appendDescriptionOf(matcher)
.appendText("\n Actual: ");
matcher.describeMismatch(actual, description);
String msg = description.toString();
if (msg.endsWith("Actual: ")) {
msg = String.format(msg + "%s", actual);
}
msg = msg.replaceAll("<", "<").replaceAll(">", ">");
Reporter.log(msg, result ? MessageTypes.Pass : MessageTypes.Fail);
return result;
}
/**
* A replacement for MatcherAssert.assertThat that renders View objects nicely.
*
* @param message the message to display.
* @param actual the actual value.
* @param matcher a matcher that accepts or rejects actual.
*/
public static <T> void assertThat(String message, T actual, Matcher<T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description
.appendText(message)
.appendText("\nExpected: ")
.appendDescriptionOf(matcher)
.appendText("\n Got: ");
if (actual instanceof View) {
description.appendValue(HumanReadables.describe((View) actual));
} else {
description.appendValue(actual);
}
description.appendText("\n");
throw new AssertionFailedError(description.toString());
}
}
/**
* Assert that the given class adheres to the immutable class rules.
*
* @param clazz the class to check
*
* @throws java.lang.AssertionError if the class is not an
* immutable class
*/
public static void assertThatClassIsImmutable(Class<?> clazz) {
final ImmutableClassChecker checker = new ImmutableClassChecker();
if (!checker.isImmutableClass(clazz, false)) {
final Description toDescription = new StringDescription();
final Description mismatchDescription = new StringDescription();
checker.describeTo(toDescription);
checker.describeMismatch(mismatchDescription);
final String reason =
"\n" +
"Expected: is \"" + toDescription.toString() + "\"\n" +
" but : was \"" + mismatchDescription.toString() + "\"";
throw new AssertionError(reason);
}
}
/**
* Assert that the given class adheres to the immutable class rules, but
* is not declared final. Classes that need to be inherited from cannot be
* declared final.
*
* @param clazz the class to check
*
* @throws java.lang.AssertionError if the class is not an
* immutable class
*/
public static void assertThatClassIsImmutableBaseClass(Class<?> clazz) {
final ImmutableClassChecker checker = new ImmutableClassChecker();
if (!checker.isImmutableClass(clazz, true)) {
final Description toDescription = new StringDescription();
final Description mismatchDescription = new StringDescription();
checker.describeTo(toDescription);
checker.describeMismatch(mismatchDescription);
final String reason =
"\n" +
"Expected: is \"" + toDescription.toString() + "\"\n" +
" but : was \"" + mismatchDescription.toString() + "\"";
throw new AssertionError(reason);
}
}
/**
* Assert that the given class adheres to the utility class rules.
*
* @param clazz the class to check
*
* @throws java.lang.AssertionError if the class is not a valid
* utility class
*/
public static void assertThatClassIsUtility(Class<?> clazz) {
final UtilityClassChecker checker = new UtilityClassChecker();
if (!checker.isProperlyDefinedUtilityClass(clazz)) {
final Description toDescription = new StringDescription();
final Description mismatchDescription = new StringDescription();
checker.describeTo(toDescription);
checker.describeMismatch(mismatchDescription);
final String reason =
"\n" +
"Expected: is \"" + toDescription.toString() + "\"\n" +
" but : was \"" + mismatchDescription.toString() + "\"";
throw new AssertionError(reason);
}
}
/**
* Assert that the given matcher matches the actual value.
* @param <T> the static type accepted by the matcher
* @param reason additional information about the error
* @param actual the value to match against
* @param matcher the matcher
*/
public static <T> void assertThat(String reason, T actual, Matcher<T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description.appendText(reason);
description.appendText("\nExpected: ");
description.appendDescriptionOf(matcher);
description.appendText("\n but: ");
matcher.describeMismatch(actual, description);
throw new AssertionError(description.toString());
}
}
public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description.appendText(reason).appendText("\nExpected: ").appendDescriptionOf(matcher).appendText("\n but: ");
matcher.describeMismatch(actual, description);
throw new BpmnError(description.toString());
}
}
public T waitFor(Matcher<? super T> matcher) {
ListenableFuture<T> finished = future(matcher);
try {
return finished.get(WAIT_TIMEOUT, TimeUnit.SECONDS);
} catch (Exception e) {
Description d = new StringDescription();
matcher.describeTo(d);
throw new AssertionError("Failed waiting for " + d.toString(), e);
}
}
public static <E> void require(Scope scope, E e, Matcher<E> matcher) {
if (!matcher.matches(e)) {
final Description description = new StringDescription();
matcher.describeMismatch(e, description);
final String message = "Precondition violated! " + description.toString();
switch (scope) {
case ARGUMENT:
throw new IllegalArgumentException(message);
case STATE:
throw new IllegalStateException(message);
default:
throw new RuntimeException("Unexpected scope value: '" + scope.name() + "'");
}
}
}
static void fail(@Nullable Object actualValue, Matcher<?> matcher) {
Description description =
new StringDescription()
.appendText("\nExpected: ")
.appendDescriptionOf(matcher)
.appendText("\n but: ");
matcher.describeMismatch(actualValue, description);
AssertionError assertionError = new AssertionError(description.toString());
assertionError.setStackTrace(ObjectChecker.trimStackTrace(assertionError.getStackTrace()));
throw assertionError;
}
private String describeMatcher() {
final Description d = new StringDescription();
matcher.describeTo(d);
return d.toString();
}
public String getArgumentsLine(List<Matcher> matchers, PrintSettings printSettings) {
Description result = new StringDescription();
result.appendList("(", ", ", ");", applyPrintSettings(matchers, printSettings));
return result.toString();
}
public String getArgumentsBlock(List<Matcher> matchers, PrintSettings printSettings) {
Description result = new StringDescription();
result.appendList("(\n ", ",\n ", "\n);", applyPrintSettings(matchers, printSettings));
return result.toString();
}
public String getArgumentsLine(List<Matcher> matchers, PrintSettings printSettings) {
Description result = new StringDescription();
result.appendList("(", ", ", ");", applyPrintSettings(matchers, printSettings));
return result.toString();
}
public String getArgumentsBlock(List<Matcher> matchers, PrintSettings printSettings) {
Description result = new StringDescription();
result.appendList("(\n ", ",\n ", "\n);", applyPrintSettings(matchers, printSettings));
return result.toString();
}
/**
* Copy of {@link MatcherAssert#assertThat(String, Object, Matcher)} with
* the following additions:
* <ul>
* <li>If the matcher can provide expected value, a
* {@link ComparisonFailure} exception is thrown instead of
* {@link AssertionError} in order to display differences between expected
* string and actual string in the IDE.</li>
* <li>If the matcher is a {@link CustomReason} matcher and no reason is
* provided, the reason of the matcher is used to provide more information
* about the context (which message has failed for example)</li>
* </ul>
*
* @param reason
* the reason
* @param actual
* the actual value
* @param matcher
* the matcher to apply
* @param <T>
* the type used for the matcher
*/
public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
if (!matcher.matches(actual)) {
Description description = getDescription(reason, actual, matcher);
if (hasExpectedValue(matcher)) {
ExpectedValueProvider<T> comparable = getComparable(matcher);
throw new ComparisonFailure(description.toString(), String.valueOf(comparable == null ? null : comparable.getExpectedValue()), String.valueOf(actual));
} else {
throw new AssertionError(description.toString());
}
}
}