Skip to content

Instantly share code, notes, and snippets.

@longtimeago
Last active March 22, 2016 14:44
Show Gist options
  • Save longtimeago/5e7e81b29730f26ac40f to your computer and use it in GitHub Desktop.
Save longtimeago/5e7e81b29730f26ac40f to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2016 Paul Polishchuk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
/**
* Generic matcher which allows to create matcher for complex object by adding child matchers for each field.
* It shows exact fields and values in case of objects mismatch.
* @param <T> Type of Object to be matched
*/
public final class CombinableMatcher<T> extends BaseMatcher<T> {
private final Collection<Matcher<? super T>> matchers = new ArrayList<>();
private final Collection<Matcher<? super T>> failed = new ArrayList<>();
@Override
public boolean matches(final Object item) {
this.failed.addAll(this.matchers.stream().filter(matcher -> !matcher.matches(item)).collect(Collectors.toList()));
return this.failed.isEmpty();
}
@Override
public void describeTo(final Description description) {
description.appendList("(", " and ", ")", this.matchers);
}
@Override
public void describeMismatch(final Object item, final Description description) {
this.failed.stream().forEach(matcher -> {
description.appendDescriptionOf(matcher).appendText(" but observed ");
matcher.describeMismatch(item, description);
description.appendText("; ");
});
}
/**
* Append field matcher, wrapped by {@link FeatureMatcher}, which improves output readability.
* @param name property name (will be used in error description only.)
* @param matcher Matcher for the property value.
* @param func Function to get property value from the object.
* @param <U> Type of the property.
* @return this
*/
public <U> CombinableMatcher<T> and(final String name, final Matcher<U> matcher, final Function<T, U> func) {
this.matchers.add(new FeatureMatcher<T, U>(matcher, name, name) {
@Override
protected U featureValueOf(final T actual) {
return func.apply(actual);
}
});
return this;
}
}
@longtimeago
Copy link
Author

Java 8 ONLY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment