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

Theories: only get all data points from enum and boolean types if the… #1651

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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,11 +85,15 @@ 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);

FromDataPoints fromDataPoints = unassigned.getAnnotation(FromDataPoints.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Shouldn't this check for ParametersSuppliedBy or, alternatively, shouldn't SpecificDataPointsSupplier throw an exception if it couldn't find a matching field?

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);
}
}
awturner marked this conversation as resolved.
Show resolved Hide resolved
} else {
return emptyList();
}
Expand Down Expand Up @@ -150,4 +154,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"));
}
}