Skip to content

Commit

Permalink
Fixes #571 : add lambda support to Object extracting
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-costigliola committed Jan 6, 2016
1 parent e4b7372 commit 5cc5266
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
Expand Up @@ -13,8 +13,6 @@
package org.assertj.core.api;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
import static org.assertj.core.api.filter.Filters.filter;
import static org.assertj.core.extractor.Extractors.byName;
import static org.assertj.core.extractor.Extractors.resultOf;
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/org/assertj/core/api/AbstractObjectAssert.java
Expand Up @@ -14,6 +14,9 @@

import static org.assertj.core.extractor.Extractors.byName;

import java.util.function.Function;
import java.util.stream.Stream;

import org.assertj.core.groups.Tuple;
import org.assertj.core.util.introspection.IntrospectionError;

Expand Down Expand Up @@ -299,7 +302,7 @@ public S hasFieldOrPropertyWithValue(String name, Object value) {
* Private fields can be extracted unless you call {@link Assertions#setAllowExtractingPrivateFields(boolean) Assertions.setAllowExtractingPrivateFields(false)}.
* <p/>
* Example:
* <pre><code class='java'> // Create frodo, setting its name, age and Race fields (Race having a name field)
* <pre><code class='java'> // Create frodo, setting its name, age and Race (Race having a name property)
* TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);
*
* // let's verify Frodo's name, age and race name:
Expand All @@ -320,4 +323,34 @@ public AbstractObjectArrayAssert<?, Object> extracting(String... propertiesOrFie
Tuple values = byName(propertiesOrFields).extract(actual);
return new ObjectArrayAssert<Object>(values.toArray());
}

/**
* Use the given {@link Function}s to extract the values from the object under test into an array, this new array becoming
* the object under test.
* <p/>
* If the given {@link Function}s extract the id, name and email values then the array will contain the id, name and email values
* of the object under test, you can then perform array assertions on the extracted values.
* <p/>
* Example:
* <pre><code class='java'> // Create frodo, setting its name, age and Race (Race having a name property)
* TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);
*
* // let's verify Frodo's name, age and race name:
* assertThat(frodo).extracting(TolkienCharacter::getName,
* character -> character.age, // public field
* character -> character.getRace().getName())
* .containsExactly(&quot;Frodo&quot;, 33, "Hobbit");</code></pre>
* <p/>
* Note that the order of extracted values is consistent with the iteration order of the array under test.
*
* @param extractors the extractor functions to extract a value from an element of the Iterable under test.
* @return a new assertion object whose object under test is the array containing the extracted values
*/
@SafeVarargs
public final AbstractObjectArrayAssert<?, Object> extracting(Function<? super A, Object>... extractors) {
Object[] values = Stream.of(extractors)
.map(extractor -> extractor.apply(actual))
.toArray();
return new ObjectArrayAssert<Object>(values);
}
}
Expand Up @@ -17,18 +17,38 @@
import org.assertj.core.api.ObjectAssert;
import org.assertj.core.test.Employee;
import org.assertj.core.test.Name;
import org.junit.Before;
import org.junit.Test;

/**
* Tests for <code>{@link ObjectAssert#extracting(String[])}</code>.
*/
public class ObjectAssert_extracting_Test {

private Employee luke;

@Before
public void setup() {
luke = new Employee(2L, new Name("Luke", "Skywalker"), 26);
}

@Test
public void should_allow_assertions_on_array_of_properties_extracted_from_given_object_by_name() {
assertThat(luke).extracting("id", "name")
.hasSize(2)
.doesNotContainNull();
assertThat(luke).extracting("name.first", "name.last")
.hasSize(2)
.containsExactly("Luke", "Skywalker");
}

@Test
public void should_allow_assertions_on_array_of_property_values_extracted_from_given_object() {
Employee luke = new Employee(2L, new Name("Luke", "Skywalker"), 26);

assertThat(luke).extracting("id", "name").doesNotContainNull();
assertThat(luke).extracting("name.first", "name.last").containsExactly("Luke", "Skywalker");
public void should_allow_assertions_on_array_of_properties_extracted_from_given_object_with_lambdas() {
assertThat(luke).extracting(Employee::getName, Employee::getAge)
.hasSize(2)
.doesNotContainNull();
assertThat(luke).extracting(employee -> employee.getName().first, employee -> employee.getName().getLast())
.hasSize(2)
.containsExactly("Luke", "Skywalker");
}
}

0 comments on commit 5cc5266

Please sign in to comment.