Skip to content

Commit

Permalink
(llorllale#203) Simplify Assertion to ease debug
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Sep 27, 2020
1 parent 068ec63 commit 1d308b1
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/main/java/org/llorllale/cactoos/matchers/Assertion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,15 +72,21 @@
* Matchers derived from BaseMatcher, such as IsEqual.
*/
public final class Assertion<T> {

/**
* Message.
*/
private final String msg;

/**
* Whether this assertion is refuted.
* Tested value.
*/
private final Unchecked<Boolean> refuted;
private final T test;

/**
* Refutation error.
* Matcher.
*/
private final Unchecked<AssertionError> error;
private final Matcher<T> matcher;

/**
* Ctor.
Expand All @@ -92,27 +97,24 @@ public final class Assertion<T> {
public Assertion(
final String msg, final T test, final Matcher<T> 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;
}

/**
* Affirm this 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());
}
}
}

0 comments on commit 1d308b1

Please sign in to comment.