Search in sources :

Example 16 with CheckReturnValue

use of org.assertj.core.util.CheckReturnValue in project assertj-core by joel-costigliola.

the class AbstractObjectArrayAssert method usingComparatorForType.

/**
 * Allows to set a specific comparator for the given type of elements or their fields.
 * Extends {@link #usingComparatorForElementFieldsWithType} by applying comparator specified for given type
 * to elements themselves, not only to their fields.
 * <p>
 * Usage of this method affects comparators set by the following methods:
 * <ul>
 * <li>{@link #usingFieldByFieldElementComparator}</li>
 * <li>{@link #usingElementComparatorOnFields}</li>
 * <li>{@link #usingElementComparatorIgnoringFields}</li>
 * <li>{@link #usingRecursiveFieldByFieldElementComparator}</li>
 * </ul>
 * <p>
 * Example:
 * <pre><code class='java'> Person obiwan = new Person("Obi-Wan");
 * obiwan.setHeight(new BigDecimal("1.820"));
 *
 * // assertion will pass
 * assertThat(obiwan).extracting("name", "height")
 *                   .usingComparatorForType(BIG_DECIMAL_COMPARATOR, BigDecimal.class)
 *                   .containsExactly("Obi-Wan", new BigDecimal("1.82"));</code></pre>
 *
 * @param <C> the type of elements to compare.
 * @param comparator the {@link java.util.Comparator} to use
 * @param type the {@link java.lang.Class} of the type of the element or element fields the comparator should be used for
 * @return {@code this} assertions object
 * @since 2.9.0 / 3.9.0
 */
@CheckReturnValue
public <C> SELF usingComparatorForType(Comparator<C> comparator, Class<C> type) {
    if (arrays.getComparator() == null) {
        usingElementComparator(new ExtendedByTypesComparator(getComparatorsByType()));
    }
    getComparatorsForElementPropertyOrFieldTypes().put(type, comparator);
    getComparatorsByType().put(type, comparator);
    return myself;
}
Also used : ExtendedByTypesComparator(org.assertj.core.internal.ExtendedByTypesComparator) CheckReturnValue(org.assertj.core.util.CheckReturnValue)

Example 17 with CheckReturnValue

use of org.assertj.core.util.CheckReturnValue in project assertj-core by joel-costigliola.

the class AbstractObjectArrayAssert method usingElementComparator.

/**
 * Use given custom comparator instead of relying on actual type A <code>equals</code> method to compare group
 * elements for incoming assertion checks.
 * <p>
 * Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default
 * comparison strategy.
 * <p>
 * Examples :
 * <pre><code class='java'> // compares invoices by payee
 * assertThat(invoiceArray).usingComparator(invoicePayeeComparator).isEqualTo(expectedinvoiceArray).
 *
 * // compares invoices by date, doesNotHaveDuplicates and contains both use the given invoice date comparator
 * assertThat(invoiceArray).usingComparator(invoiceDateComparator).doesNotHaveDuplicates().contains(may2010Invoice)
 *
 * // as assertThat(invoiceArray) creates a new assertion, it falls back to standard comparison strategy
 * // based on Invoice's equal method to compare invoiceArray elements to lowestInvoice.
 * assertThat(invoiceArray).contains(lowestInvoice).
 *
 * // standard comparison : the fellowshipOfTheRing includes Gandalf but not Sauron (believe me) ...
 * assertThat(fellowshipOfTheRing).contains(gandalf)
 *                                .doesNotContain(sauron);
 *
 * // ... but if we compare only races, Sauron is in fellowshipOfTheRing because he's a Maia like Gandalf.
 * assertThat(fellowshipOfTheRing).usingElementComparator(raceComparator)
 *                                .contains(sauron);</code></pre>
 *
 * @param elementComparator the comparator to use for incoming assertion checks.
 * @throws NullPointerException if the given comparator is {@code null}.
 * @return {@code this} assertion object.
 */
@Override
@CheckReturnValue
public SELF usingElementComparator(Comparator<? super ELEMENT> elementComparator) {
    this.arrays = new ObjectArrays(new ComparatorBasedComparisonStrategy(elementComparator));
    // to have the same semantics on base assertions like isEqualTo, we need to use an iterable comparator comparing
    // elements with elementComparator parameter
    objects = new Objects(new ObjectArrayElementComparisonStrategy<>(elementComparator));
    return myself;
}
Also used : ObjectArrayElementComparisonStrategy(org.assertj.core.internal.ObjectArrayElementComparisonStrategy) Objects(org.assertj.core.internal.Objects) ComparatorBasedComparisonStrategy(org.assertj.core.internal.ComparatorBasedComparisonStrategy) ObjectArrays(org.assertj.core.internal.ObjectArrays) CheckReturnValue(org.assertj.core.util.CheckReturnValue)

Example 18 with CheckReturnValue

use of org.assertj.core.util.CheckReturnValue in project assertj-core by joel-costigliola.

the class AbstractObjectAssert method extracting.

/**
 * Extract the values of given fields/properties from the object under test into a list, this new list becoming
 * the object under test.
 * <p>
 * If you extract "id", "name" and "email" fields/properties then the list will contain the id, name and email values
 * of the object under test, you can then perform list assertions on the extracted values.
 * <p>
 * Nested fields/properties are supported, specifying "adress.street.number" is equivalent to get the value
 * corresponding to actual.getAdress().getStreet().getNumber()
 * <p>
 * Private fields can be extracted unless you call {@link Assertions#setAllowExtractingPrivateFields(boolean) Assertions.setAllowExtractingPrivateFields(false)}.
 * <p>
 * If the object under test is a {@link Map} with {@link String} keys, extracting will extract values matching the given fields/properties.
 * <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(&quot;name&quot;, &quot;age&quot;, &quot;race.name&quot;)
 *                  .containsExactly(&quot;Frodo&quot;, 33, "Hobbit");</code></pre>
 *
 * A property with the given name is looked for first, if it doesn't exist then a field with the given name is looked
 * for, if the field is not accessible (i.e. does not exist) an {@link IntrospectionError} is thrown.
 * <p>
 * Note that the order of extracted values is consistent with the order of the given property/field.
 *
 * @param propertiesOrFields the properties/fields to extract from the initial object under test
 * @return a new assertion object whose object under test is the list containing the extracted properties/fields values
 * @throws IntrospectionError if one of the given name does not match a field or property
 */
@CheckReturnValue
public AbstractListAssert<?, List<? extends Object>, Object, ObjectAssert<Object>> extracting(String... propertiesOrFields) {
    Tuple values = byName(propertiesOrFields).extract(actual);
    String extractedPropertiesOrFieldsDescription = extractedDescriptionOf(propertiesOrFields);
    String description = mostRelevantDescription(info.description(), extractedPropertiesOrFieldsDescription);
    return newListAssertInstance(values.toList()).as(description);
}
Also used : Tuple(org.assertj.core.groups.Tuple) CheckReturnValue(org.assertj.core.util.CheckReturnValue)

Example 19 with CheckReturnValue

use of org.assertj.core.util.CheckReturnValue in project assertj-core by joel-costigliola.

the class AbstractIntegerAssert method usingComparator.

@Override
@CheckReturnValue
public SELF usingComparator(Comparator<? super Integer> customComparator, String customComparatorDescription) {
    super.usingComparator(customComparator, customComparatorDescription);
    integers = new Integers(new ComparatorBasedComparisonStrategy(customComparator, customComparatorDescription));
    return myself;
}
Also used : Integers(org.assertj.core.internal.Integers) ComparatorBasedComparisonStrategy(org.assertj.core.internal.ComparatorBasedComparisonStrategy) CheckReturnValue(org.assertj.core.util.CheckReturnValue)

Example 20 with CheckReturnValue

use of org.assertj.core.util.CheckReturnValue in project assertj-core by joel-costigliola.

the class AbstractListAssert method usingElementComparator.

@Override
@CheckReturnValue
public SELF usingElementComparator(Comparator<? super ELEMENT> customComparator) {
    super.usingElementComparator(customComparator);
    lists = new Lists(new ComparatorBasedComparisonStrategy(customComparator));
    return myself;
}
Also used : Lists(org.assertj.core.internal.Lists) ComparatorBasedComparisonStrategy(org.assertj.core.internal.ComparatorBasedComparisonStrategy) CheckReturnValue(org.assertj.core.util.CheckReturnValue)

Aggregations

CheckReturnValue (org.assertj.core.util.CheckReturnValue)24 ComparatorBasedComparisonStrategy (org.assertj.core.internal.ComparatorBasedComparisonStrategy)18 ExtendedByTypesComparator (org.assertj.core.internal.ExtendedByTypesComparator)3 Objects (org.assertj.core.internal.Objects)3 Tuple (org.assertj.core.groups.Tuple)2 Comparables (org.assertj.core.internal.Comparables)2 Integers (org.assertj.core.internal.Integers)2 Longs (org.assertj.core.internal.Longs)2 ObjectArrays (org.assertj.core.internal.ObjectArrays)2 AtomicReferenceArrayElementComparisonStrategy (org.assertj.core.internal.AtomicReferenceArrayElementComparisonStrategy)1 BigDecimals (org.assertj.core.internal.BigDecimals)1 BigIntegers (org.assertj.core.internal.BigIntegers)1 Bytes (org.assertj.core.internal.Bytes)1 Characters (org.assertj.core.internal.Characters)1 Dates (org.assertj.core.internal.Dates)1 Doubles (org.assertj.core.internal.Doubles)1 Floats (org.assertj.core.internal.Floats)1 IterableElementComparisonStrategy (org.assertj.core.internal.IterableElementComparisonStrategy)1 Iterables (org.assertj.core.internal.Iterables)1 Lists (org.assertj.core.internal.Lists)1