Java Code Examples for org.hamcrest.Description#toString()

The following examples show how to use org.hamcrest.Description#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Validator.java    From qaf with MIT License 6 votes vote down vote up
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;
}
 
Example 2
Source File: ViewMatchers.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
  }
}
 
Example 3
Source File: RestClientCallArgumentMatcher.java    From ods-provisioning-app with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 4
Source File: UtilityClassChecker.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: ImmutableClassChecker.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 6
Source File: ImmutableClassChecker.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 7
Source File: ObjectChecker.java    From immutables with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: MatcherAssertionErrors.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
	}
}
 
Example 9
Source File: MatcherAssert.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example 10
Source File: Contracts.java    From cloudml with GNU Lesser General Public License v3.0 5 votes vote down vote up
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() + "'");
        }
    }
}
 
Example 11
Source File: AsyncChannelAsserts.java    From c5-replicator with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 12
Source File: MatchersPrinter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public String getArgumentsLine(List<Matcher> matchers, PrintSettings printSettings) {
    Description result = new StringDescription();
    result.appendList("(", ", ", ");", applyPrintSettings(matchers, printSettings));
    return result.toString();
}
 
Example 13
Source File: MatchersPrinter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public String getArgumentsBlock(List<Matcher> matchers, PrintSettings printSettings) {
    Description result = new StringDescription();
    result.appendList("(\n    ", ",\n    ", "\n);", applyPrintSettings(matchers, printSettings));
    return result.toString();
}
 
Example 14
Source File: MatchersPrinter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public String getArgumentsLine(List<Matcher> matchers, PrintSettings printSettings) {
    Description result = new StringDescription();
    result.appendList("(", ", ", ");", applyPrintSettings(matchers, printSettings));
    return result.toString();
}
 
Example 15
Source File: MatchersPrinter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public String getArgumentsBlock(List<Matcher> matchers, PrintSettings printSettings) {
    Description result = new StringDescription();
    result.appendList("(\n    ", ",\n    ", "\n);", applyPrintSettings(matchers, printSettings));
    return result.toString();
}
 
Example 16
Source File: HamcrestCondition.java    From litho with Apache License 2.0 4 votes vote down vote up
private String describeMatcher() {
  final Description d = new StringDescription();
  matcher.describeTo(d);
  return d.toString();
}
 
Example 17
Source File: AssertionHelper.java    From ogham with Apache License 2.0 3 votes vote down vote up
/**
 * 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());
		}
	}
}