Skip to content

Commit

Permalink
Test status quo for null/"null" conversion support in parameterized t…
Browse files Browse the repository at this point in the history
…ests

See #3472
  • Loading branch information
sbrannen committed Sep 22, 2023
1 parent 2ab6828 commit 738b13c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,40 @@
*/
class ParameterizedTestIntegrationTests {

@ParameterizedTest
@CsvSource(textBlock = """
apple, True
banana, true
lemon, false
kumquat, null
""")
void sweetFruit(String fruit, Boolean sweet) {
switch (fruit) {
case "apple" -> assertThat(sweet).isTrue();
case "banana" -> assertThat(sweet).isTrue();
case "lemon" -> assertThat(sweet).isFalse();
case "kumquat" -> assertThat(sweet).isFalse(); // "null" --> false
default -> fail("Unexpected fruit : " + fruit);
}
}

@ParameterizedTest
@CsvSource(nullValues = "null", textBlock = """
apple, True
banana, true
lemon, false
kumquat, null
""")
void sweetFruitWithNullableBoolean(String fruit, Boolean sweet) {
switch (fruit) {
case "apple" -> assertThat(sweet).isTrue();
case "banana" -> assertThat(sweet).isTrue();
case "lemon" -> assertThat(sweet).isFalse();
case "kumquat" -> assertThat(sweet).isNull(); // null --> null
default -> fail("Unexpected fruit : " + fruit);
}
}

@ParameterizedTest
@CsvSource(quoteCharacter = '"', textBlock = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Unit tests for {@link DefaultArgumentConverter}.
Expand All @@ -55,11 +57,13 @@ class DefaultArgumentConverterTests {
void isAwareOfNull() {
assertConverts(null, Object.class, null);
assertConverts(null, String.class, null);
assertConverts(null, Boolean.class, null);
}

@Test
void isAwareOfWrapperTypesForPrimitiveTypes() {
assertConverts(true, boolean.class, true);
assertConverts(false, boolean.class, false);
assertConverts((byte) 1, byte.class, (byte) 1);
assertConverts('o', char.class, 'o');
assertConverts((short) 1, short.class, (short) 1);
Expand All @@ -85,6 +89,7 @@ void isAwareOfWideningConversions() {
@Test
void convertsStringsToPrimitiveTypes() {
assertConverts("true", boolean.class, true);
assertConverts("false", boolean.class, false);
assertConverts("o", char.class, 'o');
assertConverts("1", byte.class, (byte) 1);
assertConverts("1_0", byte.class, (byte) 10);
Expand All @@ -100,6 +105,54 @@ void convertsStringsToPrimitiveTypes() {
assertConverts("42.2_3", double.class, 42.23);
}

@Test
void convertsStringsToPrimitiveWrapperTypes() {
assertConverts("true", Boolean.class, true);
assertConverts("false", Boolean.class, false);
assertConverts("o", Character.class, 'o');
assertConverts("1", Byte.class, (byte) 1);
assertConverts("1_0", Byte.class, (byte) 10);
assertConverts("1", Short.class, (short) 1);
assertConverts("1_2", Short.class, (short) 12);
assertConverts("42", Integer.class, 42);
assertConverts("700_050_000", Integer.class, 700_050_000);
assertConverts("42", Long.class, 42L);
assertConverts("4_2", Long.class, 42L);
assertConverts("42.23", Float.class, 42.23f);
assertConverts("42.2_3", Float.class, 42.23f);
assertConverts("42.23", Double.class, 42.23);
assertConverts("42.2_3", Double.class, 42.23);
}

@Test
void convertsTheWordNullToBooleanFalse() {
assertConverts("null", boolean.class, false);
assertConverts("NULL", boolean.class, false);
assertConverts("null", Boolean.class, false);
assertConverts("NULL", Boolean.class, false);
}

@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(classes = { char.class, boolean.class, short.class, byte.class, int.class, long.class, float.class,
double.class })
void throwsExceptionForNullToPrimitiveTypeConversion(Class<?> type) {
assertThatExceptionOfType(ArgumentConversionException.class) //
.isThrownBy(() -> convert(null, type)) //
.withMessage("Cannot convert null to primitive value of type " + type.getCanonicalName());
}

@ParameterizedTest(name = "[{index}] {0}")
// NOTE: everything except Boolean.class and Character.class.
@ValueSource(classes = { Short.class, Byte.class, Integer.class, Long.class, Float.class, Double.class })
void throwsExceptionWhenConvertingTheWordNullToPrimitiveWrapperType(Class<?> type) {
assertThatExceptionOfType(ArgumentConversionException.class) //
.isThrownBy(() -> convert("null", type)) //
.withMessage("Failed to convert String \"null\" to type " + type.getCanonicalName());
assertThatExceptionOfType(ArgumentConversionException.class) //
.isThrownBy(() -> convert("NULL", type)) //
.withMessage("Failed to convert String \"NULL\" to type " + type.getCanonicalName());
}

@Test
void throwsExceptionOnInvalidStringForPrimitiveTypes() {
assertThatExceptionOfType(ArgumentConversionException.class) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,12 @@ void customEmptyValueAndDefaultNullValue() {

@Test
void customNullValues() {
var annotation = csvSource().nullValues("N/A", "NIL").lines("apple, , NIL, '', N/A, banana").build();
var annotation = csvSource().nullValues("N/A", "NIL", "null")//
.lines("apple, , NIL, '', N/A, banana, null").build();

var arguments = provideArguments(annotation);

assertThat(arguments).containsExactly(array("apple", null, null, "", null, "banana"));
assertThat(arguments).containsExactly(array("apple", null, null, "", null, "banana", null));
}

@Test
Expand Down

0 comments on commit 738b13c

Please sign in to comment.