Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create assertThat with reason supplier to defer calculation of reason #299

Open
Rhobal opened this issue Apr 30, 2020 · 1 comment
Open

Comments

@Rhobal
Copy link

Rhobal commented Apr 30, 2020

The reason is only required when an assertion fails. If a Supplier could be provided instead of a String, an expensive or destructive construction of a reason (such as reading an InputStream) could be deferred.
Example code:

    public static <T> void assertThat(Supplier<String> reason, T actual, Matcher<? super T> matcher) {
        if (!matcher.matches(actual)) {
            Description description = new StringDescription();
            description.appendText(reason.get())
                       .appendText(System.lineSeparator())
                       .appendText("Expected: ")
                       .appendDescriptionOf(matcher)
                       .appendText(System.lineSeparator())
                       .appendText("     but: ");
            matcher.describeMismatch(actual, description);
            
            throw new AssertionError(description.toString());
        }
    }

    public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
    	assertThat(() -> reason, actual, matcher);
    }
@tumbarumba
Copy link
Member

This seems like a reasonable improvement, though issues #206 and #207 will have to be resolved first, as Supplier is a 1.8 functional interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants