Skip to content

Commit

Permalink
Merge pull request #654 from pimterry/autodatapoints-#109
Browse files Browse the repository at this point in the history
Autogenerated datapoints for boolean and enum parameters
  • Loading branch information
David Saff committed Mar 19, 2013
2 parents c7b1880 + af69286 commit e0a2efc
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.junit.experimental.theories.internal;

import static java.util.Collections.emptyList;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
Expand Down Expand Up @@ -77,14 +79,32 @@ public Object[] getActualValues(int start, int stop, boolean nullsOk)
public List<PotentialAssignment> potentialsForNextUnassigned()
throws Exception {
ParameterSignature unassigned = nextUnassigned();
return getSupplier(unassigned).getValueSources(unassigned);
List<PotentialAssignment> assignments = getSupplier(unassigned).getValueSources(unassigned);

if (assignments.size() == 0) {
assignments = generateAssignmentsFromTypeAlone(unassigned);
}

return assignments;
}

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();
}
}

private ParameterSupplier getSupplier(ParameterSignature unassigned)
throws Exception {
ParametersSuppliedBy annotation = unassigned
.findDeepAnnotation(ParametersSuppliedBy.class);

if (annotation != null) {
return buildParameterSupplierFromClass(annotation.value());
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.junit.experimental.theories.internal;

import java.util.Arrays;
import java.util.List;

import org.junit.experimental.theories.ParameterSignature;
import org.junit.experimental.theories.ParameterSupplier;
import org.junit.experimental.theories.PotentialAssignment;

public class BooleanSupplier extends ParameterSupplier {

@Override
public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
return Arrays.asList(PotentialAssignment.forValue("true", true),
PotentialAssignment.forValue("false", false));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.junit.experimental.theories.internal;

import java.util.ArrayList;
import java.util.List;

import org.junit.experimental.theories.ParameterSignature;
import org.junit.experimental.theories.ParameterSupplier;
import org.junit.experimental.theories.PotentialAssignment;

public class EnumSupplier extends ParameterSupplier {

private Class<?> enumType;

public EnumSupplier(Class<?> enumType) {
this.enumType = enumType;
}

@Override
public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
Object[] enumValues = enumType.getEnumConstants();

List<PotentialAssignment> assignments = new ArrayList<PotentialAssignment>();
for (Object value : enumValues) {
assignments.add(PotentialAssignment.forValue(value.toString(), value));
}

return assignments;
}

}
2 changes: 2 additions & 0 deletions src/test/java/org/junit/tests/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.junit.tests.experimental.theories.internal.ParameterizedAssertionErrorTest;
import org.junit.tests.experimental.theories.internal.SpecificDataPointsSupplierTest;
import org.junit.tests.experimental.theories.runner.TheoriesPerformanceTest;
import org.junit.tests.experimental.theories.runner.WithAutoGeneratedDataPoints;
import org.junit.tests.experimental.theories.runner.WithDataPointMethod;
import org.junit.tests.experimental.theories.runner.WithNamedDataPoints;
import org.junit.tests.junit3compatibility.AllTestsTest;
Expand Down Expand Up @@ -143,6 +144,7 @@
ParameterizedAssertionErrorTest.class,
WithDataPointMethod.class,
WithNamedDataPoints.class,
WithAutoGeneratedDataPoints.class,
MatcherTest.class,
ObjectContractTest.class,
TheoriesPerformanceTest.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.tests.experimental.theories.runner.SuccessfulWithDataPointFields;
import org.junit.tests.experimental.theories.runner.UnsuccessfulWithDataPointFields;
import org.junit.tests.experimental.theories.runner.WhenNoParametersMatch;
import org.junit.tests.experimental.theories.runner.WithAutoGeneratedDataPoints;
import org.junit.tests.experimental.theories.runner.WithDataPointMethod;
import org.junit.tests.experimental.theories.runner.WithExtendedParameterSources;
import org.junit.tests.experimental.theories.runner.WithNamedDataPoints;
Expand All @@ -29,7 +30,8 @@
ParameterSignatureTest.class, WhenNoParametersMatch.class,
WithExtendedParameterSources.class, StubbedTheoriesTest.class,
WithOnlyTestAnnotations.class, WithNamedDataPoints.class,
WithUnresolvedGenericTypeVariablesOnTheoryParms.class})
WithUnresolvedGenericTypeVariablesOnTheoryParms.class,
WithAutoGeneratedDataPoints.class})
public class ExperimentalTests {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.junit.tests.experimental.theories.runner;

import static org.junit.Assert.assertEquals;
import static org.junit.tests.experimental.theories.TheoryTestUtils.potentialAssignments;
import org.junit.Test;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.runner.RunWith;

public class WithAutoGeneratedDataPoints {

private enum ENUM { VALUE, OTHER_VALUE, THIRD_VALUE };

@RunWith(Theories.class)
public static class TheoryTestClassWithAutogeneratedParameterValues {

public void theory(ENUM e) {
}

public void theory(boolean b) {
}

}

@Test
public void shouldAutomaticallyGenerateEnumDataPoints() throws Exception {
assertEquals(ENUM.values().length, potentialAssignments(
TheoryTestClassWithAutogeneratedParameterValues.class.getMethod("theory", ENUM.class)).size());
}

@Test
public void shouldAutomaticallyGenerateBooleanDataPoints() throws Exception {
assertEquals(2, potentialAssignments(
TheoryTestClassWithAutogeneratedParameterValues.class.getMethod("theory", boolean.class)).size());
}

@RunWith(Theories.class)
public static class TheoryTestClassWithSpecificEnumDataPoint {

@DataPoint
public static ENUM value = ENUM.OTHER_VALUE;

public void theory(ENUM e) {
}

}

@Test
public void shouldNotAutogenerateEnumDataPointsWhenSpecificDataPointGiven() throws Exception {
assertEquals(1, potentialAssignments(
TheoryTestClassWithSpecificEnumDataPoint.class.getMethod("theory", ENUM.class)).size());
}

@RunWith(Theories.class)
public static class TheoryTestClassWithSpecificBooleanDataPoint {

@DataPoint
public static boolean value = true;

public void theory(boolean b) {
}

}

@Test
public void shouldNotAutogenerateBooleanDataPointsWhenSpecificDataPointGiven() throws Exception {
assertEquals(1, potentialAssignments(
TheoryTestClassWithSpecificBooleanDataPoint.class.getMethod("theory", boolean.class)).size());
}

}

0 comments on commit e0a2efc

Please sign in to comment.