Search in sources :

Example 36 with Description

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);
            }
        }
    };
}
Also used : Item(org.apache.beam.sdk.transforms.display.DisplayData.Item) Description(org.hamcrest.Description) CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher)

Example 37 with Description

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());
}
Also used : Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) StringDescription(org.hamcrest.StringDescription) Test(org.junit.Test)

Example 38 with Description

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) {
        }
    }));
}
Also used : Description(org.hamcrest.Description) RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) BaseMatcher(org.hamcrest.BaseMatcher) Message(sample.data.Message) List(java.util.List) Test(org.junit.Test)

Example 39 with Description

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));
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ViewMatchers.withContentDescription(android.support.test.espresso.matcher.ViewMatchers.withContentDescription) ViewParent(android.view.ViewParent) ViewGroup(android.view.ViewGroup) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView)

Example 40 with Description

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));
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ViewMatchers.withContentDescription(android.support.test.espresso.matcher.ViewMatchers.withContentDescription) ViewParent(android.view.ViewParent) ViewGroup(android.view.ViewGroup) Espresso.onView(android.support.test.espresso.Espresso.onView) View(android.view.View)

Aggregations

Description (org.hamcrest.Description)122 Test (org.junit.Test)38 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)35 StringDescription (org.hamcrest.StringDescription)29 BaseMatcher (org.hamcrest.BaseMatcher)27 View (android.view.View)22 ViewParent (android.view.ViewParent)11 TextView (android.widget.TextView)11 ViewGroup (android.view.ViewGroup)8 Expectations (org.jmock.Expectations)8 URL (java.net.URL)7 Matcher (org.hamcrest.Matcher)7 Invocation (org.jmock.api.Invocation)7 BoundedMatcher (android.support.test.espresso.matcher.BoundedMatcher)6 ImageView (android.widget.ImageView)6 File (java.io.File)6 IOException (java.io.IOException)6 URI (java.net.URI)6 List (java.util.List)6 JsonNode (org.codehaus.jackson.JsonNode)6