You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Static factories for creating Matchers using lambdas #440
Having static factories that accept lambdas will reduce duplicated/boilerplate code when creating matchers. A flawed PR (#203) was created for this however it lacked unit tests and the implementation failed some standard tests when passed incompatible objects (TypeSafeDiagnosingMatcher is supposed to fail gracefully without an exception when passed an incompatible object but was throwing an exception instead).
I intend to create a new PR for this with unit tests and an implementation that conforms to the expected behaviour for TypeSafeDiagnosingMatcher.
I have a PR ready to create that has two static factories - one for TypeSafeDiagnosingMatcher and one for FeatureMatcher. I haven't created a static factory in all classes as I believe that these two are the primary ones used for creating custom Matcher implementations. If there are other classes that are commonly used for custom matchers, let me know and I can look into creating static factories for those as well.
The static factory method signature for TypeSafeDiagnosingMatcher looks like this:
/** * A matcher that checks if a directory exists. * @return the file matcher */publicstaticMatcher<Path> anExistingDirectory() {
returnmatcher(Files::isDirectory, "an existing directory", "is not a directory", Path.class);
}
The static factory method signature for FeatureMatcher looks like this:
/** * A matcher that checks if a file size matches an expected size. * @param expected matcher for the expected size * @return the file matcher */publicstaticMatcher<Path> hasSize(finalMatcher<Long> expected) {
returnFeatureMatcher.matcher(expected, p->toUncheckedEx(()->Files.size(p)), "A file with size", "size", Path.class);
}
instead of
/** * A matcher that checks if a file size matches an expected size. * @param expected matcher for the expected size * @return the file matcher */publicstaticMatcher<Path> hasSize(finalMatcher<Long> expected) {
returnnewFeatureMatcher<Path, Long>(expected, "A file with size", "size") {
@OverrideprotectedLongfeatureValueOf(Pathactual) { returntoUncheckedEx(()->Files.size(actual)); }
};
}
Activity
rmcdouga commentedon Aug 24, 2025
I have a PR ready to create that has two static factories - one for TypeSafeDiagnosingMatcher and one for FeatureMatcher. I haven't created a static factory in all classes as I believe that these two are the primary ones used for creating custom Matcher implementations. If there are other classes that are commonly used for custom matchers, let me know and I can look into creating static factories for those as well.
The static factory method signature for TypeSafeDiagnosingMatcher looks like this:
A sample matcher looks like this:
The static factory method signature for FeatureMatcher looks like this:
A sample FeatureMatcher matcher looks like this:
instead of