Search in sources :

Example 1 with ExtendedByTypesComparator

use of org.assertj.core.internal.ExtendedByTypesComparator in project assertj-core by joel-costigliola.

the class AbstractIterableAssert 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 next 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'>
 *     // assertion will pass
 *     assertThat(asList("some", new BigDecimal("4.2")))
 *         .usingComparatorForType(BIG_DECIMAL_COMPARATOR, BigDecimal.class)
 *         .contains(new BigDecimal("4.20"));
 * </code></pre>
 *
 * @param <T> 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 <T> SELF usingComparatorForType(Comparator<T> comparator, Class<T> type) {
    if (iterables.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 2 with ExtendedByTypesComparator

use of org.assertj.core.internal.ExtendedByTypesComparator 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 3 with ExtendedByTypesComparator

use of org.assertj.core.internal.ExtendedByTypesComparator in project assertj-core by joel-costigliola.

the class AtomicReferenceArrayAssert 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 next 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'> // assertion will pass
 * assertThat(new AtomicReferenceArray&lt;&gt;(new Object[] { "some", new BigDecimal("4.2") }))
 *       .usingComparatorForType(BIG_DECIMAL_COMPARATOR, BigDecimal.class)
 *       .contains(new BigDecimal("4.20"));
 * </code></pre>
 *
 * @param <C> the type 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> AtomicReferenceArrayAssert<T> 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 4 with ExtendedByTypesComparator

use of org.assertj.core.internal.ExtendedByTypesComparator in project assertj-core by joel-costigliola.

the class ListAssert_usingElementComparatorOnFields_Test method verify_internal_effects.

@Override
protected void verify_internal_effects() {
    Lists lists = getLists(assertions);
    Iterables iterables = getIterables(assertions);
    assertThat(lists).isNotSameAs(listsBefore);
    assertThat(iterables).isNotSameAs(iterablesBefore);
    assertThat(iterables.getComparisonStrategy()).isInstanceOf(ComparatorBasedComparisonStrategy.class);
    assertThat(lists.getComparisonStrategy()).isInstanceOf(ComparatorBasedComparisonStrategy.class);
    Comparator<?> listsElementComparator = ((ExtendedByTypesComparator) ((ComparatorBasedComparisonStrategy) lists.getComparisonStrategy()).getComparator()).getComparator();
    assertThat(listsElementComparator).isInstanceOf(OnFieldsComparator.class);
    assertThat(((OnFieldsComparator) listsElementComparator).getFields()).containsOnly("field");
    Comparator<?> iterablesElementComparator = ((ExtendedByTypesComparator) ((ComparatorBasedComparisonStrategy) iterables.getComparisonStrategy()).getComparator()).getComparator();
    assertThat(iterablesElementComparator).isInstanceOf(OnFieldsComparator.class);
    assertThat(((OnFieldsComparator) iterablesElementComparator).getFields()).containsOnly("field");
}
Also used : Lists(org.assertj.core.internal.Lists) OnFieldsComparator(org.assertj.core.internal.OnFieldsComparator) Iterables(org.assertj.core.internal.Iterables) ExtendedByTypesComparator(org.assertj.core.internal.ExtendedByTypesComparator)

Example 5 with ExtendedByTypesComparator

use of org.assertj.core.internal.ExtendedByTypesComparator in project assertj-core by joel-costigliola.

the class ListAssert_usingFieldByFieldElementComparator_Test method verify_internal_effects.

@Override
protected void verify_internal_effects() {
    Lists lists = getLists(assertions);
    Iterables iterables = getIterables(assertions);
    assertThat(lists).isNotSameAs(listsBefore);
    assertThat(iterables).isNotSameAs(iterablesBefore);
    assertThat(iterables.getComparisonStrategy()).isInstanceOf(ComparatorBasedComparisonStrategy.class);
    assertThat(lists.getComparisonStrategy()).isInstanceOf(ComparatorBasedComparisonStrategy.class);
    Comparator<?> listsElementComparator = ((ExtendedByTypesComparator) ((ComparatorBasedComparisonStrategy) lists.getComparisonStrategy()).getComparator()).getComparator();
    assertThat(listsElementComparator).isInstanceOf(FieldByFieldComparator.class);
    Comparator<?> iterablesElementComparator = ((ExtendedByTypesComparator) ((ComparatorBasedComparisonStrategy) iterables.getComparisonStrategy()).getComparator()).getComparator();
    assertThat(iterablesElementComparator).isInstanceOf(FieldByFieldComparator.class);
}
Also used : Lists(org.assertj.core.internal.Lists) Iterables(org.assertj.core.internal.Iterables) ExtendedByTypesComparator(org.assertj.core.internal.ExtendedByTypesComparator)

Aggregations

ExtendedByTypesComparator (org.assertj.core.internal.ExtendedByTypesComparator)5 CheckReturnValue (org.assertj.core.util.CheckReturnValue)3 Iterables (org.assertj.core.internal.Iterables)2 Lists (org.assertj.core.internal.Lists)2 OnFieldsComparator (org.assertj.core.internal.OnFieldsComparator)1