Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

extra tests for coverage's sake

  • Loading branch information...
commit a6e0bcd661ef8aa18c7bed7d74059e25654779dd 1 parent 03b912c
@pholser authored
View
10 .gitignore
@@ -0,0 +1,10 @@
+.classpath
+.project
+.settings
+*.iml
+*.ipr
+*.iws
+bin
+target
+*~
+.*~
View
51 pom.xml
@@ -0,0 +1,51 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.pholser</groupId>
+ <artifactId>junit-quickcheck</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ <packaging>jar</packaging>
+ <name>junit-quickcheck</name>
+ <description>QuickCheck-style parameter suppliers for JUnit theories</description>
+ <url>http://github.com/pholser/junit-quickcheck</url>
+ <licenses>
+ <license>
+ <name>The MIT License</name>
+ <url>http://www.opensource.org/licenses/mit-license.php</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+ <scm>
+ <connection>scm:git:git://github.com/pholser/junit-quickcheck.git</connection>
+ <developerConnection>scm:git:ssh://git@github.com/pholser/junit-quickcheck.git</developerConnection>
+ <url>http://github.com/pholser/junit-quickcheck</url>
+ </scm>
+ <developers>
+ <developer>
+ <id>pholser</id>
+ <name>Paul Holser</name>
+ <email>pholser@alumni.rice.edu</email>
+ <url>http://www.pholser.com</url>
+ </developer>
+ </developers>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.2</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
View
27 src/main/java/com/pholser/junit/parameters/GeneratingParameterSupplier.java
@@ -29,6 +29,20 @@ public Object randomValue(SourceOfRandomness random) {
}
});
extractors.put(Double.class, extractors.get(double.class));
+ extractors.put(float.class, new RandomValueExtractor() {
+ @Override
+ public Object randomValue(SourceOfRandomness random) {
+ return random.nextFloat();
+ }
+ });
+ extractors.put(Float.class, extractors.get(float.class));
+ extractors.put(boolean.class, new RandomValueExtractor() {
+ @Override
+ public Object randomValue(SourceOfRandomness random) {
+ return random.nextBoolean();
+ }
+ });
+ extractors.put(Boolean.class, extractors.get(boolean.class));
extractors.put(String.class, new RandomValueExtractor() {
@Override
public Object randomValue(SourceOfRandomness random) {
@@ -53,17 +67,24 @@ public GeneratingParameterSupplier(SourceOfRandomness random) {
@Override
public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
- List<PotentialAssignment> potentials = new ArrayList<PotentialAssignment>();
- ForAll generationParms = sig.getAnnotation(ForAll.class);
RandomValueExtractor extractor = extractor(sig);
+ int sampleSize = sampleSizeFor(sig);
- for (int i = 0; i < generationParms.sampleSize(); ++i) {
+ List<PotentialAssignment> potentials = new ArrayList<PotentialAssignment>();
+ for (int i = 0; i < sampleSize; ++i) {
Object generated = extractor.randomValue(random);
potentials.add(PotentialAssignment.forValue(String.valueOf(generated), generated));
}
return potentials;
}
+ private static int sampleSizeFor(ParameterSignature sig) {
+ Class<?> type = sig.getType();
+ if (boolean.class.equals(type) || Boolean.class.equals(type))
+ return 2;
+ return sig.getAnnotation(ForAll.class).sampleSize();
+ }
+
private RandomValueExtractor extractor(ParameterSignature sig) {
ExtractedBy extractedBy = sig.getAnnotation(ExtractedBy.class);
View
59 ...rameters/WhenMarkingTheoryParametersAsForAll.java → ...rameters/MarkingTheoryParametersAsForAllTest.java
@@ -10,12 +10,13 @@
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
-public class WhenMarkingTheoryParametersAsForAll {
+public class MarkingTheoryParametersAsForAllTest {
@Test
public void shouldFeedDefaultNumberOfRandomIntsToAMarkedIntParameter() throws Exception {
JUnitCore.runClasses(ForDefaultNumberOfInts.class);
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(), ForDefaultNumberOfInts.iterations);
+ assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(),
+ ForDefaultNumberOfInts.iterations);
}
@RunWith(Theories.class)
@@ -32,7 +33,8 @@ public void shouldHold(@ForAll int i) {
public void shouldFeedDefaultNumberOfRandomDoublesToAMarkedDoubleParameter() throws Exception {
JUnitCore.runClasses(ForDefaultNumberOfDoubles.class);
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(), ForDefaultNumberOfDoubles.iterations);
+ assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(),
+ ForDefaultNumberOfDoubles.iterations);
}
@RunWith(Theories.class)
@@ -46,6 +48,57 @@ public void shouldHold(@ForAll double d) {
}
@Test
+ public void shouldFeedDefaultNumberOfRandomFloatsToAMarkedStringParameter() throws Exception {
+ JUnitCore.runClasses(ForDefaultNumberOfFloats.class);
+
+ assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(), ForDefaultNumberOfFloats.iterations);
+ }
+
+ @RunWith(Theories.class)
+ public static class ForDefaultNumberOfFloats {
+ static int iterations;
+
+ @Theory
+ public void shouldHold(@ForAll float f) {
+ ++iterations;
+ }
+ }
+
+ @Test
+ public void shouldFeedExactlyTwoBooleansToAMarkedBooleanParameter() throws Exception {
+ JUnitCore.runClasses(ForDefaultNumberOfBooleans.class);
+
+ assertEquals(2, ForDefaultNumberOfBooleans.iterations);
+ }
+
+ @RunWith(Theories.class)
+ public static class ForDefaultNumberOfBooleans {
+ static int iterations;
+
+ @Theory
+ public void shouldHold(@ForAll boolean b) {
+ ++iterations;
+ }
+ }
+
+ @Test
+ public void shouldFeedExactlyTwoBooleansWrappersToAMarkedBooleanWrapperParameter() throws Exception {
+ JUnitCore.runClasses(ForDefaultNumberOfBooleanWrappers.class);
+
+ assertEquals(2, ForDefaultNumberOfBooleans.iterations);
+ }
+
+ @RunWith(Theories.class)
+ public static class ForDefaultNumberOfBooleanWrappers {
+ static int iterations;
+
+ @Theory
+ public void shouldHold(@ForAll Boolean b) {
+ ++iterations;
+ }
+ }
+
+ @Test
public void shouldFeedDefaultNumberOfRandomStringsToAMarkedStringParameter() throws Exception {
JUnitCore.runClasses(ForDefaultNumberOfStrings.class);
Please sign in to comment.
Something went wrong with that request. Please try again.