-
-
Save MaZderMind/186ea38b0b56379b423b54034070f18f to your computer and use it in GitHub Desktop.
Hamcrest matcher that matches a Lambda.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util.matcher; | |
import java.util.function.Function; | |
import org.hamcrest.BaseMatcher; | |
import org.hamcrest.Description; | |
public class LambdaMatcher<T> extends BaseMatcher<T> { | |
private final Function<T, Boolean> matcher; | |
private final String description; | |
public LambdaMatcher(Function<T, Boolean> matcher, | |
String description) { | |
this.matcher = matcher; | |
this.description = description; | |
} | |
public LambdaMatcher(Function<T, Boolean> matcher) { | |
this(matcher, "matches lambda"); | |
} | |
public static <T> LambdaMatcher<T> matchesLambda(Function<T, Boolean> matcher) { | |
return new LambdaMatcher<T>(matcher); | |
} | |
public static <T> LambdaMatcher<T> matchesLambda(Function<T, Boolean> matcher, String description) { | |
return new LambdaMatcher<T>(matcher, description); | |
} | |
@Override | |
public boolean matches(Object argument) { | |
return matcher.apply((T) argument); | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText(this.description); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment