Skip to content

Commit

Permalink
Theories: only get all data points from enum and boolean types if the…
Browse files Browse the repository at this point in the history
…re is no @FromDataPoints annotation, or the named data points represent a non-empty set of the same type.

Fixes junit-team#1648
  • Loading branch information
awturner committed Feb 11, 2020
1 parent 95af976 commit 0828f45
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.junit.experimental.theories.FromDataPoints;
import org.junit.experimental.theories.ParameterSignature;
import org.junit.experimental.theories.ParameterSupplier;
import org.junit.experimental.theories.ParametersSuppliedBy;
Expand Down Expand Up @@ -85,14 +85,16 @@ public List<PotentialAssignment> potentialsForNextUnassigned()

private List<PotentialAssignment> generateAssignmentsFromTypeAlone(ParameterSignature unassigned) {
Class<?> paramType = unassigned.getType();

if (paramType.isEnum()) {
return new EnumSupplier(paramType).getValueSources(unassigned);
} else if (paramType.equals(Boolean.class) || paramType.equals(boolean.class)) {
return new BooleanSupplier().getValueSources(unassigned);
} else {
return emptyList();

FromDataPoints fromDataPoints = unassigned.getAnnotation(FromDataPoints.class);
if (fromDataPoints == null) {
if (paramType.isEnum()) {
return new EnumSupplier(paramType).getValueSources(unassigned);
} else if (paramType.equals(Boolean.class) || paramType.equals(boolean.class)) {
return new BooleanSupplier().getValueSources(unassigned);
}
}
return emptyList();
}

private ParameterSupplier getSupplier(ParameterSignature unassigned)
Expand Down Expand Up @@ -150,4 +152,4 @@ public Object[] getArgumentStrings(boolean nullsOk)
}
return values;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
TypeMatchingBetweenMultiDataPointsMethod.class,
UnsuccessfulWithDataPointFields.class,
WhenNoParametersMatch.class,
WhenNoParametersMatchEnumeratedTypes.class,
WithAutoGeneratedDataPoints.class,
WithDataPointMethod.class,
WithExtendedParameterSources.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.junit.tests.experimental.theories.runner;

import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;

import org.junit.Test;
import org.junit.experimental.results.PrintableResult;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.FromDataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class WhenNoParametersMatchEnumeratedTypes {
public enum SomeEnum {
FIRST, SECOND
}

@RunWith(Theories.class)
public static class AssumptionsFailBoolean {
@Theory
public void shouldFailBecauseExplicitFromDataPointsNotKnown(
@FromDataPoints("unknown") boolean b) {}

@Theory
public void shouldSucceedBecauseNoExplicitFromDataPoints(boolean b) {}
}

@Test
public void showFailedAssumptionsWhenNoParametersFoundBoolean() {
assertThat(
testResult(AssumptionsFailBoolean.class),
hasSingleFailureContaining(
"Never found parameters that satisfied method assumptions"));
}

@RunWith(Theories.class)
public static class AssumptionsFailEnum {
@Theory
public void shouldFailBecauseExplicitFromDataPointsNotKnown(
@FromDataPoints("unknown") SomeEnum e) {}

@Theory
public void shouldSucceedBecauseNoExplicitFromDataPoints(SomeEnum e) {}
}

@Test
public void showFailedAssumptionsWhenNoParametersFoundEnum() {
assertThat(
testResult(AssumptionsFailEnum.class),
hasSingleFailureContaining(
"Never found parameters that satisfied method assumptions"));
}

@RunWith(Theories.class)
public static class AssumptionsFailWrongType {
@DataPoints("known") public static final String[] known = {"known"};

@Theory
public void shouldSucceedBecauseRightType(@FromDataPoints("known") String s) {}

@Theory
public void shouldFailBecauseWrongTypeBoolean(@FromDataPoints("known") boolean b) {}

@Theory
public void shouldFailBecauseWrongTypeEnum(@FromDataPoints("known") SomeEnum e) {}
}

@Test
public void showFailedAssumptionsWhenWrongType() {
PrintableResult result = testResult(AssumptionsFailWrongType.class);
assertThat(result, failureCountIs(2));
assertThat(
result,
hasFailureContaining(
"Never found parameters that satisfied method assumptions"));
}
}

0 comments on commit 0828f45

Please sign in to comment.