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

The following examples show how to use org.hamcrest.StringDescription#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: HamcrestMatching.java    From verano-http with MIT License 6 votes vote down vote up
@Override
public final Collection<String> apply(final Dict request) {
    final Collection<String> result;
    final String matching = this.func.apply(request);
    if (this.matcher.matches(matching)) {
        result = new CollectionOf<>();
    } else {
        final StringDescription description = new StringDescription();
        description
            .appendText("\nExpected: ")
            .appendDescriptionOf(this.matcher)
            .appendText("\n     but: ");
        this.matcher.describeMismatch(matching, description);
        result = new CollectionOf<>(description.toString());
    }
    return result;
}
 
Example 2
Source File: DescriptiveSoftAssert.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Override
public <T> boolean assertThat(String businessDescription, String systemDescription, T actual,
        Matcher<? super T> matcher)
{
    boolean isMatches = matcher.matches(actual);
    if (!isMatches)
    {
        String assertionDescription = getAssertionDescriptionString(actual, matcher);
        return recordAssertion(format(businessDescription, assertionDescription),
                format(systemDescription, assertionDescription), isMatches);
    }
    StringDescription description = new StringDescription();
    matcher.describeTo(description);
    String matchedString = description.toString();
    return recordAssertion(businessDescription + StringUtils.SPACE + matchedString,
            systemDescription + StringUtils.SPACE + matchedString, isMatches);
}
 
Example 3
Source File: ErrorReportTestUtil.java    From aeron with Apache License 2.0 6 votes vote down vote up
public String toString()
{
    final StringDescription description = new StringDescription();
    final String lineSeparator = System.getProperty("line.separator");

    description.appendText("Unable to match: ");
    matcher.describeTo(description);
    description.appendText(", against the following errors:");
    encodedExceptions.forEach(
        (encodedException) ->
        {
            description.appendText(lineSeparator).appendText("  ");
            description.appendText(encodedException);
        });
    description.appendText(lineSeparator);

    return description.toString();
}
 
Example 4
Source File: SoftAssert.java    From vividus with Apache License 2.0 5 votes vote down vote up
protected <T> String getAssertionDescriptionString(T actual, Matcher<? super T> matcher)
{
    StringDescription stringDescription = new StringDescription();
    stringDescription.appendText(EXPECTED).appendDescriptionOf(matcher);

    StringDescription mismatchStringDescription = new StringDescription();
    matcher.describeMismatch(actual, mismatchStringDescription);

    String mismatchString = mismatchStringDescription.toString();
    if (!mismatchString.isEmpty())
    {
        stringDescription.appendText(ACTUAL).appendText(mismatchString);
    }
    return stringDescription.toString();
}
 
Example 5
Source File: JsonSoftAssert.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Override
public <T> String getAssertionDescriptionString(T actual, Matcher<? super T> matcher)
{
    if (matcher instanceof ConfigurableJsonMatcher)
    {
        if (matcher.matches(actual))
        {
            return "Condition is true";
        }
        StringDescription mismatchStringDescription = new StringDescription();
        matcher.describeMismatch(actual, mismatchStringDescription);
        return mismatchStringDescription.toString();
    }
    return super.getAssertionDescriptionString(actual, matcher);
}
 
Example 6
Source File: CommonMatcher.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchesSafely(Matcher m, Description mismatchDescription) {
    StringDescription stringDescription = new StringDescription();
    m.describeTo(stringDescription);
    final String description = stringDescription.toString();

    if (!matcher.matches(description)) {
        mismatchDescription.appendText("description ");
        matcher.describeMismatch(description, mismatchDescription);
        return false;
    }
    return true;
}
 
Example 7
Source File: Matchers.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
public static <T> Function1<T, String> diagnoseMismatch(final DiagnosingMatcher matcher) {
    return t -> {
        StringDescription mismatchDescription = new StringDescription();
        matcher.describeMismatch(t, mismatchDescription);
        return mismatchDescription.toString();
    };
}
 
Example 8
Source File: CommonMatcher.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchesSafely(Matcher m, Description mismatchDescription) {
    StringDescription stringDescription = new StringDescription();
    m.describeTo(stringDescription);
    final String description = stringDescription.toString();

    if (!matcher.matches(description)) {
        mismatchDescription.appendText("description ");
        matcher.describeMismatch(description, mismatchDescription);
        return false;
    }
    return true;
}
 
Example 9
Source File: ExpectedException.java    From hamcrest-junit with Eclipse Public License 1.0 4 votes vote down vote up
private String missingExceptionMessage() {
    String expectation= StringDescription.toString(matcherBuilder.build());
    return format(missingExceptionMessage, expectation);
}
 
Example 10
Source File: UtilTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
static String describe(Matcher m) {
  final StringDescription d = new StringDescription();
  m.describeTo(d);
  return d.toString();
}
 
Example 11
Source File: UtilTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
static String mismatchDescription(Matcher m, Object item) {
  final StringDescription d = new StringDescription();
  m.describeMismatch(item, d);
  return d.toString();
}
 
Example 12
Source File: TestBase.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
protected static String describe(SelfDescribing m) {
    return StringDescription.toString(m);
}
 
Example 13
Source File: TestBase.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
protected static String describe(SelfDescribing m) {
    return StringDescription.toString(m);
}
 
Example 14
Source File: CursorMatchers.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matchesSafely(Cursor cursor) {
  int chosenColumn = columnIndex;
  if (chosenColumn < 0) {
    chosenColumn = findColumnIndex(columnNameMatcher, cursor);
    if (chosenColumn < 0) {
      StringDescription description = new StringDescription();
      columnNameMatcher.describeTo(description);
      if (chosenColumn == COLUMN_NOT_FOUND) {
        if (checkColumns) {
          throw new IllegalArgumentException(
              "Couldn't find column in "
                  + Arrays.asList(cursor.getColumnNames())
                  + " matching "
                  + description.toString());
        }
        // this cursor position doesn't have a column with this name, but other ones might
        // (e.g. in case of MergeCursor), so if columnChecks are off continue the search.
        return false;
      } else if (chosenColumn == MULTIPLE_COLUMNS_FOUND) {
        throw new IllegalArgumentException(
            "Multiple columns in "
                + Arrays.asList(cursor.getColumnNames())
                + " match "
                + description.toString());
      } else {
        throw new IllegalArgumentException(
            "Couldn't find column in " + Arrays.asList(cursor.getColumnNames()));
      }
    }
  }
  try {
    return applier.apply(cursor, chosenColumn, valueMatcher);
  } catch (CursorIndexOutOfBoundsException e) {
    if (checkColumns) {
      throw new IllegalArgumentException("Column index is invalid", e);
    }
    // this cursor position doesn't have a column with this index, but other ones might
    // (e.g. in case of MergeCursor), so if columnChecks are off continue the search.
    return false;
  }
}
 
Example 15
Source File: ExpectedException.java    From hamcrest-junit with Eclipse Public License 1.0 4 votes vote down vote up
private String missingExceptionMessage() {
    String expectation= StringDescription.toString(matcherBuilder.build());
    return format(missingExceptionMessage, expectation);
}
 
Example 16
Source File: Poller.java    From astrix with Apache License 2.0 4 votes vote down vote up
private String describeFailureOf(Probe probe) {
	StringDescription description = new StringDescription();
	probe.describeFailureTo(description);
	return description.toString();
}
 
Example 17
Source File: ApiSurface.java    From beam with Apache License 2.0 4 votes vote down vote up
private boolean verifyNoAbandoned(
    final ApiSurface checkedApiSurface,
    final Set<Matcher<Class<?>>> allowedClasses,
    final Description mismatchDescription) {

  // <helper_lambdas>

  final Function<Matcher<Class<?>>, String> toMessage =
      abandonedClassMacther -> {
        final StringDescription description = new StringDescription();
        description.appendText("No ");
        abandonedClassMacther.describeTo(description);
        return description.toString();
      };

  final Predicate<Matcher<Class<?>>> matchedByExposedClasses =
      classMatcher ->
          FluentIterable.from(checkedApiSurface.getExposedClasses())
              .anyMatch(classMatcher::matches);

  // </helper_lambdas>

  final ImmutableSet<Matcher<Class<?>>> matchedClassMatchers =
      FluentIterable.from(allowedClasses).filter(matchedByExposedClasses).toSet();

  final Sets.SetView<Matcher<Class<?>>> abandonedClassMatchers =
      Sets.difference(allowedClasses, matchedClassMatchers);

  final ImmutableList<String> messages =
      FluentIterable.from(abandonedClassMatchers)
          .transform(toMessage)
          .toSortedList(Ordering.natural());

  if (!messages.isEmpty()) {
    mismatchDescription.appendText(
        "The following white-listed scopes did not have matching classes on the API surface:"
            + "\n\t"
            + Joiner.on("\n\t").join(messages));
  }

  return messages.isEmpty();
}
 
Example 18
Source File: UtilTest.java    From Quicksql with MIT License 4 votes vote down vote up
static String describe(Matcher m) {
  final StringDescription d = new StringDescription();
  m.describeTo(d);
  return d.toString();
}
 
Example 19
Source File: UtilTest.java    From Quicksql with MIT License 4 votes vote down vote up
static String mismatchDescription(Matcher m, Object item) {
  final StringDescription d = new StringDescription();
  m.describeMismatch(item, d);
  return d.toString();
}