Java Code Examples for org.hamcrest.Matcher#describeMismatch()
The following examples show how to use
org.hamcrest.Matcher#describeMismatch() .
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: FirstMatchers.java From mobius with Apache License 2.0 | 6 votes |
/** * Returns a matcher that matches {@link First} instances with a model that matches the supplied * model matcher. * * @param matcher the matcher to apply to the model * @param <M> the model type * @param <F> the effect type */ public static <M, F> Matcher<First<M, F>> hasModel(Matcher<M> matcher) { return new TypeSafeDiagnosingMatcher<First<M, F>>() { @Override protected boolean matchesSafely(First<M, F> item, Description mismatchDescription) { if (!matcher.matches(item.model())) { mismatchDescription.appendText("bad model: "); matcher.describeMismatch(item.model(), mismatchDescription); return false; } else { mismatchDescription.appendText("has model: "); matcher.describeMismatch(item.model(), mismatchDescription); return true; } } @Override public void describeTo(Description description) { description.appendText("has a model: ").appendDescriptionOf(matcher); } }; }
Example 2
Source File: NextMatchers.java From mobius with Apache License 2.0 | 6 votes |
/** * Returns a matcher that matches {@link Next} instances with a model that matches the supplied * model matcher. * * @param matcher the matcher to apply to the model * @param <M> the model type * @param <F> the effect type */ public static <M, F> Matcher<Next<M, F>> hasModel(Matcher<M> matcher) { return new TypeSafeDiagnosingMatcher<Next<M, F>>() { @Override protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) { if (!item.hasModel()) { mismatchDescription.appendText("it had no model"); return false; } else if (!matcher.matches(item.modelUnsafe())) { mismatchDescription.appendText("the model "); matcher.describeMismatch(item.modelUnsafe(), mismatchDescription); return false; } else { return true; } } @Override public void describeTo(Description description) { description.appendText("Next with model ").appendDescriptionOf(matcher); } }; }
Example 3
Source File: NextMatchers.java From mobius with Apache License 2.0 | 6 votes |
/** * Returns a matcher that matches {@link Next} instances whose effects match the supplied effect * matcher. * * @param matcher the matcher to apply to the effects * @param <M> the model type * @param <F> the effect type */ public static <M, F> Matcher<Next<M, F>> hasEffects(Matcher<Iterable<F>> matcher) { return new TypeSafeDiagnosingMatcher<Next<M, F>>() { @Override protected boolean matchesSafely(Next<M, F> item, Description mismatchDescription) { if (!item.hasEffects()) { mismatchDescription.appendText("it had no effects"); return false; } else if (!matcher.matches(item.effects())) { mismatchDescription.appendText("the effects were "); matcher.describeMismatch(item.effects(), mismatchDescription); return false; } return true; } @Override public void describeTo(Description description) { description.appendText("Next with effects ").appendDescriptionOf(matcher); } }; }
Example 4
Source File: IsJsonObjectTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void testMultipleMismatchesReportsAllMismatches() throws Exception { final Matcher<JsonNode> sut = jsonObject() .where("foo", is(jsonInt(1))) .where("bar", is(jsonInt(2))) .where("baz", is(jsonInt(3))); final ObjectNode nestedMismatches = NF.objectNode() .put("foo", -1) .put("bar", 2) .put("baz", "was string"); final StringDescription description = new StringDescription(); sut.describeMismatch(nestedMismatches, description); assertThat(description.toString(), is( "{\n" + " \"foo\": was a number node with value that was <-1>\n" + " ...\n" + " \"baz\": was not a number node, but a string node\n" + "}" )); }
Example 5
Source File: IsJsonObjectTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void lastNodeMismatchHasNoTrailingEllipsisButHasLeading() throws Exception { final Matcher<JsonNode> sut = jsonObject() .where("foo", is(jsonInt(1))) .where("bar", is(jsonBoolean(false))); final StringDescription description = new StringDescription(); sut.describeMismatch(NF.objectNode().put("foo", 1).put("bar", true), description); assertThat(description.toString(), is( "{\n" + " ...\n" + " \"bar\": was a boolean node with value that was <true>\n" + "}" )); }
Example 6
Source File: IsJsonObjectTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void firstNodeMismatchHasNoLeadingEllipsisButWithTrailing() throws Exception { final Matcher<JsonNode> sut = jsonObject() .where("foo", is(jsonInt(1))) .where("bar", is(jsonBoolean(false))); final StringDescription description = new StringDescription(); sut.describeMismatch(NF.objectNode().put("foo", 2).put("bar", false), description); assertThat(description.toString(), is( "{\n" + " \"foo\": was a number node with value that was <2>\n" + " ...\n" + "}" )); }
Example 7
Source File: IsJsonNullTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void testMismatchType() throws Exception { final Matcher<JsonNode> sut = jsonNull(); final StringDescription description = new StringDescription(); sut.describeMismatch(NF.textNode("goat"), description); assertThat(description.toString(), is( "was not a null node, but a string node" )); }
Example 8
Source File: IsJsonNumberTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void testMismatchType() throws Exception { final Matcher<JsonNode> sut = jsonInt(1); final StringDescription description = new StringDescription(); sut.describeMismatch(NF.textNode("goat"), description); assertThat(description.toString(), is( "was not a number node, but a string node" )); }
Example 9
Source File: EventMatchers.java From mapper with Apache License 2.0 | 5 votes |
public static <EventT> Matcher<MatchingHandler<EventT>> singleEvent(final Matcher<? super EventT> valueMatcher) { return new TypeSafeDiagnosingMatcher<MatchingHandler<EventT>>() { @Override protected boolean matchesSafely(MatchingHandler<EventT> item, Description mismatchDescription) { if (item.events.isEmpty()) { mismatchDescription.appendText("no events happened"); return false; } else if (item.events.size() == 1) { EventT value = item.events.get(0); if (valueMatcher.matches(value)) { return true; } else { mismatchDescription.appendText("value "); valueMatcher.describeMismatch(value, mismatchDescription); return false; } } else { mismatchDescription.appendText("few events happened: " + item.events); return false; } } @Override public void describeTo(Description description) { description.appendText("only event ").appendDescriptionOf(valueMatcher); } }; }
Example 10
Source File: IsJsonObjectTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void nonConsecutiveMismatchesSeparatedByEllipsis() throws Exception { final Matcher<JsonNode> sut = jsonObject() .where("foo", is(jsonInt(1))) .where("bar", is(jsonInt(2))) .where("baz", is(jsonInt(3))) .where("qux", is(jsonInt(4))) .where("quux", is(jsonInt(5))); final ObjectNode nestedMismatches = NF.objectNode() .put("foo", -1) .put("bar", "was string") .put("baz", 3) .put("quux", 5); final StringDescription description = new StringDescription(); sut.describeMismatch(nestedMismatches, description); assertThat(description.toString(), is( "{\n" + " \"foo\": was a number node with value that was <-1>\n" + " \"bar\": was not a number node, but a string node\n" + " ...\n" + " \"qux\": was not a number node, but a missing node\n" + " ...\n" + "}" )); }
Example 11
Source File: IsJsonBooleanTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void testMismatchValue() throws Exception { final Matcher<JsonNode> sut = jsonBoolean(false); final StringDescription description = new StringDescription(); sut.describeMismatch(NF.booleanNode(true), description); assertThat(description.toString(), is( "was a boolean node with value that was <true>" )); }
Example 12
Source File: IsJsonArrayTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void testMismatchType() throws Exception { final Matcher<JsonNode> sut = jsonArray(contains(jsonText("a"))); final StringDescription description = new StringDescription(); sut.describeMismatch(NF.booleanNode(false), description); assertThat(description.toString(), is( "was not an array node, but a boolean node" )); }
Example 13
Source File: IsJsonStringMatchingTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void invalidJsonDescription() throws Exception { final Matcher<String> sut = isJsonStringMatching(any(JsonNode.class)); final Description description = new StringDescription(); sut.describeMismatch("{", description); assertThat(description.toString(), containsString("but the string was not valid JSON")); }
Example 14
Source File: Matching.java From hamcrest-junit with Eclipse Public License 1.0 | 5 votes |
public static <T> void checkMatch(String reason, T actual, Matcher<? super T> matcher, MismatchAction action) { if (!matcher.matches(actual)) { Description description = new StringDescription(); description.appendText(reason) .appendText("\nExpected: ") .appendDescriptionOf(matcher) .appendText("\n but: "); matcher.describeMismatch(actual, description); action.mismatch(description.toString()); } }
Example 15
Source File: ARecordTest.java From octarine with Apache License 2.0 | 5 votes |
@Test public void applies_validations_if_schema_given() { Record invalidPerson = person.with(Person.age.of(-1)); Matcher<Record> matcher = ARecord.validAgainst(Person.schema) .with(Person.name, "Arthur Putey") .with(Person.age, -1); StringDescription description = new StringDescription(); matcher.describeMismatch(invalidPerson, description); assertThat(description.toString(), containsString("Age must be 0 or greater")); }
Example 16
Source File: FulltextIndexTest.java From ongdb-lab-apoc with Apache License 2.0 | 5 votes |
private static Matcher<? super PropertyContainer> hasProperty(String key, Object value) { return new TypeSafeDiagnosingMatcher<PropertyContainer>() { @Override protected boolean matchesSafely(PropertyContainer item, Description mismatchDescription) { Object property; try (Transaction tx = item.getGraphDatabase().beginTx()) { property = item.getProperty(key, null); tx.success(); } if (property == null) { mismatchDescription.appendText("property ").appendValue(key).appendText(" not present"); return false; } if (value instanceof Matcher<?>) { Matcher<?> matcher = (Matcher<?>) value; if (!matcher.matches(property)) { matcher.describeMismatch(property, mismatchDescription); return false; } return true; } if (!property.equals(value)) { mismatchDescription.appendText("property ").appendValue(key).appendText("has value").appendValue(property); return false; } return true; } @Override public void describeTo(Description description) { description.appendText("entity with property ").appendValue(key).appendText("=").appendValue(value); } }; }
Example 17
Source File: FirstMatchers.java From mobius with Apache License 2.0 | 5 votes |
/** * Returns a matcher that matches {@link First} instances whose effects match the supplied effect * matcher. * * @param matcher the matcher to apply to the effects * @param <M> the model type * @param <F> the effect type */ public static <M, F> Matcher<First<M, F>> hasEffects(Matcher<Iterable<F>> matcher) { return new TypeSafeDiagnosingMatcher<First<M, F>>() { @Override protected boolean matchesSafely(First<M, F> item, Description mismatchDescription) { if (!item.hasEffects()) { mismatchDescription.appendText("no effects"); return false; } else if (!matcher.matches(item.effects())) { mismatchDescription.appendText("bad effects: "); matcher.describeMismatch(item.effects(), mismatchDescription); return false; } else { mismatchDescription.appendText("has effects: "); matcher.describeMismatch(item.effects(), mismatchDescription); return true; } } @Override public void describeTo(Description description) { description.appendText("has effects: ").appendDescriptionOf(matcher); } }; }
Example 18
Source File: ObjectChecker.java From immutables with Apache License 2.0 | 5 votes |
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 19
Source File: VertxMatcherAssert.java From vertx-rest-client with Apache License 2.0 | 5 votes |
public static <T> void assertThat(TestContext context, 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); context.fail(description.toString()); } }
Example 20
Source File: HasFeatureMatcherTest.java From hamcrest-compose with Apache License 2.0 | 5 votes |
@Test public void describeMismatchDescribesMismatch() { Matcher<String> matcher = hasFeature("x", "y", String::length, nothing("z")); StringDescription description = new StringDescription(); matcher.describeMismatch("a", description); assertThat(description.toString(), is("y z was <1>")); }