Giter Site home page Giter Site logo

Comments (9)

twillouer avatar twillouer commented on July 20, 2024

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.

joel-costigliola avatar joel-costigliola commented on July 20, 2024

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.

twillouer avatar twillouer commented on July 20, 2024

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.

joel-costigliola avatar joel-costigliola commented on July 20, 2024

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.

twillouer avatar twillouer commented on July 20, 2024

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.

twillouer avatar twillouer commented on July 20, 2024

forget this, I have random failed..not sure hashCode is a good things..

from assertj.

twillouer avatar twillouer commented on July 20, 2024

can I change Objects.hashCodeFor to handle Array ?

from assertj.

joel-costigliola avatar joel-costigliola commented on July 20, 2024

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.

joel-costigliola avatar joel-costigliola commented on July 20, 2024

Yep, we should do that, it will make it consistent with areEqual

from assertj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.