Refine search
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 AssertionError(description.toString()); } }
@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; }
public void assertContainsMessageMatching( Matcher<String> messageMatcher ) { synchronized ( logCalls ) { for ( LogCall logCall : logCalls ) { if ( messageMatcher.matches( logCall.message ) ) { return; } } StringDescription description = new StringDescription(); description.appendDescriptionOf( messageMatcher ); fail( format( "Expected at least one log statement with message as %s, but none found. Actual log calls were:\n%s", description.toString(), serialize( logCalls.iterator() ) ) ); } }
/** * Return the description of a {@link SelfDescribing} object as a String. * * @param selfDescribing * The object to be described. * @return * The description of the object. */ public static String toString(SelfDescribing selfDescribing) { return new StringDescription().appendDescriptionOf(selfDescribing).toString(); }
public String buildError(T actual, M matcher) { final StringDescription descriptionBuilder = new StringDescription(); descriptionBuilder.appendText("Expected ").appendText(name).appendText(" "); matcher.describeTo(descriptionBuilder); descriptionBuilder.appendText(" but "); matcher.describeMismatch(actual, descriptionBuilder); final String description = descriptionBuilder.toString().replaceAll("[.\\n]+$", ""); return description + ".\n"; } }
public ValidRekord<T> fix() throws InvalidRekordException { ValidRekord<T> rekord = new ValidRekord<>(name, acceptedKeys, properties); if (!matcher.matches(rekord)) { StringDescription description = new StringDescription(); description.appendText("Expected that ").appendDescriptionOf(matcher).appendText(", but "); matcher.describeMismatch(rekord, description); description.appendText("."); throw new InvalidRekordException(description.toString()); } return rekord; }
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 getMismatchMessage(Callable<T> supplier, Matcher<? super T> matcher) { Description mismatchDescription = new StringDescription(); matcher.describeMismatch(lastResult, mismatchDescription); if (mismatchDescription.toString() != null && mismatchDescription.toString().isEmpty()) { mismatchDescription.appendText("was ").appendValue(lastResult); } return String.format("%s expected %s but %s", getCallableDescription(supplier), HamcrestToStringFilter.filter(matcher), mismatchDescription); }
static String getFailureDescription(Object matcher, Object value, @Nullable String message) { Description description = new StringDescription(); if (message != null) { description.appendText(message); description.appendText("\n\n"); } description.appendText("Expected: ") .appendDescriptionOf((Matcher) matcher); if (describeMismatchMethod == null) { // 1.1 description.appendText("\n got: ") .appendValue(value); } else { // 1.2 description.appendText("\n but: "); ReflectionUtil.invokeMethod(matcher, describeMismatchMethod, value, description); } return description.toString(); } }
protected AbstractPollingQuery(long interval, long defaultTimeout) { checkArgument(interval > 0, new StringDescription().appendText("interval is ").appendValue(interval) .appendText(" should be > 0").toString()); checkArgument(defaultTimeout > 0, new StringDescription().appendText("defaultTimeout is ").appendValue(defaultTimeout) .appendText(" should be > 0").toString()); checkArgument(defaultTimeout > interval, new StringDescription().appendText("defaultTimeout is ").appendValue(defaultTimeout) .appendText("interval is ").appendValue(interval).appendText(" defaultTimeout should be > interval").toString()); this.interval = interval; this.defaultTimeout = defaultTimeout; }
@Override public String toString() { Description description = new StringDescription(); description.appendText( "LogMatcher{" ); description.appendDescriptionOf( contextMatcher ); description.appendText( ", " ); description.appendDescriptionOf( levelMatcher ); description.appendText( ", message=" ); description.appendDescriptionOf( messageMatcher ); description.appendText( ", arguments=" ); description.appendDescriptionOf( argumentsMatcher ); description.appendText( ", throwable=" ); description.appendDescriptionOf( throwableMatcher ); description.appendText( "}" ); return description.toString(); } }
@Override public void describeTo(Description description) { final StringDescription s = new StringDescription(); sizeMatcher.describeTo(s); // Because the matchers typically passed here will start with their description with "a value", which makes the whole sentence clunky (a map with size a value greater than 2 -> a map with size greater than 2) final String sizeMatcherDesc = s.toString().replaceFirst("^a value ", ""); description.appendText("a map with size ").appendText(sizeMatcherDesc); } };
public static void describeTo(boolean paren, SelfDescribing sd, Description d, String message) { if (message == null) { describeTo(paren, sd, d); return; } if (message.contains("$1")) { StringDescription tmp = new StringDescription(); if (paren) tmp.appendText("("); tmp.appendDescriptionOf(sd); if (paren) tmp.appendText(")"); d.appendText(message.replace("$1", tmp.toString())); } else { if (paren) d.appendText("("); d.appendText(message); if (paren) d.appendText(")"); } }
@Test public void testContainsCharSequence() { final Matcher<CharSequence> angeryMatcher = containsCharSequence("angery"); assertThat(angeryMatcher.matches("angery reaccs only")).isTrue(); assertThat(angeryMatcher.matches("delet dis")).isFalse(); final StringDescription description = new StringDescription(); angeryMatcher.describeMismatch("delet", description); assertThat(description.toString()).isEqualTo("was \"delet\""); } }
@Test public void testIsEmpty() { assertThat(LongStream.empty(), isEmpty()); StringDescription description = new StringDescription(); isEmpty().describeTo(description); assertThat(description.toString(), is("an empty stream")); }
@Test public void shouldDescribeMismatchOfEvaluation() { Matcher<? super ReadContext> matcher = withJsonPath("expensive", equalTo(3)); Description description = new StringDescription(); matcher.describeMismatch(BOOKS_JSON, description); assertThat(description.toString(), containsString("expensive")); assertThat(description.toString(), containsString("<10>")); }
@Override public StringBuffer describeTo(StringBuffer buffer) { hamcrestMatcher.describeTo(new StringDescription(buffer)); return buffer; } }
@Override protected boolean matchesSafely(T item) { if (condition.matches(itemToMatchCondition)) { return matcher.matches(item); } Description conditionDescription = new StringDescription() .appendValue(itemToMatchCondition) .appendDescriptionOf(condition); throw new AssumptionViolatedException( String.format("Condition is not satisfied: %s", conditionDescription)); }
public static void assertDescription(String expected, Matcher<?> matcher) { Description description = new StringDescription(); description.appendDescriptionOf(matcher); Assert.assertEquals("Expected description", expected, description.toString().trim()); }
static <T> void assertMismatchDescription(Object value, Matcher<T> matcher, Matcher<String> expectedDescription, Matcher<String> expectedMismatchDescription) { final Description matcherDescription = new StringDescription().appendDescriptionOf(matcher); assertThat(matcherDescription.toString(), expectedDescription); final StringDescription d = new StringDescription(); matcher.describeMismatch(value, d); assertThat(d.toString(), expectedMismatchDescription); } }