Skip to content

Commit

Permalink
When returning multiple failures, eliminate any duplicate messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowwoc committed Jan 13, 2025
1 parent 9904e25 commit 8986a91
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 34 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# <img src="docs/checklist.svg" width=64 height=64 alt="checklist"> Requirements API

[![API](https://img.shields.io/badge/api_docs-5B45D5.svg)](https://cowwoc.github.io/requirements.java/10.7/)
[![API](https://img.shields.io/badge/api_docs-5B45D5.svg)](https://cowwoc.github.io/requirements.java/10.8/)
[![Changelog](https://img.shields.io/badge/changelog-A345D5.svg)](docs/changelog.md)
[![javascript, typescript](https://img.shields.io/badge/other%20languages-javascript,%20typescript-457FD5.svg)](../../../requirements.js)

Expand All @@ -22,7 +22,7 @@ To get started, add this Maven dependency:
<dependency>
<groupId>com.github.cowwoc.requirements</groupId>
<artifactId>java</artifactId>
<version>10.7</version>
<version>10.8</version>
</dependency>
```

Expand Down Expand Up @@ -152,14 +152,14 @@ This library offers the following features:
Designed for discovery using your favorite IDE's auto-complete feature.
The main entry points are:

* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#requireThat(T,java.lang.String))
* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#requireThat(T,java.lang.String))
for method preconditions.
* [that(value, name)](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#that(T,java.lang.String))
* [that(value, name)](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#that(T,java.lang.String))
for [class invariants, method postconditions and private methods](docs/features.md#assertion-support).
* [checkIf(value, name)](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#checkIf(T,java.lang.String))
* [checkIf(value, name)](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#checkIf(T,java.lang.String))
for multiple failures and customized error handling.

See the [API documentation](https://cowwoc.github.io/requirements.java/10.7/) for more details.
See the [API documentation](https://cowwoc.github.io/requirements.java/10.8/) for more details.

## Best practices

Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ Minor updates involving cosmetic changes have been omitted from this list.

See https://github.com/cowwoc/requirements.java/commits/main for a full list.

## Version 10.8 - 2025/01/13

* When returning multiple failures, eliminate any duplicate messages.

## Version 10.7 - 2025/01/03

* Bugfix: If the string representation of the expected and actual values was equal, the class names were not
Expand Down
6 changes: 3 additions & 3 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ String province = "Florida";
List<String> provinces = Arrays.asList("Ontario", "Quebec", "Nova Scotia", "New Brunswick", "Manitoba",
"British Columbia", "Prince Edward Island", "Saskatchewan", "Alberta", "Newfoundland and Labrador");

List<ValidationFailure> failures = checkIf(name, "name").length().isBetween(10, 30).elseGetFailures();
List<ValidationFailure> failures = new ArrayList<>(checkIf(name, "name").length().isBetween(10, 30).elseGetFailures());
failures.addAll(checkIf(provinces, "provinces").contains(province).elseGetFailures());

for (ValidationFailure failure: failures)
Expand Down Expand Up @@ -174,7 +174,7 @@ requireThat(nameToAge, "nameToAge").
## String diff

When
a [String comparison](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/type/component/ObjectValidatorComponent#isEqualTo(java.lang.Object))
a [String comparison](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/type/component/ObjectValidatorComponent#isEqualTo(java.lang.Object))
fails, the library outputs a diff of the values being compared.

Depending on the terminal capability, you will see a [textual](textual_diff.md) or a [colored](colored_diff.md) diff.
Expand All @@ -188,7 +188,7 @@ terminal.

The use of colors is disabled by default if stdin or stdout are redirected, even if ANSI colors are supported.
To enable colors,
invoke [GlobalConfiguration.terminalEncoding(TerminalEncoding)](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/GlobalConfiguration.html#terminalEncoding(com.github.cowwoc.requirements10.java.TerminalEncoding)).
invoke [GlobalConfiguration.terminalEncoding(TerminalEncoding)](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/GlobalConfiguration.html#terminalEncoding(com.github.cowwoc.requirements10.java.TerminalEncoding)).

## Returning the value after validation

Expand Down
8 changes: 4 additions & 4 deletions docs/supported_libraries.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Each module uses a separate class pair for validation. For example,
[DefaultJavaValidators](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html)
[DefaultJavaValidators](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html)
and
[JavaValidators](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/JavaValidators.html)
[JavaValidators](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/JavaValidators.html)
validate the core Java API. Similarly,
[DefaultGuavaValidators](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/guava/DefaultGuavaValidators.html)
[DefaultGuavaValidators](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/guava/DefaultGuavaValidators.html)
and
[GuavaValidators](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/guava/GuavaValidators.html)
[GuavaValidators](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/guava/GuavaValidators.html)
validate the Guava API.

The following table lists validators for third-party libraries:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ public boolean throwOnFailure()
{
if (failures.isEmpty())
return true;
Throwable throwable;
if (failures.size() == 1)
{
ValidationFailure failure = failures.getFirst();
throwable = failure.getException();
}
else
throwable = new MultipleFailuresException(failures);
Throwable throwable = getThrowable();
if (cleanStackTrace)
Exceptions.removeLibraryFromStackTrace(throwable);
switch (throwable)
Expand All @@ -87,6 +80,19 @@ public boolean throwOnFailure()
}
}

/**
* @return the failure exception
*/
private Throwable getThrowable()
{
if (failures.size() == 1)
{
ValidationFailure failure = failures.getFirst();
return failure.getException();
}
return new MultipleFailuresException(failures);
}

/**
* Returns the validation failure messages.
*
Expand Down Expand Up @@ -115,14 +121,7 @@ public Throwable getException()
{
if (failures.isEmpty())
return null;
Throwable throwable;
if (failures.size() == 1)
{
ValidationFailure failure = failures.getFirst();
throwable = failure.getException();
}
else
throwable = new MultipleFailuresException(failures);
Throwable throwable = getThrowable();
if (cleanStackTrace)
Exceptions.removeLibraryFromStackTrace(throwable);
return throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ public void addFailure(String message, Throwable cause, ExceptionBuilder excepti
{
ValidationFailureImpl failure = new ValidationFailureImpl(configuration, message, cause,
exceptionBuilder, Set.of());
for (ValidationFailure existingFailure : failures)
if (existingFailure.getMessage().equals(failure.getMessage()))
{
// Skip duplicate failure messages
return;
}

failures.add(failure);
if (configuration.throwOnFailure())
{
Expand Down Expand Up @@ -186,6 +193,13 @@ public <E extends Exception> void addFailure(String message, Throwable cause,
{
ValidationFailureImpl failure = new ValidationFailureImpl(configuration, message, cause, exceptionBuilder,
Set.of(checkedException));
for (ValidationFailure existingFailure : failures)
if (existingFailure.getMessage().equals(failure.getMessage()))
{
// Skip duplicate failure messages
return;
}

failures.add(failure);
if (configuration.throwOnFailure())
{
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.15</version>
<version>1.5.16</version>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
Expand Down Expand Up @@ -147,7 +147,7 @@
<dependency>
<groupId>org.twdata.maven</groupId>
<artifactId>mojo-executor</artifactId>
<version>2.4.1-m2</version>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
Expand All @@ -157,7 +157,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.1</version>
<version>3.27.2</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,9 +715,8 @@ public void multipleFailuresNullIsComparableTo()
TestValidators validators = TestValidators.of(scope);

Integer actual = null;
// Ensure that expectedMessages does not contain the same failure message twice
List<String> expectedMessages = List.of("""
"actual" must be equal to 5.
actual: null""", """
"actual" must be equal to 5.
actual: null""");
try (ConfigurationUpdater configurationUpdater = validators.updateConfiguration())
Expand Down

0 comments on commit 8986a91

Please sign in to comment.