org.hamcrest.Description#toString ( )源码实例Demo

下面列出了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();
}
 
源代码2 项目: qaf   文件: Validator.java
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("<", "&lt;").replaceAll(">", "&gt;");
	Reporter.log(msg, result ? MessageTypes.Pass : MessageTypes.Fail);

	return result;
}
 
源代码3 项目: android-test   文件: ViewMatchers.java
/**
 * 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());
  }
}
 
源代码4 项目: onos   文件: ImmutableClassChecker.java
/**
 * 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);
    }
}
 
源代码5 项目: onos   文件: ImmutableClassChecker.java
/**
 * 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);
    }
}
 
源代码6 项目: onos   文件: UtilityClassChecker.java
/**
 * 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());
	}
}
 
源代码8 项目: flowable-engine   文件: MatcherAssert.java
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());
    }
}
 
源代码9 项目: c5-replicator   文件: AsyncChannelAsserts.java
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);
  }
}
 
源代码10 项目: cloudml   文件: Contracts.java
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() + "'");
        }
    }
}
 
源代码11 项目: immutables   文件: ObjectChecker.java
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;
}
 
源代码12 项目: litho   文件: HamcrestCondition.java
private String describeMatcher() {
  final Description d = new StringDescription();
  matcher.describeTo(d);
  return d.toString();
}
 
源代码13 项目: astor   文件: MatchersPrinter.java
public String getArgumentsLine(List<Matcher> matchers, PrintSettings printSettings) {
    Description result = new StringDescription();
    result.appendList("(", ", ", ");", applyPrintSettings(matchers, printSettings));
    return result.toString();
}
 
源代码14 项目: astor   文件: MatchersPrinter.java
public String getArgumentsBlock(List<Matcher> matchers, PrintSettings printSettings) {
    Description result = new StringDescription();
    result.appendList("(\n    ", ",\n    ", "\n);", applyPrintSettings(matchers, printSettings));
    return result.toString();
}
 
源代码15 项目: astor   文件: MatchersPrinter.java
public String getArgumentsLine(List<Matcher> matchers, PrintSettings printSettings) {
    Description result = new StringDescription();
    result.appendList("(", ", ", ");", applyPrintSettings(matchers, printSettings));
    return result.toString();
}
 
源代码16 项目: astor   文件: MatchersPrinter.java
public String getArgumentsBlock(List<Matcher> matchers, PrintSettings printSettings) {
    Description result = new StringDescription();
    result.appendList("(\n    ", ",\n    ", "\n);", applyPrintSettings(matchers, printSettings));
    return result.toString();
}
 
源代码17 项目: ogham   文件: AssertionHelper.java
/**
 * 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());
		}
	}
}