square / assertj-android

A set of AssertJ helpers geared toward testing Android.
https://square.github.io/assertj-android/
Apache License 2.0
1.58k stars 156 forks source link

Palette support library module #109

Closed JakeWharton closed 10 years ago

makovkastar commented 10 years ago

Hi @JakeWharton. I implemented the Palette support library module, but the problem occured: the PaletteItem class doesn't override the equals method. So for example assertion:

public PaletteAssert hasVibrantColor(PaletteItem color) {
  isNotNull();
  PaletteItem actualColor = actual.getVibrantColor();
  assertThat(actualColor) //
      .overridingErrorMessage("Expected vibrant color <%s> but was <%s>", color, actualColor) //
      .isEqualTo(color);
  return this;
}

fails with the error for equal colors:

java.lang.AssertionError: Expected vibrant color <PaletteItem [ffeaaf11][HSL: [43.686638, 0.8645418,  
0.49215686]][Population: 1153]> but was <PaletteItem [ffeaaf11][HSL: [43.686638, 0.8645418, 
0.49215686]][Population: 1153]>

How to handle this?

JakeWharton commented 10 years ago

For now we can check equality manually. We should file a bug on b.android.com about this as well.

JakeWharton commented 10 years ago

Add a private static equals method in the assertion which takes two instances. Or you can also compare the toString output, but I like that a lot less.

makovkastar commented 10 years ago

Okay, I have opened a bug for this.

The problem with a manual equality check is that getPopulation() method is not public. However this attribute must be involved in the comparison. Using reflection is not a case, am I right?

So 2 options remain:

JakeWharton commented 10 years ago

Use toString for now. Thanks for filing and for the work. I'll take a look at the rest later. On Aug 3, 2014 12:00 PM, "Melnykov Oleksandr" notifications@github.com wrote:

Okay, I have opened a bug https://code.google.com/p/android/issues/detail?id=74434&thanks=74434&ts=1407091929 for this.

The problem with a manual equality check is that getPopulation() method is not public. However this attribute must be involved in the comparison. Using reflection is not a case, am I right?

So 2 options remain:

  • compare toString() outputs
  • wait until the equals(...) methid will be addded in the upcoming releases

— Reply to this email directly or view it on GitHub https://github.com/square/assertj-android/issues/109#issuecomment-50999444 .

f2prateek commented 10 years ago

Isn't there a equalsByComparingFields in assertj?

makovkastar commented 10 years ago

Yes, it has isEqualToComparingFieldByField method(and some other modifications), however it compares only accessible fields values. In our case getPopulation() is not accesible getter (it has the package-private visibility).