Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import static java.util.Objects.requireNonNull;
import static org.assertj.core.error.ElementsShouldMatch.elementsShouldMatch;
import static org.assertj.core.error.ElementsShouldSatisfy.elementsShouldSatisfy;
import static org.assertj.core.error.ShouldBeAnArray.shouldBeAnArray;
import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.error.ShouldBeNullOrEmpty.shouldBeNullOrEmpty;
import static org.assertj.core.error.ShouldContain.shouldContain;
import static org.assertj.core.error.ShouldHaveSameSizeAs.shouldHaveSameSizeAs;
import static org.assertj.core.error.ShouldHaveSize.shouldHaveSize;
import static org.assertj.core.error.ShouldHaveSizeBetween.shouldHaveSizeBetween;
Expand All @@ -33,15 +35,22 @@

import java.lang.reflect.Array;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

import org.assertj.core.annotation.CheckReturnValue;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractIterableAssert;
import org.assertj.core.api.ThrowingConsumer;
import org.assertj.core.error.UnsatisfiedRequirement;
import org.assertj.core.presentation.PredicateDescription;
import org.eclipse.collections.api.PrimitiveIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.list.fixed.ArrayAdapter;

/**
* Base class for implementations of Eclipse Collections {@link RichIterable} assertions.
Expand Down Expand Up @@ -88,6 +97,57 @@ private void assertAllMatch(Predicate<? super ELEMENT> predicate, PredicateDescr
throw assertionError(elementsShouldMatch(actual, nonMatches.size() == 1 ? nonMatches.getFirst() : nonMatches, predicateDescription));
}

@Override
public SELF allSatisfy(Consumer<? super ELEMENT> requirements) {
return executeAssertion(() -> assertAllSatisfy(requirements));
}

@Override
public SELF allSatisfy(ThrowingConsumer<? super ELEMENT> requirements) {
return allSatisfy(((Consumer<? super ELEMENT>) requirements));
}

private void assertAllSatisfy(Consumer<? super ELEMENT> requirements) {
isNotNull();
isNotEmpty();
requireNonNull(requirements, "The Consumer<T> expressing the assertions requirements must not be null");

RichIterable<UnsatisfiedRequirement> unsatisfiedRequirements = actual.collect(element -> failsRequirements(requirements, element))
.collectIf(Optional::isPresent, Optional::get);
if (unsatisfiedRequirements.isEmpty()) {
return;
}

throw assertionError(elementsShouldSatisfy(actual, unsatisfiedRequirements.toList(), info));
}

private static <E> Optional<UnsatisfiedRequirement> failsRequirements(Consumer<? super E> requirements, E element) {
try {
requirements.accept(element);
} catch (AssertionError ex) {
return Optional.of(new UnsatisfiedRequirement(element, ex));
}
return Optional.empty();
}

@Override
protected void assertContains(ELEMENT[] values) {
isNotNull();
requireNonNull(values, "The array of values to look for should not be null");

if (actual.isEmpty() && values.length == 0) {
return;
}

ArrayAdapter<ELEMENT> valuesList = ArrayAdapter.adapt(values);
MutableList<ELEMENT> notFound = valuesList.reject(actual::contains);
if (notFound.isEmpty()) {
return;
}

throw assertionError(shouldContain(actual, valuesList, notFound)); // TODO: ComparisonStrategy???
}

@Override
@CheckReturnValue
public <T> SELF filteredOn(Function<? super ELEMENT, T> function, T expectedValue) {
Expand Down Expand Up @@ -169,6 +229,28 @@ public SELF hasSameSizeAs(Object other) {
});
}

/**
* Verifies that the actual RichIterable matches the size of the given primitive iterable.
*
* @param other the primitive iterable to compare size with
* @return {@code this} assertion object
* @throws AssertionError if the actual RichIterable is {@code null}
* @throws AssertionError if the actual RichIterable does not have the same size as the given primitive iterable
*/
public SELF hasSameSizeAs(PrimitiveIterable other) {
return executeAssertion(() -> {
isNotNull();

int actualSize = actual.size();
int otherSize = sizeOf(other);
if (actualSize == otherSize) {
return;
}

throw assertionError(shouldHaveSameSizeAs(actual, other, actualSize, otherSize));
});
}

/**
* Verifies that the number of values in the actual RichIterable is equal to the given one.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,73 @@
*/
package org.assertj.eclipse.collections.api;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.error.ElementsShouldMatch.elementsShouldMatch;
import static org.assertj.core.error.ElementsShouldSatisfy.elementsShouldSatisfy;
import static org.assertj.core.error.ShouldContain.shouldContain;

import java.util.Optional;

import org.assertj.core.error.UnsatisfiedRequirement;
import org.assertj.core.presentation.PredicateDescription;
import org.eclipse.collections.api.BooleanIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.predicate.primitive.BooleanPredicate;
import org.eclipse.collections.api.block.procedure.primitive.BooleanProcedure;
import org.eclipse.collections.api.factory.primitive.BooleanLists;
import org.eclipse.collections.api.list.primitive.BooleanList;

public class BooleanIterableAssert extends AbstractPrimitiveIterableAssert<BooleanIterableAssert, BooleanIterable> {
public BooleanIterableAssert(BooleanIterable actual) {
super(actual, BooleanIterableAssert.class);
}

public BooleanIterableAssert allMatch(BooleanPredicate predicate) {
return executeAssertion(() -> assertAllMatch(predicate, PredicateDescription.GIVEN));
}

public BooleanIterableAssert allMatch(BooleanPredicate predicate, String predicateDescription) {
return executeAssertion(() -> assertAllMatch(predicate, new PredicateDescription(predicateDescription)));
}

private void assertAllMatch(BooleanPredicate predicate, PredicateDescription predicateDescription) {
isNotNull();
requireNonNull(predicate, "The predicate to evaluate should not be null");
isNotEmpty();

BooleanList nonMatches = actual.reject(predicate).toList();
if (nonMatches.isEmpty()) {
return;
}

throw assertionError(elementsShouldMatch(actual, nonMatches.size() == 1 ? nonMatches.getFirst() : nonMatches, predicateDescription));
}

public BooleanIterableAssert allSatisfy(BooleanProcedure requirements) {
return executeAssertion(() -> {
isNotNull();
isNotEmpty();
requireNonNull(requirements, "The BooleanProcedure expressing the assertions requirements must not be null");

RichIterable<UnsatisfiedRequirement> unsatisfiedRequirements = actual.collect(element -> failsRequirements(requirements, element))
.collectIf(Optional::isPresent, Optional::get);
if (unsatisfiedRequirements.isEmpty()) {
return;
}

throw assertionError(elementsShouldSatisfy(actual, unsatisfiedRequirements.toList(), info));
});
}

private static Optional<UnsatisfiedRequirement> failsRequirements(BooleanProcedure requirements, boolean element) {
try {
requirements.value(element);
} catch (AssertionError ex) {
return Optional.of(new UnsatisfiedRequirement(element, ex));
}
return Optional.empty();
}

public BooleanIterableAssert contains(boolean... values) {
return executeAssertion(() -> {
isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,74 @@
*/
package org.assertj.eclipse.collections.api;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.error.ElementsShouldMatch.elementsShouldMatch;
import static org.assertj.core.error.ElementsShouldSatisfy.elementsShouldSatisfy;
import static org.assertj.core.error.ShouldContain.shouldContain;

import java.util.Optional;

import org.assertj.core.error.UnsatisfiedRequirement;
import org.assertj.core.presentation.PredicateDescription;
import org.eclipse.collections.api.ByteIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.predicate.primitive.BytePredicate;
import org.eclipse.collections.api.block.procedure.primitive.ByteProcedure;
import org.eclipse.collections.api.factory.primitive.ByteLists;
import org.eclipse.collections.api.list.primitive.ByteList;

public class ByteIterableAssert extends AbstractPrimitiveIterableAssert<ByteIterableAssert, ByteIterable> {

public ByteIterableAssert(ByteIterable actual) {
super(actual, ByteIterableAssert.class);
}

public ByteIterableAssert allMatch(BytePredicate predicate) {
return executeAssertion(() -> assertAllMatch(predicate, PredicateDescription.GIVEN));
}

public ByteIterableAssert allMatch(BytePredicate predicate, String predicateDescription) {
return executeAssertion(() -> assertAllMatch(predicate, new PredicateDescription(predicateDescription)));
}

private void assertAllMatch(BytePredicate predicate, PredicateDescription predicateDescription) {
isNotNull();
requireNonNull(predicate, "The predicate to evaluate should not be null");
isNotEmpty();

ByteList nonMatches = actual.reject(predicate).toList();
if (nonMatches.isEmpty()) {
return;
}

throw assertionError(elementsShouldMatch(actual, nonMatches.size() == 1 ? nonMatches.getFirst() : nonMatches, predicateDescription));
}

public ByteIterableAssert allSatisfy(ByteProcedure requirements) {
return executeAssertion(() -> {
isNotNull();
isNotEmpty();
requireNonNull(requirements, "The ByteProcedure expressing the assertions requirements must not be null");

RichIterable<UnsatisfiedRequirement> unsatisfiedRequirements = actual.collect(element -> failsRequirements(requirements, element))
.collectIf(Optional::isPresent, Optional::get);
if (unsatisfiedRequirements.isEmpty()) {
return;
}

throw assertionError(elementsShouldSatisfy(actual, unsatisfiedRequirements.toList(), info));
});
}

private static Optional<UnsatisfiedRequirement> failsRequirements(ByteProcedure requirements, byte element) {
try {
requirements.value(element);
} catch (AssertionError ex) {
return Optional.of(new UnsatisfiedRequirement(element, ex));
}
return Optional.empty();
}

public ByteIterableAssert contains(byte... values) {
return executeAssertion(() -> {
isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,73 @@
*/
package org.assertj.eclipse.collections.api;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.error.ElementsShouldMatch.elementsShouldMatch;
import static org.assertj.core.error.ElementsShouldSatisfy.elementsShouldSatisfy;
import static org.assertj.core.error.ShouldContain.shouldContain;

import java.util.Optional;

import org.assertj.core.error.UnsatisfiedRequirement;
import org.assertj.core.presentation.PredicateDescription;
import org.eclipse.collections.api.CharIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.predicate.primitive.CharPredicate;
import org.eclipse.collections.api.block.procedure.primitive.CharProcedure;
import org.eclipse.collections.api.factory.primitive.CharLists;
import org.eclipse.collections.api.list.primitive.CharList;

public class CharIterableAssert extends AbstractPrimitiveIterableAssert<CharIterableAssert, CharIterable> {
public CharIterableAssert(CharIterable actual) {
super(actual, CharIterableAssert.class);
}

public CharIterableAssert allMatch(CharPredicate predicate) {
return executeAssertion(() -> assertAllMatch(predicate, PredicateDescription.GIVEN));
}

public CharIterableAssert allMatch(CharPredicate predicate, String predicateDescription) {
return executeAssertion(() -> assertAllMatch(predicate, new PredicateDescription(predicateDescription)));
}

private void assertAllMatch(CharPredicate predicate, PredicateDescription predicateDescription) {
isNotNull();
requireNonNull(predicate, "The predicate to evaluate should not be null");
isNotEmpty();

CharList nonMatches = actual.reject(predicate).toList();
if (nonMatches.isEmpty()) {
return;
}

throw assertionError(elementsShouldMatch(actual, nonMatches.size() == 1 ? nonMatches.getFirst() : nonMatches, predicateDescription));
}

public CharIterableAssert allSatisfy(CharProcedure requirements) {
return executeAssertion(() -> {
isNotNull();
isNotEmpty();
requireNonNull(requirements, "The CharProcedure expressing the assertions requirements must not be null");

RichIterable<UnsatisfiedRequirement> unsatisfiedRequirements = actual.collect(element -> failsRequirements(requirements, element))
.collectIf(Optional::isPresent, Optional::get);
if (unsatisfiedRequirements.isEmpty()) {
return;
}

throw assertionError(elementsShouldSatisfy(actual, unsatisfiedRequirements.toList(), info));
});
}

private static Optional<UnsatisfiedRequirement> failsRequirements(CharProcedure requirements, char element) {
try {
requirements.value(element);
} catch (AssertionError ex) {
return Optional.of(new UnsatisfiedRequirement(element, ex));
}
return Optional.empty();
}

public CharIterableAssert contains(char... values) {
return executeAssertion(() -> {
isNotNull();
Expand Down
Loading
Loading