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

The following examples show how to use org.hamcrest.StringDescription#appendText() . 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: ViewAssertions.java    From android-test with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: AutomatorAssertion.java    From device-automator with MIT License 6 votes vote down vote up
/**
 * 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 3
Source File: AutomatorAssertion.java    From device-automator with MIT License 6 votes vote down vote up
/**
 * Asserts that the ui element specified in {@link DeviceAutomator#onDevice(UiObjectMatcher)}
 * has a content description that matches the given matcher.
 *
 * @param matcher The <a href="http://hamcrest.org/JavaHamcrest/">Hamcrest</a> to match against.
 * @return
 */
public static AutomatorAssertion contentDescription(final Matcher matcher) {
    return new AutomatorAssertion() {
        @Override
        public void wrappedCheck(UiObject object) throws UiObjectNotFoundException {
            visible(true).check(object);
            if (!matcher.matches(object.getContentDescription())) {
                StringDescription description = new StringDescription();
                description.appendText("Expected ");
                matcher.describeTo(description);
                description.appendText(" ");
                matcher.describeMismatch(object.getText(), description);
                assertTrue(description.toString(), false);
            }
        }
    };
}
 
Example 4
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 5
Source File: ViewAssertions.java    From android with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion haveItemCount(final int count) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
            StringDescription description = new StringDescription();
            description.appendText("'");

            if (noViewFoundException != null) {
                description.appendText(String.format(
                        "' check could not be performed because view '%s' was not found.\n",
                        noViewFoundException.getViewMatcherDescription()));
                Log.e(TAG, description.toString());
                throw noViewFoundException;
            } else {
                int actualCount = -1;

                if (view instanceof RecyclerView) {
                    actualCount = getItemCount((RecyclerView) view);
                }

                if (actualCount == -1) {
                    throw new AssertionError("Cannot get view item count.");
                } else if (actualCount != count) {
                    throw new AssertionError("View has " + actualCount +
                            "items while expected " + count);
                }
            }
        }
    };
}
 
Example 6
Source File: RecyclerSortedViewAssertion.java    From Game-of-Thrones with Apache License 2.0 5 votes vote down vote up
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
  StringDescription description = new StringDescription();
  RecyclerView recyclerView = (RecyclerView) view;
  sortedList = withAdapter.itemsToSort(recyclerView);

  checkIsNotEmpty(view, description);

  description.appendText("The list " + sortedList + " is not sorted");
  assertTrue(description.toString(), Ordering.natural().<T>isOrdered(sortedList));
}
 
Example 7
Source File: RecyclerSortedViewAssertion.java    From Game-of-Thrones with Apache License 2.0 5 votes vote down vote up
private void checkIsNotEmpty(View view, StringDescription description) {
  if (sortedList.isEmpty()) {
    description.appendText("The list must be not empty");
    throw (new PerformException.Builder())
        .withActionDescription(description.toString())
        .withViewDescription(HumanReadables.describe(view))
        .withCause(new IllegalStateException("The list is empty"))
        .build();
  }
}
 
Example 8
Source File: DescriptionUtilsTest.java    From java-hamcrest with Apache License 2.0 5 votes vote down vote up
@Test
public void testIndentDescription() throws Exception {
  StringDescription innerDescription = new StringDescription();
  innerDescription.appendText("a\nb");

  StringDescription description = new StringDescription();
  DescriptionUtils.indentDescription(description, innerDescription);

  assertThat(description.toString(), is("a\n  b\n"));
}
 
Example 9
Source File: DescriptionUtilsTest.java    From java-hamcrest with Apache License 2.0 5 votes vote down vote up
@Test
public void testIndentDescriptionNoExtraNewline() throws Exception {
  StringDescription innerDescription = new StringDescription();
  innerDescription.appendText("a\nb\n");

  StringDescription description = new StringDescription();
  DescriptionUtils.indentDescription(description, innerDescription);

  assertThat(description.toString(), is("a\n  b\n"));
}
 
Example 10
Source File: WebViewAssertions.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
protected void checkResult(WebView view, E result) {
  StringDescription description = new StringDescription();
  description.appendText("'");
  resultMatcher.describeTo(description);
  description.appendText("' doesn't match: ");
  description.appendText(null == result ? "null" : resultDescriber.apply(result));
  assertThat(description.toString(), result, resultMatcher);
}
 
Example 11
Source File: PositionAssertions.java    From android-test with Apache License 2.0 5 votes vote down vote up
static ViewAssertion relativePositionOf(
    final Matcher<View> viewMatcher, final Position position) {
  checkNotNull(viewMatcher);
  return new ViewAssertion() {
    @Override
    public void check(final View foundView, NoMatchingViewException noViewException) {
      StringDescription description = new StringDescription();
      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 {
        // TODO: describe the foundView matcher instead of the foundView itself.
        description
            .appendText("View:")
            .appendText(HumanReadables.describe(foundView))
            .appendText(" is not ")
            .appendText(position.toString())
            .appendText(" view ")
            .appendText(viewMatcher.toString());
        assertThat(
            description.toString(),
            isRelativePosition(
                foundView, findView(viewMatcher, getTopViewGroup(foundView)), position),
            is(true));
      }
    }
  };
}
 
Example 12
Source File: ViewInteraction.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the given action on the view selected by the current view matcher. Should be executed
 * on the main thread.
 *
 * @param viewAction the action to execute.
 */
private void doPerform(final SingleExecutionViewAction viewAction) {
  checkNotNull(viewAction);
  final Matcher<? extends View> constraints = checkNotNull(viewAction.getConstraints());
  uiController.loopMainThreadUntilIdle();
  View targetView = viewFinder.getView();
  Log.i(
      TAG,
      String.format(
          Locale.ROOT,
          "Performing '%s' action on view %s",
          viewAction.getDescription(),
          viewMatcher));
  if (!constraints.matches(targetView)) {
    // TODO: update this to describeMismatch once hamcrest 1.4 is available
    StringDescription stringDescription =
        new StringDescription(
            new StringBuilder(
                "Action will not be performed because the target view "
                    + "does not match one or more of the following constraints:\n"));
    constraints.describeTo(stringDescription);
    stringDescription
        .appendText("\nTarget view: ")
        .appendValue(HumanReadables.describe(targetView));

    if (viewAction.getInnerViewAction() instanceof ScrollToAction
        && isDescendantOfA(isAssignableFrom(AdapterView.class)).matches(targetView)) {
      stringDescription.appendText(
          "\nFurther Info: ScrollToAction on a view inside an AdapterView will not work. "
              + "Use Espresso.onData to load the view.");
    }
    throw new PerformException.Builder()
        .withActionDescription(viewAction.getDescription())
        .withViewDescription(viewMatcher.toString())
        .withCause(new RuntimeException(stringDescription.toString()))
        .build();
  } else {
    viewAction.perform(uiController, targetView);
  }
}
 
Example 13
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();
}