Skip to content

Commit

Permalink
Removes clutter from error output when multiple classes fail
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 7, 2022
1 parent 5f1976c commit 7ecafbe
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Removed duplicated website urls and diagnostic output when using `forPackage()` or `forClasses()` and multiple classes have issues.

## [3.12.1] - 2022-12-02

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void verify() {
}
String messages = Formatter
.of(
"EqualsVerifier found a problem in %% %%.\n---\n%%\n---\n%%\n%%",
"EqualsVerifier found a problem in %% %%.\n---\n%%\n---\n%%\n---\n%%",
failures.size(),
failures.size() == 1 ? "class" : "classes",
failures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ public EqualsVerifierReport report() {
return report(true);
}

/**
* Performs the verification of the contracts for {@code equals} and {@code hashCode} and
* returns an {@link EqualsVerifierReport} with the results of the verification.
*
* @param showUrl Whether or not to show the url at the end of the error message.
* @return An {@link EqualsVerifierReport} that indicates whether the contract is met and
* whether {@link EqualsVerifier}'s preconditions hold.
*/
public EqualsVerifierReport report(boolean showUrl) {
try {
performVerification();
Expand All @@ -349,14 +357,13 @@ public EqualsVerifierReport report(boolean showUrl) {

private String buildErrorMessage(String description, boolean showUrl) {
String message = description == null ? "<no message>" : description;
return Formatter
.of(
"EqualsVerifier found a problem in class %%.\n-> %%\n\n%%",
type.getName(),
message,
ErrorMessage.suffix()
)
String result = Formatter
.of("EqualsVerifier found a problem in class %%.\n-> %%", type.getName(), message)
.format();
if (showUrl) {
result += "\n\n" + ErrorMessage.suffix();
}
return result;
}

private void performVerification() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ public ExpectedException assertMessageContains(String... fragments) {
return this;
}

public ExpectedException assertMessageContainsOnce(String fragment) {
int occurrences = 0;
String message = e.getMessage();
while (message.indexOf(fragment) > -1) {
occurrences += 1;
message = message.substring(message.indexOf(fragment) + fragment.length());
}
if (occurrences != 1) {
fail(
"Message [" +
e.getMessage() +
"] contains [" +
fragment +
"] " +
occurrences +
" times; should be 1."
);
}
return this;
}

public ExpectedException assertMessageDoesNotContain(String... fragments) {
String message = e.getMessage();
for (String fragment : fragments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import nl.jqno.equalsverifier.internal.exceptions.AssertionException;
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException;
import nl.jqno.equalsverifier.internal.util.Formatter;
import nl.jqno.equalsverifier.testhelpers.packages.correct.A;
import nl.jqno.equalsverifier.testhelpers.types.MutablePoint;
import nl.jqno.equalsverifier.testhelpers.types.Point;
import nl.jqno.equalsverifier.testhelpers.types.RecursiveTypeHelper.Node;
import org.junit.jupiter.api.Test;
Expand All @@ -27,14 +27,12 @@ public void messageIsValidForSingleType_whenEqualsVerifierFails_givenExceptionIs
@Test
public void messageIsValidForMultipleTypes_whenEqualsVerifierFails_givenExceptionIsGeneratedByEqualsVerifierItself() {
ExpectedException
.when(() -> EqualsVerifier.forClasses(A.class, Point.class).verify())
.assertMessageContains(
Point.class.getSimpleName(),
SEE_ALSO,
WEBSITE_URL,
SUFFIX,
"---"
);
.when(() -> EqualsVerifier.forClasses(Point.class, MutablePoint.class).verify())
.assertMessageContains(Point.class.getSimpleName())
.assertMessageContains("---")
.assertMessageContainsOnce(SEE_ALSO)
.assertMessageContainsOnce(WEBSITE_URL)
.assertMessageContainsOnce(SUFFIX);
}

@Test
Expand Down

0 comments on commit 7ecafbe

Please sign in to comment.