Search in sources :

Example 66 with Description

use of org.hamcrest.Description in project material-components-android by material-components.

the class TestUtilsMatchers method withStartDrawableFilledWith.

/**
   * Returns a matcher that matches TextViews whose start drawable is filled with the specified fill
   * color.
   */
public static Matcher withStartDrawableFilledWith(@ColorInt final int fillColor, final int allowedComponentVariance) {
    return new BoundedMatcher<View, TextView>(TextView.class) {

        private String failedCheckDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText(failedCheckDescription);
        }

        @Override
        public boolean matchesSafely(final TextView view) {
            final Drawable[] compoundDrawables = view.getCompoundDrawables();
            final boolean isRtl = (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL);
            final Drawable startDrawable = isRtl ? compoundDrawables[2] : compoundDrawables[0];
            if (startDrawable == null) {
                failedCheckDescription = "no start drawable";
                return false;
            }
            try {
                final Rect bounds = startDrawable.getBounds();
                TestUtils.assertAllPixelsOfColor("", startDrawable, bounds.width(), bounds.height(), true, fillColor, allowedComponentVariance, true);
            } catch (Throwable t) {
                failedCheckDescription = t.getMessage();
                return false;
            }
            return true;
        }
    };
}
Also used : Rect(android.graphics.Rect) Description(org.hamcrest.Description) Drawable(android.graphics.drawable.Drawable) BoundedMatcher(android.support.test.espresso.matcher.BoundedMatcher) TextView(android.widget.TextView)

Example 67 with Description

use of org.hamcrest.Description in project junit4 by junit-team.

the class Guesser method reguesses.

@Override
public List<ReguessableValue> reguesses(AssumptionViolatedException e) {
    final ArrayList<ReguessableValue> returnThis = new ArrayList<ReguessableValue>();
    e.describeTo(new BaseDescription() {

        @Override
        protected void append(char arg0) {
        }

        boolean expectedSeen = false;

        Object expected = null;

        @Override
        public Description appendValue(Object value) {
            noteValue(value);
            return super.appendValue(value);
        }

        private void noteValue(Object value) {
            if (!expectedSeen) {
                expected = value;
                expectedSeen = true;
                return;
            }
            GuessMap newGuesses = guesses.replaceGuess(expected, value);
            returnThis.add(new Guesser<T>(getType(), newGuesses));
        }
    });
    return returnThis;
}
Also used : BaseDescription(org.hamcrest.BaseDescription) Description(org.hamcrest.Description) BaseDescription(org.hamcrest.BaseDescription) ArrayList(java.util.ArrayList)

Example 68 with Description

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

Example 69 with Description

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

Example 70 with Description

use of org.hamcrest.Description in project gradle by gradle.

the class FileCollectionMatchers method sameCollection.

@Factory
public static <T extends FileCollection> Matcher<T> sameCollection(final FileCollection expected) {
    return new BaseMatcher<T>() {

        public boolean matches(Object o) {
            FileCollection actual = (FileCollection) o;
            List<? extends FileCollection> actualCollections = unpack(actual);
            List<? extends FileCollection> expectedCollections = unpack(expected);
            boolean equals = actualCollections.equals(expectedCollections);
            if (!equals) {
                System.out.println("expected: " + expectedCollections);
                System.out.println("actual: " + actualCollections);
            }
            return equals;
        }

        private List<? extends FileCollection> unpack(FileCollection expected) {
            if (expected instanceof UnionFileCollection) {
                UnionFileCollection collection = (UnionFileCollection) expected;
                return new ArrayList<FileCollection>(collection.getSources());
            }
            if (expected instanceof DefaultConfigurableFileCollection) {
                DefaultConfigurableFileCollection collection = (DefaultConfigurableFileCollection) expected;
                return new ArrayList<FileCollection>((Set) collection.getFrom());
            }
            if (expected instanceof CompositeFileCollection) {
                CompositeFileCollection collection = (CompositeFileCollection) expected;
                DefaultFileCollectionResolveContext context = new DefaultFileCollectionResolveContext(TestFiles.resolver());
                collection.visitContents(context);
                return context.resolveAsFileCollections();
            }
            throw new RuntimeException("Cannot get children of " + expected);
        }

        public void describeTo(Description description) {
            description.appendText("same file collection as ").appendValue(expected);
        }
    };
}
Also used : UnionFileCollection(org.gradle.api.internal.file.UnionFileCollection) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) ArrayList(java.util.ArrayList) DefaultConfigurableFileCollection(org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection) DefaultFileCollectionResolveContext(org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext) CompositeFileCollection(org.gradle.api.internal.file.CompositeFileCollection) CompositeFileCollection(org.gradle.api.internal.file.CompositeFileCollection) UnionFileCollection(org.gradle.api.internal.file.UnionFileCollection) DefaultConfigurableFileCollection(org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection) Factory(org.hamcrest.Factory)

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