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()); } }
/** * 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 DescriptionBuilder(Description description) { this.description = (description instanceof NullDescription) ? new StringDescription() : description; this.matches = true; }
private String describeMatcher() { Description d = new StringDescription(); matcher.describeTo(d); return d.toString(); } }
@Override public StringBuffer describeTo(StringBuffer buffer) { hamcrestMatcher.describeTo(new StringDescription(buffer)); return buffer; } }
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()); } }
/** * 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(); }
@Override public void appendTo(StringBuffer buffer) { hamcrestMatcher.describeTo(new StringDescription(buffer)); } }
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 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"; } }
@Test public void shouldBeDescriptive() { Matcher<? super ReadContext> matcher = withJsonPath("path", equalTo(2)); Description description = new StringDescription(); matcher.describeTo(description); assertThat(description.toString(), containsString("path")); assertThat(description.toString(), containsString("<2>")); }
@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>")); }
@Test public void shouldDescribeMismatchOfInvalidJson() { Matcher<File> matcher = isJsonFile(withPathEvaluatedTo(true)); Description description = new StringDescription(); matcher.describeMismatch(INVALID_JSON, description); assertThat(description.toString(), containsString("invalid.json")); assertThat(description.toString(), containsString("invalid-json")); } }
@Test public void shouldDescribeMismatchOfValidJson() { Matcher<Object> matcher = isJson(withPathEvaluatedTo(true)); Description description = new StringDescription(); matcher.describeMismatch(BOOKS_JSON_STRING, description); assertThat(description.toString(), containsString(TestingMatchers.MISMATCHED_TEXT)); }
@Test public void shouldDescribeMismatchOfInvalidJson() { Matcher<Object> matcher = isJson(withPathEvaluatedTo(true)); Description description = new StringDescription(); matcher.describeMismatch("invalid-json", description); assertThat(description.toString(), containsString("\"invalid-json\"")); }
@Test public void shouldDescribeMismatchOfInvalidJson() { Matcher<String> matcher = isJsonString(withPathEvaluatedTo(true)); Description description = new StringDescription(); matcher.describeMismatch("invalid-json", description); assertThat(description.toString(), containsString("\"invalid-json\"")); } }
@Test public void shouldBeDescriptive() { Matcher<Object> matcher = isJson(withPathEvaluatedTo(true)); Description description = new StringDescription(); matcher.describeTo(description); assertThat(description.toString(), startsWith("is json")); assertThat(description.toString(), containsString(TestingMatchers.MATCH_TRUE_TEXT)); }
@Test public void shouldBeDescriptive() { Matcher<File> matcher = isJsonFile(withPathEvaluatedTo(true)); Description description = new StringDescription(); matcher.describeTo(description); assertThat(description.toString(), startsWith("is json")); assertThat(description.toString(), containsString(MATCH_TRUE_TEXT)); }
@Test public void shouldBeDescriptive() { Matcher<String> matcher = isJsonString(withPathEvaluatedTo(true)); Description description = new StringDescription(); matcher.describeTo(description); assertThat(description.toString(), startsWith("is json")); assertThat(description.toString(), containsString(MATCH_TRUE_TEXT)); }