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
5 changes: 3 additions & 2 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
module org.assertj.eclipse.collections {
exports org.assertj.eclipse.collections.api;
exports org.assertj.eclipse.collections.error;
exports org.assertj.eclipse.collections.util;

requires org.assertj.core;
requires org.eclipse.collections.api;
requires transitive org.assertj.core;
requires transitive org.eclipse.collections.api;
requires org.eclipse.collections.impl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
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.ShouldBeAnArray.shouldBeAnArray;
import static org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.error.ShouldBeNullOrEmpty.shouldBeNullOrEmpty;
Expand All @@ -26,8 +28,8 @@
import static org.assertj.core.error.ShouldHaveSizeLessThan.shouldHaveSizeLessThan;
import static org.assertj.core.error.ShouldHaveSizeLessThanOrEqualTo.shouldHaveSizeLessThanOrEqualTo;
import static org.assertj.core.error.ShouldNotBeEmpty.shouldNotBeEmpty;
import static org.assertj.core.util.IterableUtil.sizeOf;
import static org.assertj.core.util.Preconditions.checkArgument;
import static org.assertj.eclipse.collections.util.RichIterableUtil.sizeOf;

import java.lang.reflect.Array;
import java.util.Objects;
Expand All @@ -37,7 +39,9 @@
import org.assertj.core.annotation.CheckReturnValue;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractIterableAssert;
import org.assertj.core.presentation.PredicateDescription;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.list.ImmutableList;

/**
* Base class for implementations of Eclipse Collections {@link RichIterable} assertions.
Expand All @@ -61,6 +65,29 @@ protected AbstractRichIterableAssert(ACTUAL actual, Class<?> selfType) {
super(actual, selfType);
}

@Override
public SELF allMatch(Predicate<? super ELEMENT> predicate) {
return executeAssertion(() -> assertAllMatch(predicate, PredicateDescription.GIVEN));
}

@Override
public SELF allMatch(Predicate<? super ELEMENT> predicate, String predicateDescription) {
return executeAssertion(() -> assertAllMatch(predicate, new PredicateDescription(predicateDescription)));
}

private void assertAllMatch(Predicate<? super ELEMENT> predicate, PredicateDescription predicateDescription) {
isNotNull();
requireNonNull(predicate, "The predicate to evaluate should not be null");
isNotEmpty();

ImmutableList<? extends ELEMENT> nonMatches = actual.reject(predicate::test).toImmutableList();
if (nonMatches.isEmpty()) {
return;
}

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

@Override
@CheckReturnValue
public <T> SELF filteredOn(Function<? super ELEMENT, T> function, T expectedValue) {
Expand Down Expand Up @@ -103,20 +130,16 @@ public SELF filteredOn(Predicate<? super ELEMENT> predicate) {
*/
@Override
public SELF hasSameSizeAs(Iterable<?> other) {
isNotNull();

final int otherSize;
if (other instanceof RichIterable<?> richIterable) {
otherSize = richIterable.size();
} else {
otherSize = sizeOf(other);
}

int actualSize = actual.size();
if (actualSize == otherSize) {
return myself;
}
throw assertionError(shouldHaveSameSizeAs(actual, other, actualSize, otherSize));
return executeAssertion(() -> {
isNotNull();

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

/**
Expand All @@ -129,19 +152,21 @@ public SELF hasSameSizeAs(Iterable<?> other) {
*/
@Override
public SELF hasSameSizeAs(Object other) {
isNotNull();
return executeAssertion(() -> {
isNotNull();

if (!(other != null && other.getClass().isArray())) {
throw assertionError(shouldBeAnArray(other));
}
if (!(other != null && other.getClass().isArray())) {
throw assertionError(shouldBeAnArray(other));
}

int otherSize = Array.getLength(other);
int actualSize = actual.size();
if (actualSize == otherSize) {
return myself;
}
int otherSize = Array.getLength(other);
int actualSize = actual.size();
if (actualSize == otherSize) {
return;
}

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

/**
Expand All @@ -164,14 +189,16 @@ public SELF hasSameSizeAs(Object other) {
*/
@Override
public SELF hasSize(int expected) {
isNotNull();
return executeAssertion(() -> {
isNotNull();

int actualSize = actual.size();
if (actualSize == expected) {
return myself;
}
int actualSize = actual.size();
if (actualSize == expected) {
return;
}

throw assertionError(shouldHaveSize(actual, actualSize, expected));
throw assertionError(shouldHaveSize(actual, actualSize, expected));
});
}

/**
Expand All @@ -193,20 +220,22 @@ public SELF hasSize(int expected) {
*/
@Override
public SELF hasSizeBetween(int lowerBoundary, int higherBoundary) {
isNotNull();

if (!(higherBoundary >= lowerBoundary)) {
throw new IllegalArgumentException("The higher boundary <%s> must be greater than the lower boundary <%s>.".formatted(
higherBoundary,
lowerBoundary));
}

int actualSize = actual.size();
if (actualSize >= lowerBoundary && actualSize <= higherBoundary) {
return myself;
}

throw assertionError(shouldHaveSizeBetween(actual, actualSize, lowerBoundary, higherBoundary));
return executeAssertion(() -> {
isNotNull();

if (!(higherBoundary >= lowerBoundary)) {
throw new IllegalArgumentException("The higher boundary <%s> must be greater than the lower boundary <%s>.".formatted(
higherBoundary,
lowerBoundary));
}

int actualSize = actual.size();
if (actualSize >= lowerBoundary && actualSize <= higherBoundary) {
return;
}

throw assertionError(shouldHaveSizeBetween(actual, actualSize, lowerBoundary, higherBoundary));
});
}

/**
Expand All @@ -226,14 +255,16 @@ public SELF hasSizeBetween(int lowerBoundary, int higherBoundary) {
*/
@Override
public SELF hasSizeGreaterThan(int boundary) {
isNotNull();
return executeAssertion(() -> {
isNotNull();

int actualSize = actual.size();
if (actualSize > boundary) {
return myself;
}
int actualSize = actual.size();
if (actualSize > boundary) {
return;
}

throw assertionError(shouldHaveSizeGreaterThan(actual, actualSize, boundary));
throw assertionError(shouldHaveSizeGreaterThan(actual, actualSize, boundary));
});
}

/**
Expand All @@ -255,14 +286,16 @@ public SELF hasSizeGreaterThan(int boundary) {
*/
@Override
public SELF hasSizeGreaterThanOrEqualTo(int boundary) {
isNotNull();
return executeAssertion(() -> {
isNotNull();

int actualSize = actual.size();
if (actualSize >= boundary) {
return myself;
}
int actualSize = actual.size();
if (actualSize >= boundary) {
return;
}

throw assertionError(shouldHaveSizeGreaterThanOrEqualTo(actual, actualSize, boundary));
throw assertionError(shouldHaveSizeGreaterThanOrEqualTo(actual, actualSize, boundary));
});
}

/**
Expand All @@ -283,14 +316,16 @@ public SELF hasSizeGreaterThanOrEqualTo(int boundary) {
*/
@Override
public SELF hasSizeLessThan(int boundary) {
isNotNull();
return executeAssertion(() -> {
isNotNull();

int actualSize = actual.size();
if (actualSize < boundary) {
return myself;
}
int actualSize = actual.size();
if (actualSize < boundary) {
return;
}

throw assertionError(shouldHaveSizeLessThan(actual, actualSize, boundary));
throw assertionError(shouldHaveSizeLessThan(actual, actualSize, boundary));
});
}

/**
Expand All @@ -312,14 +347,16 @@ public SELF hasSizeLessThan(int boundary) {
*/
@Override
public SELF hasSizeLessThanOrEqualTo(int boundary) {
isNotNull();
return executeAssertion(() -> {
isNotNull();

int actualSize = actual.size();
if (actualSize <= boundary) {
return myself;
}
int actualSize = actual.size();
if (actualSize <= boundary) {
return;
}

throw assertionError(shouldHaveSizeLessThanOrEqualTo(actual, actualSize, boundary));
throw assertionError(shouldHaveSizeLessThanOrEqualTo(actual, actualSize, boundary));
});
}

/**
Expand All @@ -329,13 +366,15 @@ public SELF hasSizeLessThanOrEqualTo(int boundary) {
*/
@Override
public void isEmpty() {
isNotNull();
executeAssertion(() -> {
isNotNull();

if (actual.isEmpty()) {
return;
}
if (actual.isEmpty()) {
return;
}

throw assertionError(shouldBeEmpty(actual));
throw assertionError(shouldBeEmpty(actual));
});
}

/**
Expand All @@ -346,13 +385,15 @@ public void isEmpty() {
*/
@Override
public SELF isNotEmpty() {
isNotNull();
return executeAssertion(() -> {
isNotNull();

if (actual.notEmpty()) {
return myself;
}
if (actual.notEmpty()) {
return;
}

throw assertionError(shouldNotBeEmpty());
throw assertionError(shouldNotBeEmpty());
});
}

/**
Expand All @@ -362,11 +403,13 @@ public SELF isNotEmpty() {
*/
@Override
public void isNullOrEmpty() {
if (actual == null || actual.isEmpty()) {
return;
}
executeAssertion(() -> {
if (actual == null || actual.isEmpty()) {
return;
}

throw assertionError(shouldBeNullOrEmpty(actual));
throw assertionError(shouldBeNullOrEmpty(actual));
});
}

/**
Expand Down
Loading
Loading