Comments (9)
I'm not ok with your correction, you forgot to set the Comparator in the StandardComparaisonStrategy.newSetUsingComparisonStrategy
try with this test (in Iterables_assertDoesNotHaveDuplicates_Test for example) :
@Test
public void should_fail_if_actual_contains_duplicates_array() {
AssertionInfo info = someInfo();
Collection<String[]> actual = newArrayList(new String[]{"Luke", "Yoda"}, new String[]{"Luke", "Yoda"});
Collection<String[]> duplicate = new LinkedHashSet<String[]>();
duplicate.add(new String[]{"Luke", "Yoda"});
try {
iterables.assertDoesNotHaveDuplicates(info, actual);
} catch (AssertionError e) {
return;
}
failBecauseExpectedAssertionErrorWasNotThrown();
}
The test is fine in the "old" master branch, but not in your correction.
In fact, the Object.areEqual have special test for array, so if you don't call him, this will fail.
from assertj.
You're right, good point.
I can either go back to your initial solution or define a comparator in StandardComparaisonStrategy using areEqual to test for equality but I'm not so sure of the comparator approach since, beside equality, it does not mean anything to compare objects.
WDYT ?
from assertj.
I'm trying a naïve approch like
protected Set<Object> newSetUsingComparisonStrategy() {
return new TreeSet<Object>(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return Objects.areEqual(o1, o2) ? 0 : -1;
}
});
}
works fine for Array, but new problem for handle null :
public class StandardComparisonStrategy_duplicatesFrom_Test extends AbstractTest_StandardComparisonStrategy {
@Test
public void should_return_existing_duplicates() {
Iterable<?> duplicates = standardComparisonStrategy
.duplicatesFrom(newArrayList("Merry", "Frodo", null, null, "Merry", "Sam", "Frodo"));
assertEquals(3, sizeOf(duplicates));
assertTrue(standardComparisonStrategy.iterableContains(duplicates, "Frodo"));
assertTrue(standardComparisonStrategy.iterableContains(duplicates, "Merry"));
assertTrue(standardComparisonStrategy.iterableContains(duplicates, null));
}
this test doesn't work anymore...
Will try harder and see if I can find a better approch (when my daughter sleep :P)
from assertj.
I was implementing the comparator solution to see what it looks like, handling null like this makes your test succeeds :
private StandardComparisonStrategy() {
// define a comparator so that we can use areEqual to compare objects in Set collections
// the "less than" comparison does not make much sense here but need to be defined.
comparator = new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
if (areEqual(o1, o2)) return 0;
// here, both objects can't be null but one can
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.hashCode() < o2.hashCode() ? -1 : 1;
}
};
}
from assertj.
for now, the best than I have :
protected Set<Object> newSetUsingComparisonStrategy() {
return new TreeSet<Object>(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
if (Objects.areEqual(o1, o2)) {
return 0;
}
return Objects.hashCodeFor(o1) - Objects.hashCodeFor(o2) < 0 ? -1 : 1;
}
});
}
Work with :
@Test
public void should_return_existing_duplicates_array() {
Iterable<?> duplicates = standardComparisonStrategy.duplicatesFrom(newArrayList(new String[] { "Merry" },
new String[] { "Frodo" }, new String[] { null }, new String[] { null }, new String[] { "Merry" },
new String[] { "Sam" }, new String[] { "Frodo" }));
assertTrue("must contains Frodo", standardComparisonStrategy.iterableContains(duplicates, new String[] { "Frodo" }));
assertTrue("must contains Merry", standardComparisonStrategy.iterableContains(duplicates, new String[] { "Merry" }));
assertTrue("must contains null", standardComparisonStrategy.iterableContains(duplicates, new String[] { null }));
assertEquals(3, sizeOf(duplicates));
}
in StandardComparisonStrategy_duplicatesFrom_Test
I try this last test with your strategy and he doen't pass
from assertj.
forget this, I have random failed..not sure hashCode is a good things..
from assertj.
can I change Objects.hashCodeFor to handle Array ?
from assertj.
Yeah was having the same problem.
William, let's consider chatting with gmail if you like , github not being very handy for that ;-)
My gmail is : joel.costigliola(at)gmail.com
from assertj.
Yep, we should do that, it will make it consistent with areEqual
from assertj.
Related Issues (20)
- Add AbstractOptionalAssert#hasValueSatisfying accepting ThrowableConsumer HOT 2
- Add a `containsInOrder` for collections HOT 1
- Move to Java 17 HOT 1
- Drop multi-release build in favor of module-only HOT 1
- `removeAssertJRelatedElementsFromStackTrace` does not remove all the JDK elements triggered by AssertJ HOT 12
- Deprecate `assertThat(Iterable, AssertFactory)` and `assertThat(Iterable, Class)`
- support for isImmutable() HOT 2
- `isUnmodifiable` assertion for `Iterator` instances HOT 3
- Standardize ShouldBeEqual and remove AssertionErrorFactory
- Failed containsOnlyNulls Assertion for Null-Extracted List Elements HOT 4
- Missing consistent order of Javadoc tags
- Merge assertj-core-java17 into assertj-core HOT 3
- Generating Javadoc in a Maven project using <detectLinks> does not work HOT 5
- Navigation to `assertj-core` or `guava` types from `assertj-guava` Javadoc site has unnecessary header
- `satisfiesOnlyOnce` stack trace does not point to failed nested assertion HOT 3
- Evaluate `DateTimeFormatter` as replacement of `SimpleDateFormat` for internal usage
- Support for Duration in Assertions.byLessThan, Assertions.within HOT 2
- Add an unwrap method to the API to get actual value HOT 3
- Unable to construct iterable assertions across a specific type easily HOT 7
- Ambiguous `assertThat` for `Temporal` instance implementing `Comparable` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from assertj.