org.hamcrest.StringDescription Java Examples
The following examples show how to use
org.hamcrest.StringDescription.
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: DescriptiveSoftAssert.java From vividus with Apache License 2.0 | 6 votes |
@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 #2
Source File: WhileList.java From flink-spector with Apache License 2.0 | 6 votes |
@Override public boolean matchesSafely(Iterable<T> objects, Description mismatch) { int numMatches = 0; int numMismatches = 0; Description mismatches = new StringDescription(); int i = 0; for (T item : objects) { if (!matcher.matches(item)) { if (numMismatches < 10) { matcher.describeMismatch(item, mismatches); mismatches.appendText(" on record #" + (i + 1)); } numMismatches++; } else { numMatches++; } if (!validWhile(numMatches, numMismatches)) { describeMismatch(numMatches, numMismatches, true, mismatch, mismatches); return false; } i++; } describeMismatch(numMatches, numMismatches, false, mismatch, mismatches); return validAfter(numMatches); }
Example #3
Source File: IsPojoTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void testMismatchFormattingInOrderOfAddition() throws Exception { final IsPojo<SomeClass> sut = pojo(SomeClass.class) .where("foo", is(41)) .where("baz", is( pojo(SomeClass.class) .where("foo", is(43)) )) .withProperty("bar", is("not-bar")); final StringDescription description = new StringDescription(); sut.describeMismatch(new SomeClass(), description); assertThat(description.toString(), is( "SomeClass {\n" + " foo(): was <42>\n" + " baz(): SomeClass {\n" + " foo(): was <42>\n" + " }\n" + " getBar(): was \"bar\"\n" + "}" )); }
Example #4
Source File: SuccessfullyCompletedFutureTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void testInterruptedMismatchFormatting() throws Exception { final SettableFuture<Void> future = SettableFuture.create(); try { // Interrupt this current thread so that future.get() will throw InterruptedException Thread.currentThread().interrupt(); final StringDescription description = new StringDescription(); SUT.describeMismatch(future, description); assertThat(description.toString(), is("a future that was not completed")); } finally { // Clear the interrupted flag to avoid interference between tests Thread.interrupted(); } }
Example #5
Source File: SuccessfullyCompletedFutureTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void testCancelledMismatchFormatting() throws Exception { final SettableFuture<Void> future = SettableFuture.create(); try { // Cancel the future future.cancel(true); final StringDescription description = new StringDescription(); SUT.describeMismatch(future, description); assertThat(description.toString(), is("a future that was cancelled")); } finally { // Clear the interrupted flag to avoid interference between tests Thread.interrupted(); } }
Example #6
Source File: ImmutableClassChecker.java From onos with Apache License 2.0 | 6 votes |
/** * 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 #7
Source File: IsJsonObjectTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void testMismatchNested() throws Exception { final Matcher<JsonNode> sut = is( jsonObject() .where("foo", is(jsonInt(1))) .where("bar", is(jsonBoolean(true))) .where("baz", is( jsonObject() .where("foo", is(jsonNull()))))); final StringDescription description = new StringDescription(); sut.describeMismatch(NF.objectNode().put("foo", 1).put("bar", true) .set("baz", NF.objectNode().set("foo", NF.booleanNode(false))), description); assertThat(description.toString(), is( "{\n" + " ...\n" + " \"baz\": {\n" + " \"foo\": was not a null node, but a boolean node\n" + " }\n" + "}" )); }
Example #8
Source File: ExceptionallyCompletedCompletionStageTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void testInterruptedMismatchFormatting() throws Exception { final CompletableFuture<Void> future = runAsync(waitUntilInterrupted()); try { // Interrupt this current thread so that future.get() will throw InterruptedException Thread.currentThread().interrupt(); final StringDescription description = new StringDescription(); SUT.describeMismatch(future, description); assertThat(description.toString(), is("a stage that was not completed")); } finally { // Clear the interrupted flag to avoid interference between tests Thread.interrupted(); } }
Example #9
Source File: AssertionHelper.java From ogham with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") private static <T> Description getDescription(String reason, T actual, Matcher<? super T> matcher, String additionalText) { if (matcher instanceof CustomDescriptionProvider) { return ((CustomDescriptionProvider<T>) matcher).describe(reason, actual, additionalText); } // @formatter:off Description description = new StringDescription(); description.appendText(getReason(reason, matcher)) .appendText(additionalText==null ? "" : ("\n"+additionalText)) .appendText("\nExpected: ") .appendDescriptionOf(matcher) .appendText("\n but: "); matcher.describeMismatch(actual, description); // @formatter:on return description; }
Example #10
Source File: ViewAssertions.java From android-test with Apache License 2.0 | 6 votes |
@Override public void check(View view, NoMatchingViewException noViewException) { StringDescription description = new StringDescription(); description.appendText("'"); viewMatcher.describeTo(description); if (noViewException != null) { description.appendText( String.format( Locale.ROOT, "' check could not be performed because view '%s' was not found.\n", noViewException.getViewMatcherDescription())); Log.e(TAG, description.toString()); throw noViewException; } else { description.appendText("' doesn't match the selected view."); assertThat(description.toString(), view, viewMatcher); } }
Example #11
Source File: IsJsonObject.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Override public void describeTo(Description description) { description.appendText("{\n"); for (Map.Entry<String, Matcher<? super JsonNode>> entryMatcher : entryMatchers.entrySet()) { final String key = entryMatcher.getKey(); final Matcher<? super JsonNode> valueMatcher = entryMatcher.getValue(); description.appendText(" "); describeKey(key, description); description.appendText(": "); final Description innerDescription = new StringDescription(); valueMatcher.describeTo(innerDescription); DescriptionUtils.indentDescription(description, innerDescription); } description.appendText("}"); }
Example #12
Source File: AutomatorAssertion.java From device-automator with MIT License | 6 votes |
/** * Asserts that the ui element specified in {@link DeviceAutomator#onDevice(UiObjectMatcher)} * has text that matches the given matcher. * * @param matcher The <a href="http://hamcrest.org/JavaHamcrest/">Hamcrest</a> to match against. * @return */ public static AutomatorAssertion text(final Matcher matcher) { return new AutomatorAssertion() { @Override public void wrappedCheck(UiObject object) throws UiObjectNotFoundException { visible(true).check(object); if (!matcher.matches(object.getText())) { StringDescription description = new StringDescription(); description.appendText("Expected "); matcher.describeTo(description); description.appendText(" "); matcher.describeMismatch(object.getText(), description); assertTrue(description.toString(), false); } } }; }
Example #13
Source File: RestClientCallArgumentMatcher.java From ods-provisioning-app with Apache License 2.0 | 6 votes |
@Override public boolean matches(RestClientCall argument) { invalidMatcherList.clear(); mismatchDescription = new StringDescription(); for (Pair<FeatureMatcher, Function<RestClientCall, ?>> pair : featureMatcherList) { FeatureMatcher featureMatcher = pair.getLeft(); featureMatcher.describeMismatch(argument, mismatchDescription); if (!featureMatcher.matches(argument)) { Function<RestClientCall, ?> valueExtractor = pair.getRight(); Object apply = argument == null ? null : valueExtractor.apply(argument); invalidMatcherList.add(Pair.of(featureMatcher, apply)); } } boolean isValid = invalidMatcherList.size() == 0; if (isValid) { captureArgumentValues(argument); } return isValid; }
Example #14
Source File: DescriptionUtilsTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void describeNestMismatchesNoEllipsisBetweenConsecutiveMismatches() throws Exception { Set<String> allKeys = new LinkedHashSet<>(asList("first", "second", "third", "forth")); StringDescription description = new StringDescription(); Map<String, Consumer<Description>> mismatchedKeys = ImmutableMap.of( "second", desc -> desc.appendText("mismatch!"), "third", desc -> desc.appendText("mismatch!")); BiConsumer<String, Description> describeKey = (str, desc) -> desc.appendText(str); DescriptionUtils.describeNestedMismatches(allKeys, description, mismatchedKeys, describeKey); assertThat(description.toString(), is( "{\n" + " ...\n" + " second: mismatch!\n" + " third: mismatch!\n" + " ...\n" + "}" )); }
Example #15
Source File: IsJsonObjectTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void multipleConsecutiveMismatchesHaveNoEllipsis() 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", "was string") .put("baz", 3); 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" + "}" )); }
Example #16
Source File: ExceptionallyCompletedFutureTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void testInterruptedMismatchFormatting() throws Exception { final SettableFuture<Void> future = SettableFuture.create(); try { // Interrupt this current thread so that future.get() will throw InterruptedException Thread.currentThread().interrupt(); final StringDescription description = new StringDescription(); SUT.describeMismatch(future, description); assertThat(description.toString(), is("a future that was not done")); } finally { // Clear the interrupted flag to avoid interference between tests Thread.interrupted(); } }
Example #17
Source File: IsJsonObjectTest.java From java-hamcrest with Apache License 2.0 | 6 votes |
@Test public void testDescription() throws Exception { final Matcher<JsonNode> sut = jsonObject() .where("foo", is(jsonInt(1))) .where("bar", is(jsonBoolean(false))) .where("baz", is(jsonObject() .where("foo", is(jsonNull())))); final StringDescription description = new StringDescription(); sut.describeTo(description); assertThat(description.toString(), is( "{\n" + " \"foo\": is a number node with value that is <1>\n" + " \"bar\": is a boolean node with value that is <false>\n" + " \"baz\": is {\n" + " \"foo\": is a null node\n" + " }\n" + "}" )); }
Example #18
Source File: AssumptionTest.java From hamcrest-junit with Eclipse Public License 1.0 | 5 votes |
@Test public void failingAssumeThrowsPublicAssumptionViolatedException() { Matcher<Integer> assumption = equalTo(4); try { assumeThat(3, assumption); } catch (org.junit.AssumptionViolatedException e) { assertTrue(e.getMessage().contains(StringDescription.toString(assumption))); } }
Example #19
Source File: IsJsonArrayTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void testDescription() throws Exception { final Matcher<JsonNode> sut = jsonArray(is(anything())); final StringDescription description = new StringDescription(); sut.describeTo(description); assertThat(description.toString(), is( "an array node whose elements is ANYTHING" )); }
Example #20
Source File: AsyncChannelAsserts.java From c5-replicator with Apache License 2.0 | 5 votes |
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 #21
Source File: DoubleStreamMatcherTest.java From Lightweight-Stream-API with Apache License 2.0 | 5 votes |
@Test public void testIsEmpty() { assertThat(DoubleStream.empty(), isEmpty()); StringDescription description = new StringDescription(); isEmpty().describeTo(description); assertThat(description.toString(), is("an empty stream")); }
Example #22
Source File: LazyEqualsMatcherTest.java From totallylazy with Apache License 2.0 | 5 votes |
@Test public void shouldDescribeEqualityWithExpectedAndActual() { Description description = new StringDescription(); Matcher<String> matcher = lazyEqualTo(SIMPLE_DESCRIPTION_TEXT, returns(EXPECTED)); matcher.matches(DIFFERENT_ACTUAL); matcher.describeTo(description); assertThat(description.toString(), is(FULL_DESCRIPTION_TEXT)); }
Example #23
Source File: StreamMatcherTest.java From Lightweight-Stream-API with Apache License 2.0 | 5 votes |
@Test public void testIsEmpty() { assertThat(Stream.empty(), isEmpty()); StringDescription description = new StringDescription(); isEmpty().describeTo(description); assertThat(description.toString(), is("an empty stream")); }
Example #24
Source File: IsJsonNullTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void testDescription() throws Exception { final Matcher<JsonNode> sut = jsonNull(); final StringDescription description = new StringDescription(); sut.describeTo(description); assertThat(description.toString(), is( "a null node" )); }
Example #25
Source File: AssumptionTest.java From hamcrest-junit with Eclipse Public License 1.0 | 5 votes |
@Test public void failingAssumeWithMessageReportsBothMessageAndMatcher() { String message = "Some random message string."; Matcher<Integer> assumption = equalTo(4); try { assumeThat(message, 3, assumption); } catch (org.junit.AssumptionViolatedException e) { assertTrue(e.getMessage().contains(message)); assertTrue(e.getMessage().contains(StringDescription.toString(assumption))); } }
Example #26
Source File: PersonMatchersTest.java From hamcrest-compose with Apache License 2.0 | 5 votes |
@Test public void describeMismatchWhenAllPropertiesUnequalDescribesMismatch() { Person person1 = new Person("x1", "y1", "z1"); Person person2 = new Person("x2", "y2", "z2"); StringDescription description = new StringDescription(); personEqualTo(person1).describeMismatch(person2, description); assertThat(description.toString(), is("title was \"x2\"\n" + " and first name was \"y2\"\n" + " and last name was \"z2\"")); }
Example #27
Source File: SuccessfullyCompletedCompletionStageTest.java From java-hamcrest with Apache License 2.0 | 5 votes |
@Test public void testMismatchFormatting() throws Exception { final StringDescription description = new StringDescription(); SUT.describeMismatch(completedFuture(2), description); assertThat(description.toString(), is("a stage that completed to a value that was <2>")); }
Example #28
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 #29
Source File: Contracts.java From cloudml with GNU Lesser General Public License v3.0 | 5 votes |
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 #30
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()); } }