use of org.hamcrest.Description in project beam by apache.
the class DisplayDataMatchers method includesDisplayDataFor.
/**
* Create a matcher that matches if the examined {@link DisplayData} contains all display data
* registered from the specified subcomponent and namespace.
*/
public static Matcher<DisplayData> includesDisplayDataFor(final String path, final HasDisplayData subComponent) {
return new CustomTypeSafeMatcher<DisplayData>("includes subcomponent") {
@Override
protected boolean matchesSafely(DisplayData displayData) {
DisplayData subComponentData = subComponentData(path);
if (subComponentData.items().size() == 0) {
throw new UnsupportedOperationException("subComponent contains no display data; " + "cannot verify whether it is included");
}
DisplayDataComparison comparison = checkSubset(displayData, subComponentData, path);
return comparison.missingItems.isEmpty();
}
@Override
protected void describeMismatchSafely(DisplayData displayData, Description mismatchDescription) {
DisplayData subComponentDisplayData = subComponentData(path);
DisplayDataComparison comparison = checkSubset(displayData, subComponentDisplayData, path);
mismatchDescription.appendText("did not include:\n").appendValue(comparison.missingItems).appendText("\nNon-matching items:\n").appendValue(comparison.unmatchedItems);
}
private DisplayData subComponentData(final String path) {
return DisplayData.from(new HasDisplayData() {
@Override
public void populateDisplayData(DisplayData.Builder builder) {
builder.include(path, subComponent);
}
});
}
private DisplayDataComparison checkSubset(DisplayData displayData, DisplayData included, String path) {
DisplayDataComparison comparison = new DisplayDataComparison(displayData.items());
for (Item item : included.items()) {
Item matchedItem = displayData.asMap().get(DisplayData.Identifier.of(DisplayData.Path.absolute(path), item.getNamespace(), item.getKey()));
if (matchedItem != null) {
comparison.matched(matchedItem);
} else {
comparison.missing(item);
}
}
return comparison;
}
class DisplayDataComparison {
Collection<Item> missingItems;
Collection<Item> unmatchedItems;
DisplayDataComparison(Collection<Item> superset) {
missingItems = Sets.newHashSet();
unmatchedItems = Sets.newHashSet(superset);
}
void matched(Item supersetItem) {
unmatchedItems.remove(supersetItem);
}
void missing(Item subsetItem) {
missingItems.add(subsetItem);
}
}
};
}
use of org.hamcrest.Description in project beam by apache.
the class DisplayDataMatchersTest method testHasDisplayItemDescription.
@Test
public void testHasDisplayItemDescription() {
Matcher<DisplayData> matcher = hasDisplayItem();
Description desc = new StringDescription();
Description mismatchDesc = new StringDescription();
matcher.describeTo(desc);
matcher.describeMismatch(DisplayData.none(), mismatchDesc);
assertEquals("DisplayData not an empty collection", desc.toString());
assertEquals("DisplayData was <[]>", mismatchDesc.toString());
}
use of org.hamcrest.Description in project gs-spring-security-3.2 by rwinch.
the class SecurityTests method inboxShowsOnlyRobsMessages.
@Test
public void inboxShowsOnlyRobsMessages() throws Exception {
RequestBuilder request = get("/").with(user(rob).roles("USER"));
mvc.perform(request).andExpect(model().attribute("messages", new BaseMatcher<List<Message>>() {
@Override
public boolean matches(Object other) {
@SuppressWarnings("unchecked") List<Message> messages = (List<Message>) other;
return messages.size() == 1 && messages.get(0).getId() == 100;
}
@Override
public void describeTo(Description d) {
}
}));
}
use of org.hamcrest.Description in project chefly_android by chef-ly.
the class ShoppinListTest method childAtPosition.
private static Matcher<View> childAtPosition(final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent) && view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
use of org.hamcrest.Description in project chefly_android by chef-ly.
the class FavoriteNavgationTest method childAtPosition.
private static Matcher<View> childAtPosition(final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent) && view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
Aggregations