Skip to content
Open
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
@@ -0,0 +1,213 @@
/*
* Copyright 2025-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.assertj.eclipse.collections.api;

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.ShouldHaveSameSizeAs.shouldHaveSameSizeAs;
import static org.assertj.core.error.ShouldHaveSize.shouldHaveSize;
import static org.assertj.core.error.ShouldHaveSizeBetween.shouldHaveSizeBetween;
import static org.assertj.core.error.ShouldHaveSizeGreaterThan.shouldHaveSizeGreaterThan;
import static org.assertj.core.error.ShouldHaveSizeGreaterThanOrEqualTo.shouldHaveSizeGreaterThanOrEqualTo;
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.eclipse.collections.util.RichIterableUtil.sizeOf;

import java.lang.reflect.Array;

import org.assertj.core.api.AbstractAssert;
import org.eclipse.collections.api.PrimitiveIterable;

/**
* Base class for all assertions for the {@link PrimitiveIterable} interface.
*
* @param <SELF> the "self" type of this assertion class. Please read &quot;<a href="https://bit.ly/1IZIRcY"
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
* for more details.
* @param <ACTUAL> the type of the "actual" value.
*/
public abstract class AbstractPrimitiveIterableAssert<SELF extends AbstractPrimitiveIterableAssert<SELF, ACTUAL>, ACTUAL extends PrimitiveIterable> extends AbstractAssert<SELF, ACTUAL> {

protected AbstractPrimitiveIterableAssert(ACTUAL actual, Class<?> selfType) {
super(actual, selfType);
}

public SELF hasSameSizeAs(PrimitiveIterable other) {
return executeAssertion(() -> {
isNotNull();

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

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

public SELF hasSameSizeAs(Iterable<?> other) {
return executeAssertion(() -> {
isNotNull();

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

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

public SELF hasSameSizeAs(Object other) {
return executeAssertion(() -> {
isNotNull();

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

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

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

public SELF hasSize(int expectedSize) {
return executeAssertion(() -> {
isNotNull();

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

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

public SELF hasSizeBetween(int lowerBoundary, int 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));
});
}

public SELF hasSizeGreaterThan(int boundary) {
return executeAssertion(() -> {
isNotNull();

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

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

public SELF hasSizeGreaterThanOrEqualTo(int boundary) {
return executeAssertion(() -> {
isNotNull();

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

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

public SELF hasSizeLessThan(int boundary) {
return executeAssertion(() -> {
isNotNull();

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

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

public SELF hasSizeLessThanOrEqualTo(int boundary) {
return executeAssertion(() -> {
isNotNull();

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

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

public void isEmpty() {
executeAssertion(() -> {
isNotNull();

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

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

public SELF isNotEmpty() {
return executeAssertion(() -> {
isNotNull();

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

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

public void isNullOrEmpty() {
executeAssertion(() -> {
if (actual == null || actual.isEmpty()) {
return;
}

throw assertionError(shouldBeNullOrEmpty(actual));
});
}
}
102 changes: 101 additions & 1 deletion src/main/java/org/assertj/eclipse/collections/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
package org.assertj.eclipse.collections.api;

import org.assertj.core.annotation.CheckReturnValue;
import org.eclipse.collections.api.BooleanIterable;
import org.eclipse.collections.api.ByteIterable;
import org.eclipse.collections.api.CharIterable;
import org.eclipse.collections.api.DoubleIterable;
import org.eclipse.collections.api.FloatIterable;
import org.eclipse.collections.api.IntIterable;
import org.eclipse.collections.api.LongIterable;
import org.eclipse.collections.api.ShortIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.multimap.Multimap;
Expand All @@ -26,7 +34,6 @@
* Entry point for assertion methods for the Eclipse Collections library. Each method in this class is a static factory
* for a type-specific assertion object.
*/
@CheckReturnValue
public class Assertions {
/**
* Creates a new {@link Assertions}.
Expand All @@ -42,21 +49,100 @@ protected Assertions() {
* @return the created assertion object.
* @param <T> The type of the elements in the bag
*/
@CheckReturnValue
public static <T> BagAssert<T> assertThat(Bag<T> actual) {
return new BagAssert<>(actual);
}

/**
* Creates a new instance of {@link BooleanIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static BooleanIterableAssert assertThat(BooleanIterable actual) {
return new BooleanIterableAssert(actual);
}

/**
* Creates a new instance of {@link ByteIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static ByteIterableAssert assertThat(ByteIterable actual) {
return new ByteIterableAssert(actual);
}

/**
* Creates a new instance of {@link CharIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static CharIterableAssert assertThat(CharIterable actual) {
return new CharIterableAssert(actual);
}

/**
* Creates a new instance of {@link DoubleIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static DoubleIterableAssert assertThat(DoubleIterable actual) {
return new DoubleIterableAssert(actual);
}

/**
* Creates a new instance of {@link FloatIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static FloatIterableAssert assertThat(FloatIterable actual) {
return new FloatIterableAssert(actual);
}

/**
* Creates a new instance of {@link IntIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static IntIterableAssert assertThat(IntIterable actual) {
return new IntIterableAssert(actual);
}

/**
* Creates a new instance of {@link ListIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
* @param <T> The type of the elements in the list
*/
@CheckReturnValue
public static <T> ListIterableAssert<T> assertThat(ListIterable<T> actual) {
return new ListIterableAssert<>(actual);
}

/**
* Creates a new instance of {@link LongIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static LongIterableAssert assertThat(LongIterable actual) {
return new LongIterableAssert(actual);
}

/**
* Creates a new instance of {@link MultimapAssert}.
*
Expand All @@ -65,6 +151,7 @@ public static <T> ListIterableAssert<T> assertThat(ListIterable<T> actual) {
* @param <KEY> The type of keys in the BagMultimap
* @param <VALUE> The type of values in the BagMultimap
*/
@CheckReturnValue
public static <KEY, VALUE> MultimapAssert<KEY, VALUE> assertThat(Multimap<KEY, VALUE> actual) {
return new MultimapAssert<>(actual);
}
Expand All @@ -76,17 +163,30 @@ public static <KEY, VALUE> MultimapAssert<KEY, VALUE> assertThat(Multimap<KEY, V
* @return the created assertion object.
* @param <T> The type of the elements in the set
*/
@CheckReturnValue
public static <T> SetIterableAssert<T> assertThat(SetIterable<T> actual) {
return new SetIterableAssert<>(actual);
}

/**
* Creates a new instance of {@link ShortIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
*/
@CheckReturnValue
public static ShortIterableAssert assertThat(ShortIterable actual) {
return new ShortIterableAssert(actual);
}

/**
* Creates a new instance of {@link StackIterableAssert}.
*
* @param actual the actual value.
* @return the created assertion object.
* @param <T> The type of the elements in the stack
*/
@CheckReturnValue
public static <T> StackIterableAssert<T> assertThat(StackIterable<T> actual) {
return new StackIterableAssert<>(actual);
}
Expand Down
Loading
Loading