fest-assert-2.x icon indicating copy to clipboard operation
fest-assert-2.x copied to clipboard

Feature request: extractProperty() to use fields directly

Open japgolly opened this issue 12 years ago • 7 comments

Any chance we could have extractProperty() fall back to supporting fields when a getter doesn't exist.

E.g.

class Animal {
    public String name;
}

japgolly avatar Nov 19 '12 05:11 japgolly

I think we could :)

joel-costigliola avatar Nov 19 '12 11:11 joel-costigliola

That's great! This will be good in conjunction with Android testing because with older versions of Android, the JVM or compiler doesn't inline getters (or something to that effect) so using fields directly rather than getter methods improves actually performance.

japgolly avatar Nov 20 '12 12:11 japgolly

You can use FEST-Reflect to extract the field. No need to duplicate that in FEST-Assert. The question would be if FEST-Reflect works with Android. If not, we can fix FEST-Reflect.

alexruiz avatar Jan 10 '13 15:01 alexruiz

I don't think it's an "so" easy fix, because the instrospection used by the extractProperty (see PropertySupport#propertyValue) is based on PropertyDescriptor, and according to the javadoc, it "describes one property that a Java Bean exports via a pair of accessor methods".

by extension to this issue, it can be usefull to have an extract method, and not only for properties.

class Basket {
         public BigDecimal price() {
                   return a_computed_price; 
         }
}

and the associated test can looks like :

assertThat(extract("price").from(baskets)).containsOnly("12.65", "42.00", "66.60");

dwursteisen avatar Feb 25 '13 17:02 dwursteisen

For your information, this issue has been fixed in AssertJ a fork of Fest Assert 2.0M10.

The syntax is a little different though :

// "name" need to be either a property or a public field of TolkienCharacter class.
assertThat(fellowshipOfTheRing).extracting("name")
                               .contains("Boromir", "Gandalf", "Frodo", "Legolas")
                               .doesNotContain("Sauron", "Elrond");

It also supports extracting several fields/properties :

// tuple comes from : import static org.assertj.core.api.Assertions.tuple;
assertThat(fellowshipOfTheRing).extracting("name", "age", "race.name")
                               .contains(tuple("Boromir", 37, "Man"),
                                         tuple("Sam", 38, "Hobbit"),
                                         tuple("Legolas", 1000, "Elf"));

joel-costigliola avatar Apr 15 '13 09:04 joel-costigliola

Nice. So that means that this will be in the next version of Fest Assert, like 2.0M11?

japgolly avatar Apr 28 '13 00:04 japgolly

It is available in AssertJhttps://github.com/joel-costigliola/assertj-core#readme, but I can't tell if it will be in Fest Assert, only Alex can answer that.

Regards,

Joel

On Sun, Apr 28, 2013 at 2:27 AM, David Barri [email protected]:

Nice. So that means that this will be in the next version of Fest Assert, like 2.0M11?

— Reply to this email directly or view it on GitHubhttps://github.com/alexruiz/fest-assert-2.x/issues/128#issuecomment-17125946 .

joel-costigliola avatar Apr 28 '13 13:04 joel-costigliola