-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #654 from pimterry/autodatapoints-#109
Autogenerated datapoints for boolean and enum parameters
- Loading branch information
Showing
6 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
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
18 changes: 18 additions & 0 deletions
18
src/main/java/org/junit/experimental/theories/internal/BooleanSupplier.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,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)); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/junit/experimental/theories/internal/EnumSupplier.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 @@ | ||
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; | ||
} | ||
|
||
} |
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
71 changes: 71 additions & 0 deletions
71
src/test/java/org/junit/tests/experimental/theories/runner/WithAutoGeneratedDataPoints.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,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()); | ||
} | ||
|
||
} |