Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expect component type for array and array2D factories #3387

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
Expand Down Expand Up @@ -442,18 +443,20 @@ static <T> InstanceOfAssertFactory<T, ObjectAssert<T>> type(Class<T> type) {
*
* @see #array(Class)
*/
InstanceOfAssertFactory<Object[], ObjectArrayAssert<Object>> ARRAY = array(Object[].class);
InstanceOfAssertFactory<Object[], ObjectArrayAssert<Object>> ARRAY = array(Object.class);

/**
* {@link InstanceOfAssertFactory} for an array of elements.
*
* @param <ELEMENT> the element type.
* @param arrayType the element type instance.
* @param <COMPONENT> the component type.
* @param componentType the component type instance.
* @return the factory instance.
*
* @see #ARRAY
*/
static <ELEMENT> InstanceOfAssertFactory<ELEMENT[], ObjectArrayAssert<ELEMENT>> array(Class<ELEMENT[]> arrayType) {
static <COMPONENT> InstanceOfAssertFactory<COMPONENT[], ObjectArrayAssert<COMPONENT>> array(Class<COMPONENT> componentType) {
@SuppressWarnings("unchecked")
Class<COMPONENT[]> arrayType = (Class<COMPONENT[]>) Array.newInstance(componentType, 0).getClass();
return new InstanceOfAssertFactory<>(arrayType, Assertions::assertThat);
}

Expand All @@ -462,18 +465,20 @@ static <ELEMENT> InstanceOfAssertFactory<ELEMENT[], ObjectArrayAssert<ELEMENT>>
*
* @see #array(Class)
*/
InstanceOfAssertFactory<Object[][], Object2DArrayAssert<Object>> ARRAY_2D = array2D(Object[][].class);
InstanceOfAssertFactory<Object[][], Object2DArrayAssert<Object>> ARRAY_2D = array2D(Object.class);

/**
* {@link InstanceOfAssertFactory} for a two-dimensional array of elements.
*
* @param <ELEMENT> the element type.
* @param arrayType the element type instance.
* @param <COMPONENT> the component type.
* @param componentType the component type instance.
* @return the factory instance.
*
* @see #ARRAY
*/
static <ELEMENT> InstanceOfAssertFactory<ELEMENT[][], Object2DArrayAssert<ELEMENT>> array2D(Class<ELEMENT[][]> arrayType) {
static <COMPONENT> InstanceOfAssertFactory<COMPONENT[][], Object2DArrayAssert<COMPONENT>> array2D(Class<COMPONENT> componentType) {
@SuppressWarnings("unchecked")
Class<COMPONENT[][]> arrayType = (Class<COMPONENT[][]>) Array.newInstance(componentType, 0, 0).getClass();
return new InstanceOfAssertFactory<>(arrayType, Assertions::assertThat);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ void array_typed_factory_should_allow_array_typed_assertions() {
// GIVEN
Object value = new Integer[] { 0, 1 };
// WHEN
ObjectArrayAssert<Integer> result = assertThat(value).asInstanceOf(array(Integer[].class));
ObjectArrayAssert<Integer> result = assertThat(value).asInstanceOf(array(Integer.class));
// THEN
result.containsExactly(0, 1);
}
Expand All @@ -682,7 +682,7 @@ void array_2d_typed_factory_should_allow_2d_array_typed_assertions() {
// GIVEN
Object value = new Integer[][] { { 0, 1 }, { 2, 3 } };
// WHEN
Object2DArrayAssert<Integer> result = assertThat(value).asInstanceOf(array2D(Integer[][].class));
Object2DArrayAssert<Integer> result = assertThat(value).asInstanceOf(array2D(Integer.class));
// THEN
result.hasDimensions(2, 2);
}
Expand Down