-
-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track visited values and their comparison differences so that they ca…
…n be reused. Fix #2954 a case where values comparison differences were needed multiple times but the previous values tracking would incorrectly ignore them.
- Loading branch information
1 parent
7d2df9c
commit 8379cac
Showing
9 changed files
with
314 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
assertj-core/src/main/java/org/assertj/core/api/recursive/comparison/VisitedDualValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2023 the original author or authors. | ||
*/ | ||
package org.assertj.core.api.recursive.comparison; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static java.lang.String.format; | ||
|
||
class VisitedDualValues { | ||
|
||
private List<VisitedDualValue> dualValues; | ||
|
||
VisitedDualValues() { | ||
this.dualValues = new ArrayList<>(); | ||
} | ||
|
||
void registerVisitedDualValue(DualValue dualValue) { | ||
this.dualValues.add(new VisitedDualValue(dualValue)); | ||
} | ||
|
||
void registerComparisonDifference(DualValue dualValue, ComparisonDifference comparisonDifference) { | ||
this.dualValues.stream() | ||
// register difference on dual values agnostic of location, to take care of values visited several times | ||
.filter(visitedDualValue -> visitedDualValue.dualValue.sameValues(dualValue)) | ||
.findFirst() | ||
.ifPresent(visitedDualValue -> visitedDualValue.comparisonDifferences.add(comparisonDifference)); | ||
} | ||
|
||
Optional<List<ComparisonDifference>> registeredComparisonDifferencesOf(DualValue dualValue) { | ||
return this.dualValues.stream() | ||
// use sameValues to get already visited dual values with different location | ||
.filter(visitedDualValue -> visitedDualValue.dualValue.sameValues(dualValue)) | ||
.findFirst() | ||
.map(visitedDualValue -> visitedDualValue.comparisonDifferences); | ||
} | ||
|
||
private static class VisitedDualValue { | ||
DualValue dualValue; | ||
List<ComparisonDifference> comparisonDifferences; | ||
|
||
VisitedDualValue(DualValue dualValue) { | ||
this.dualValue = dualValue; | ||
this.comparisonDifferences = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return format("VisitedDualValue[dualValue=%s, comparisonDifferences=%s]", this.dualValue, this.comparisonDifferences); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...re/src/test/java/org/assertj/core/api/recursive/comparison/DualValue_sameValues_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2023 the original author or authors. | ||
*/ | ||
package org.assertj.core.api.recursive.comparison; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.recursive.comparison.FieldLocation.rootFieldLocation; | ||
import static org.assertj.core.util.Lists.list; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class DualValue_sameValues_Test { | ||
|
||
private static final List<String> PATH = list("foo", "bar"); | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void sameValues_should_return_true_when_dual_values_refer_to_the_same_instances(DualValue dualValue1, DualValue dualValue2) { | ||
assertThat(dualValue1.sameValues(dualValue2)).isTrue(); | ||
} | ||
|
||
static Stream<Arguments> sameValues_should_return_true_when_dual_values_refer_to_the_same_instances() { | ||
Object value1 = new Object(); | ||
Object value2 = new Object(); | ||
DualValue dualValue1 = new DualValue(rootFieldLocation(), value1, value2); | ||
DualValue dualValue2 = new DualValue(rootFieldLocation(), value1, value2); | ||
DualValue dualValue3 = new DualValue(rootFieldLocation().field("foo"), value1, value2); | ||
return Stream.of(arguments(dualValue1, dualValue2), | ||
arguments(dualValue1, dualValue1), | ||
arguments(dualValue2, dualValue1), | ||
arguments(dualValue1, dualValue3), | ||
arguments(dualValue1, dualValue3)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void sameValues_should_return_false_when_dual_values_refer_to_different_instances(DualValue dualValue1, DualValue dualValue2) { | ||
assertThat(dualValue1.sameValues(dualValue2)).isFalse(); | ||
} | ||
|
||
static Stream<Arguments> sameValues_should_return_false_when_dual_values_refer_to_different_instances() { | ||
Object value1 = new Object(); | ||
Object value2 = new Object(); | ||
Object value3 = new Object(); | ||
DualValue dualValue1 = new DualValue(rootFieldLocation(), value1, value2); | ||
DualValue dualValue2 = new DualValue(rootFieldLocation(), value1, value3); | ||
DualValue dualValue3 = new DualValue(rootFieldLocation().field("foo"), value1, value3); | ||
DualValue dualValue4 = new DualValue(rootFieldLocation(), new Object(), value2); | ||
return Stream.of(arguments(dualValue1, dualValue2), | ||
arguments(dualValue2, dualValue1), | ||
arguments(dualValue1, dualValue3), | ||
arguments(dualValue1, dualValue4)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.