use of org.hamcrest.StringDescription in project double-espresso by JakeWharton.
the class ViewInteraction method doPerform.
private void doPerform(final ViewAction viewAction) {
checkNotNull(viewAction);
final Matcher<? extends View> constraints = checkNotNull(viewAction.getConstraints());
runSynchronouslyOnUiThread(new Runnable() {
@Override
public void run() {
uiController.loopMainThreadUntilIdle();
View targetView = viewFinder.getView();
Log.i(TAG, String.format("Performing '%s' action on view %s", viewAction.getDescription(), viewMatcher));
if (!constraints.matches(targetView)) {
// TODO(user): update this to describeMismatch once hamcrest is updated to new
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 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);
}
}
});
}
use of org.hamcrest.StringDescription in project double-espresso by JakeWharton.
the class ViewAssertions method matches.
/**
* Returns a generic {@link ViewAssertion} that asserts that a view exists in the view hierarchy
* and is matched by the given view matcher.
*/
public static ViewAssertion matches(final Matcher<? super View> viewMatcher) {
checkNotNull(viewMatcher);
return new ViewAssertion() {
@Override
public void check(Optional<View> view, Optional<NoMatchingViewException> noViewException) {
StringDescription description = new StringDescription();
description.appendText("'");
viewMatcher.describeTo(description);
if (noViewException.isPresent()) {
description.appendText(String.format("' check could not be performed because view '%s' was not found.\n", viewMatcher));
Log.e(TAG, description.toString());
throw noViewException.get();
} else {
// TODO(user): ideally, we should append the matcher used to find the view
// This can be done in DefaultFailureHandler (just like we currently to with
// PerformException)
description.appendText("' doesn't match the selected view.");
assertThat(description.toString(), view.get(), viewMatcher);
}
}
};
}
use of org.hamcrest.StringDescription in project double-espresso by JakeWharton.
the class ViewMatchers method assertThat.
/**
* A replacement for MatcherAssert.assertThat that renders View objects nicely.
*
* @param message the message to display.
* @param actual the actual value.
* @param matcher a matcher that accepts or rejects actual.
*/
public static <T> void assertThat(String message, T actual, Matcher<T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description.appendText(message).appendText("\nExpected: ").appendDescriptionOf(matcher).appendText("\n Got: ");
if (actual instanceof View) {
description.appendValue(HumanReadables.describe((View) actual));
} else {
description.appendValue(actual);
}
description.appendText("\n");
throw new AssertionFailedError(description.toString());
}
}
use of org.hamcrest.StringDescription in project cucumber-jvm by cucumber.
the class AbstractMatcherTest method assertDescription.
public static void assertDescription(String expected, Matcher<?> matcher) {
Description description = new StringDescription();
description.appendDescriptionOf(matcher);
Assert.assertEquals("Expected description", expected, description.toString().trim());
}
use of org.hamcrest.StringDescription in project cucumber-jvm by cucumber.
the class AbstractMatcherTest method mismatchDescription.
public static <T> String mismatchDescription(Matcher<? super T> matcher, T arg) {
Description description = new StringDescription();
matcher.describeMismatch(arg, description);
return description.toString().trim();
}
Aggregations