Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

containsExactly does not honor comparator set with usingElementComparator for arrays #249

Closed
fbiville opened this issue Oct 9, 2014 · 4 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@fbiville
Copy link
Contributor

fbiville commented Oct 9, 2014

The following test fails with AssertJ core (1.7 included).

@Test
public void should_test_contains_exactly() {
      List<String> strings = new ArrayList<String>();
      strings.add("test");
      strings.add("de taille de string");

      Comparator<String> comparator = new Comparator<String>() {
         @Override
         public int compare(String o1, String o2) {
            return (o1.length() > o2.length()) ? -1 : 1;
         }
      };
      assertThat(strings).usingElementComparator(comparator).containsExactly("de taille de string", "test");
   }
}
@joel-costigliola
Copy link
Member

The problem comes from your comparator that does not assess equality.

comparator.compare("Biville", "Biville"); // -> return 1

containsExactly relies on comparator to assess equality.

Not a bug in my opinion, do you have another use case in mind ?

@fbiville
Copy link
Contributor Author

Héhé, well spot :D (in my defence, I posted it for a colleague)

@Zapata
Copy link

Zapata commented Jan 26, 2015

Hello,

I have a similar issue:

public class CompareTest {

    public static class StartOfStringCompare implements Comparator<String> {
        private final int scale;

        public StartOfStringCompare(int scale) {
            this.scale = scale;
        }

        @Override
        public int compare(String o1, String o2) {
            return o1.substring(0, Math.min(o1.length() - 1, scale)).compareTo(
                    o2.substring(0, Math.min(o2.length() - 1, scale)));
        }
    }

    @Test
    public void should_test_contains_exactly() {
        assertThat(new StartOfStringCompare(3).compare("abcdef", "abcz"))
                .isEqualTo(0);

        assertThat(new String[] { "abcdef" }).usingElementComparator(
                new StartOfStringCompare(3)).containsExactly("abcz");
    }
}

I am missing something?
If a put a breakpoint into StartOfStringCompare.compare(), it's not called by containsExactly.
Version 1.7.1.

@joel-costigliola
Copy link
Member

No that's a bug indeed, specific to containsExactly so in the meantime you can use containsOnly it's the closest assertion (it just does not check the order of elements).

Thanks for reporting this.

@joel-costigliola joel-costigliola added the type: bug A general bug label Jan 26, 2015
@joel-costigliola joel-costigliola changed the title Bug: usingElementComparator does not work with containsExactly containsExactly does not honor comparator set with usingElementComparator Jan 26, 2015
@joel-costigliola joel-costigliola added this to the 2.0 milestone Feb 2, 2015
@joel-costigliola joel-costigliola self-assigned this Feb 6, 2015
@joel-costigliola joel-costigliola changed the title containsExactly does not honor comparator set with usingElementComparator containsExactly does not honor comparator set with usingElementComparator for arrays Feb 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants