-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
The former @CartesianProductTest extension closely mirrored Jupiter's @ParameterizedTest annotation. Since then users came up with the idea that instead of providing arguments in annotations on the method, they could be provided on the parameters themselves. // BEFORE @CartesianProductTest @CartesianValueSource(ints = { 1, 2 }) @CartesianValueSource(ints = { 3, 4 }) void myCartesianTestMethod(int x, int y) { // passing test code } // NOW @CartesianTest void myCartesianTestMethod( @values(ints = { 1, 2 }) int x, @values(ints = { 3, 4 }) int y) { // passing test code } The old variant is still around, but deprecated and will be removed in the next major release. The new variant is different in more ways than just where annotations are placed: * new package `org.junitpioneer.jupiter.cartesian` * shorter name `@CartesianTest` * to keep parameter annotations short while avoiding collisions, they are now inner types of `@CartesianTest` (so in the example above, the full name is `@CartesianTest.Values` Closes: #415 PR: #487
- Loading branch information
1 parent
5ccaed3
commit 44f5ebe
Showing
33 changed files
with
2,608 additions
and
388 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/org/junitpioneer/jupiter/cartesian/CartesianArgumentsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2016-2021 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.cartesian; | ||
|
||
import java.lang.reflect.Parameter; | ||
|
||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
|
||
/** | ||
* If you are implementing an {@link org.junit.jupiter.params.provider.ArgumentsProvider ArgumentsProvider} | ||
* for {@link CartesianTest}, it has to implement this interface <b>instead</b> to know which parameter it provides | ||
* arguments to. For more information, see | ||
* <a href="https://junit-pioneer.org/docs/cartesian-product/" target="_top">the Cartesian product documentation</a>. | ||
* | ||
* @see org.junit.jupiter.params.provider.ArgumentsProvider | ||
* @see CartesianTestExtension | ||
*/ | ||
public interface CartesianArgumentsProvider extends ArgumentsProvider { | ||
|
||
void accept(Parameter parameter); | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/org/junitpioneer/jupiter/cartesian/CartesianEnumArgumentsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2016-2021 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.cartesian; | ||
|
||
import static java.util.Arrays.stream; | ||
import static java.util.stream.Collectors.toSet; | ||
|
||
import java.lang.reflect.Parameter; | ||
import java.util.EnumSet; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.platform.commons.PreconditionViolationException; | ||
import org.junit.platform.commons.support.AnnotationSupport; | ||
|
||
/** | ||
* This is basically an enhanced copy of Jupiter's {@code EnumArgumentsProvider}, | ||
* except it does NOT support {@code @ParameterizedTest} and consumes a {@code Parameter} | ||
* instead of an annotation. | ||
*/ | ||
class CartesianEnumArgumentsProvider implements CartesianArgumentsProvider { | ||
|
||
private CartesianTest.Enum enumSource; | ||
private Class<?> parameterType; | ||
|
||
@Override | ||
public void accept(Parameter parameter) { | ||
this.parameterType = parameter.getType(); | ||
if (!Enum.class.isAssignableFrom(this.parameterType)) | ||
throw new PreconditionViolationException(String | ||
.format( | ||
"Parameter of type %s must reference an Enum type (alternatively, use the annotation's 'value' attribute to specify the type explicitly)", | ||
this.parameterType)); | ||
this.enumSource = AnnotationSupport | ||
.findAnnotation(parameter, CartesianTest.Enum.class) | ||
.orElseThrow(() -> new PreconditionViolationException( | ||
"Parameter has to be annotated with " + CartesianTest.Enum.class.getName())); | ||
} | ||
|
||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | ||
Set<? extends Enum<?>> constants = getEnumConstants(); | ||
CartesianTest.Enum.Mode mode = enumSource.mode(); | ||
String[] declaredConstantNames = enumSource.names(); | ||
if (declaredConstantNames.length > 0) { | ||
Set<String> uniqueNames = stream(declaredConstantNames).collect(toSet()); | ||
if (uniqueNames.size() != declaredConstantNames.length) | ||
throw new PreconditionViolationException("Duplicate enum constant name(s) found in " + enumSource); | ||
|
||
mode.validate(enumSource, constants, uniqueNames); | ||
constants.removeIf(constant -> !mode.select(constant, uniqueNames)); | ||
} | ||
return constants.stream().map(Arguments::of); | ||
} | ||
|
||
private <E extends Enum<E>> Set<? extends E> getEnumConstants() { | ||
Class<E> enumClass = determineEnumClass(); | ||
return EnumSet.allOf(enumClass); | ||
} | ||
|
||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
private <E extends Enum<E>> Class<E> determineEnumClass() { | ||
Class enumClass = enumSource.value(); | ||
if (enumClass.equals(NullEnum.class)) { | ||
enumClass = this.parameterType; | ||
} | ||
return enumClass; | ||
} | ||
|
||
enum NullEnum { | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.