From 1d308b1c1b89d235c6f3a293250c360c5727fb34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 27 Sep 2020 17:17:58 +0200 Subject: [PATCH] (#203) Simplify Assertion to ease debug --- .../llorllale/cactoos/matchers/Assertion.java | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/llorllale/cactoos/matchers/Assertion.java b/src/main/java/org/llorllale/cactoos/matchers/Assertion.java index 4f22f14a..6b8a0a42 100644 --- a/src/main/java/org/llorllale/cactoos/matchers/Assertion.java +++ b/src/main/java/org/llorllale/cactoos/matchers/Assertion.java @@ -26,7 +26,6 @@ */ package org.llorllale.cactoos.matchers; -import org.cactoos.scalar.Unchecked; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.StringDescription; @@ -73,15 +72,21 @@ * Matchers derived from BaseMatcher, such as IsEqual. */ public final class Assertion { + + /** + * Message. + */ + private final String msg; + /** - * Whether this assertion is refuted. + * Tested value. */ - private final Unchecked refuted; + private final T test; /** - * Refutation error. + * Matcher. */ - private final Unchecked error; + private final Matcher matcher; /** * Ctor. @@ -92,18 +97,9 @@ public final class Assertion { public Assertion( final String msg, final T test, final Matcher matcher ) { - this.refuted = new Unchecked<>(() -> !matcher.matches(test)); - this.error = new Unchecked<>( - () -> { - final Description text = new StringDescription(); - text.appendText(msg) - .appendText(String.format("%nExpected: ")) - .appendDescriptionOf(matcher) - .appendText(String.format("%n but was: ")); - matcher.describeMismatch(test, text); - return new AssertionError(text.toString()); - } - ); + this.msg = msg; + this.test = test; + this.matcher = matcher; } /** @@ -111,8 +107,14 @@ public Assertion( * @throws AssertionError if this assertion is refuted */ public void affirm() throws AssertionError { - if (this.refuted.value()) { - throw this.error.value(); + if (!this.matcher.matches(this.test)) { + final Description text = new StringDescription(); + text.appendText(this.msg) + .appendText(String.format("%nExpected: ")) + .appendDescriptionOf(this.matcher) + .appendText(String.format("%n but was: ")); + this.matcher.describeMismatch(this.test, text); + throw new AssertionError(text.toString()); } } }