unruly / java-8-matchers

Hamcrest Matchers for Java 8 features
MIT License
23 stars 5 forks source link

Use more flexible generics in matcher type signatures #1

Closed ghost closed 8 years ago

ghost commented 8 years ago

Currently, we cannot match an Optional<List<String>> against Hamcrest's hasItems matcher, which matches against Optional<Iterable<? super String>.

We can support matching against a generic type that extends that of the matcher.

public static <T, S extends T> Matcher<Optional<S>> containsFlex(Matcher<T> matcher) {
    return new TypeSafeMatcher<Optional<S>>() {
        @Override
        protected boolean matchesSafely(Optional<S> item) {
            return item.map(matcher::matches).orElse(false);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Optional with an item that matches" + matcher);
        }
    };
}
CliveEvans commented 8 years ago

You've merged this but not marked it closed @bruceeddy. Is this resolved?

ghost commented 8 years ago

Yes.

On Wed, Jan 27, 2016 at 10:29 AM, CliveEvans notifications@github.com wrote:

You've merged this but not marked it closed @bruceeddy https://github.com/bruceeddy. Is this resolved?

— Reply to this email directly or view it on GitHub https://github.com/unruly/java-8-matchers/issues/1#issuecomment-175538184 .

Bruce Eddy Development Team Lead | Phone: +447837795402 http://go.unruly.co/emailhome http://time.com/4177155/super-bowl-ads-most-shared/ Find our offices http://go.unruly.co/emailoffices | Read our latest research http://go.unruly.co/emailresearch [image: Facebook] http://go.unruly.co/sigFB [image: Twitter] http://go.unruly.co/sigTwit [image: LinkedIn] http://go.unruly.co/sigLI [image: YouTube] http://go.unruly.co/sigYT

The contents of this email are confidential and the information in it may not be disclosed except for the purpose for which it has been sent. If you have reason to believe that you are not the intended recipient of this communication, please contact the sender immediately.

Unruly Group Limited - Registered in England No. 5411297, VAT #: 108 2572 28 Registered Office: 42-46 Princelet Street, London, E1 5LP, UK