Skip to content
This repository was archived by the owner on Oct 13, 2020. It is now read-only.

Commit eca2b2a

Browse files
author
David Saff
committed
Merge pull request #22 from pholser/master
Bringing 4.12 changes into theories subtree.
2 parents 58b90fe + bf7fd61 commit eca2b2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1998
-893
lines changed

theories/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target
2+
.idea/
23
*.iml
34
*.ipr
45
*.iws

theories/README.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ JUnit Theories Runner
33

44
This is a port of the JUnit theories runner into junit.contrib.
55

6-
In addition to being current with the theories implementation in the latest release of JUnit and
6+
In addition to being current with the theories implementation in JUnit 4.12 and
77
depending on its core, this implementation contains a resolution for
8-
[JUnit GitHub issue 64](http://github.com/KentBeck/junit/issues/64), making it possible for
8+
[JUnit GitHub issue 64](http://github.com/junit-team/junit/issues/64), making it possible for
99
[junit-quickcheck](http://github.com/pholser/junit-quickcheck) to generate values for theory
1010
parameters involving generics in a safe manner.
1111

@@ -14,8 +14,24 @@ so that the one in the core can be removed, meaning that this runner can evolve
1414
releases of JUnit.
1515

1616
**PLEASE NOTE**: The classes that comprise this rendition of the JUnit theories runner are packaged
17-
as `org.junit.contrib.theories.*`, rather than `org.junit.experimental.theories.*`. Be careful not
18-
to intermix the two.
17+
as `org.junit.contrib.theories.*`, rather than `org.junit.experimental.theories.*`. Be mindful of
18+
which one you're using.
19+
20+
## Downloading
21+
22+
Releases are synced to the central Maven repository. Declare a dependency element in your POM like so:
23+
24+
...
25+
<dependencies>
26+
...
27+
<dependency>
28+
<groupId>org.junit.contrib</groupId>
29+
<artifactId>junit-theories</artifactId>
30+
<version>4.12</version>
31+
</dependency>
32+
...
33+
</dependencies>
34+
...
1935

2036
### What is a theory?
2137

@@ -84,9 +100,9 @@ We can eliminate duplicated test logic sometimes by using parameterized tests:
84100
Neither of these tests expresses important characteristics we want the the answers given by
85101
`PrimeFactors.of()` to exhibit: No matter what positive integer you give the method...
86102

87-
* The factors should be prime
88-
* The factors should multiply together to give the original integer
89-
* No two factorizations of two distinct integers are identical
103+
* The factors must be prime
104+
* The factors must multiply together to give the original integer
105+
* Factorizations of two distinct integers must themselves be distinct (the Fundamental Theorem of Arithmetic)
90106

91107
Whenever we want to express characteristics of a test subject that hold for entire classes of
92108
inputs, and we can express the characteristics in terms of inputs and outputs, we can codify these

theories/pom.xml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<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/xsd/maven-4.0.0.xsd">
42
<modelVersion>4.0.0</modelVersion>
53

64
<parent>
75
<groupId>org.sonatype.oss</groupId>
86
<artifactId>oss-parent</artifactId>
9-
<version>7</version>
7+
<version>9</version>
108
</parent>
119

1210
<groupId>org.junit.contrib</groupId>
1311
<artifactId>junit-theories</artifactId>
14-
<version>4.11-SNAPSHOT</version>
12+
<version>4.13-SNAPSHOT</version>
1513
<packaging>jar</packaging>
1614
<name>junit-theories</name>
1715
<description>JUnit theories runner</description>
@@ -28,8 +26,8 @@
2826
</license>
2927
</licenses>
3028
<scm>
31-
<connection>scm:git:ssh://[email protected]:pholser/junit-theories.git</connection>
32-
<developerConnection>scm:git:ssh://[email protected]:pholser/junit-theories.git</developerConnection>
29+
<connection>scm:git:[email protected]:pholser/junit-theories.git</connection>
30+
<developerConnection>scm:git:[email protected]:pholser/junit-theories.git</developerConnection>
3331
<url>http://github.com/pholser/junit-theories</url>
3432
</scm>
3533
<developers>
@@ -49,7 +47,7 @@
4947
<dependency>
5048
<groupId>junit</groupId>
5149
<artifactId>junit</artifactId>
52-
<version>4.11</version>
50+
<version>4.12</version>
5351
</dependency>
5452
<dependency>
5553
<groupId>org.javaruntype</groupId>
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
package org.junit.contrib.theories;
22

3-
import java.lang.annotation.ElementType;
43
import java.lang.annotation.Retention;
54
import java.lang.annotation.RetentionPolicy;
65
import java.lang.annotation.Target;
76

8-
@Target({ElementType.METHOD, ElementType.FIELD})
9-
@Retention(RetentionPolicy.RUNTIME)
7+
import static java.lang.annotation.ElementType.*;
8+
import static java.lang.annotation.RetentionPolicy.*;
9+
10+
/**
11+
* <p>Marking a field or method with this annotation will cause the field value or the value returned by the method
12+
* to be used as a potential value for a theory parameter in that class, when run with the {@link Theories} runner.</p>
13+
*
14+
* <p>A data point is only considered as a potential value for parameters for which its type is assignable.
15+
* When multiple data points exist with overlapping types, more control can be obtained by naming each data point
16+
* using the value of this annotation, e.g. with <code>&#064;DataPoint({"dataset1", "dataset2"})</code>, and then
17+
* specifying which named set to consider as potential values for each parameter using the
18+
* {@link FromDataPoints} annotation.</p>
19+
*
20+
* <p>Parameters with no specified source will use all data points that are assignable to the parameter type
21+
* as potential values, including named sets of data points.</p>
22+
*
23+
* <pre>
24+
* &#064;DataPoint
25+
* public static String dataPoint = "value";
26+
*
27+
* &#064;DataPoint("generated")
28+
* public static String generatedDataPoint() {
29+
* return "generated value";
30+
* }
31+
*
32+
* &#064;Theory
33+
* public void theoryMethod(String param) {
34+
* ...
35+
* }
36+
* </pre>
37+
*
38+
* @see DataPoint
39+
* @see FromDataPoints
40+
* @see Theories
41+
* @see Theory
42+
*/
43+
@Retention(RUNTIME)
44+
@Target({FIELD, METHOD})
1045
public @interface DataPoint {
46+
String[] value() default {};
47+
48+
Class<? extends Throwable>[] ignoredExceptions() default {};
1149
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
11
package org.junit.contrib.theories;
22

33
import java.lang.annotation.Retention;
4-
import java.lang.annotation.RetentionPolicy;
4+
import java.lang.annotation.Target;
55

6-
@Retention(RetentionPolicy.RUNTIME)
6+
import static java.lang.annotation.ElementType.*;
7+
import static java.lang.annotation.RetentionPolicy.*;
8+
9+
/**
10+
* <p>Marking an array or iterable-typed field or method with this annotation will cause the values in the array
11+
* or iterable given to be used as potential value for theory parameters in that class when run with the
12+
* {@link Theories} runner.</p>
13+
*
14+
* <p>Data Points will only be considered as potential values for parameters for which their types are assignable.
15+
* When multiple sets of DataPoints exist with overlapping types, more control can be obtained by naming the
16+
* data points using the value of this annotation, e.g. with <code>&#064;DataPoints({"dataset1", "dataset2"})</code>,
17+
* and then specifying which named set to consider as potential values for each parameter using the
18+
* {@link FromDataPoints} annotation.</p>
19+
*
20+
* <p>Parameters with no specified source will use all data points that are assignable to the parameter type as
21+
* potential values, including named sets of data points.</p>
22+
*
23+
* <p>Data points methods whose array types aren't assignable from the target parameter type (and so can't possibly
24+
* return relevant values) will not be called when generating values for that parameter. Iterable-typed data points
25+
* methods must always be called though, as this information is not available here after generic type erasure,
26+
* so expensive methods returning iterable data points are a bad idea.</p>
27+
*
28+
* <pre>
29+
* &#064;DataPoints
30+
* public static String[] dataPoints = new String[] { ... };
31+
*
32+
* &#064;DataPoints
33+
* public static String[] generatedDataPoints() {
34+
* return new String[] { ... };
35+
* }
36+
*
37+
* &#064;Theory
38+
* public void theoryMethod(String param) {
39+
* ...
40+
* }
41+
* </pre>
42+
*
43+
* @see DataPoint
44+
* @see FromDataPoints
45+
* @see Theories
46+
* @see Theory
47+
*/
48+
@Retention(RUNTIME)
49+
@Target({ FIELD, METHOD })
750
public @interface DataPoints {
51+
String[] value() default {};
52+
53+
Class<? extends Throwable>[] ignoredExceptions() default {};
854
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.junit.contrib.theories;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.Target;
5+
6+
import org.junit.contrib.theories.internal.SpecificDataPointsSupplier;
7+
8+
import static java.lang.annotation.ElementType.*;
9+
import static java.lang.annotation.RetentionPolicy.*;
10+
11+
/**
12+
* <p>Marking a parameter of a {@link Theory} method with this annotation will limit the data points considered
13+
* as potential values for that parameter to only the {@link DataPoints} with the given name.</p>
14+
*
15+
* <p>Data points without names will not be considered as values for any parameters annotated with
16+
* &#064FromDataPoints.</p>
17+
*
18+
* <pre>
19+
* &#064;DataPoints
20+
* public static String[] unnamed = new String[] { ... };
21+
*
22+
* &#064;DataPoints("regexes")
23+
* public static String[] regexStrings = new String[] { ... };
24+
*
25+
* &#064;DataPoints({"forMatching", "alphanumeric"})
26+
* public static String[] testStrings = new String[] { ... };
27+
*
28+
* &#064;Theory
29+
* public void stringTheory(String param) {
30+
* // This will be called with every value in 'regexStrings',
31+
* // 'testStrings' and 'unnamed'.
32+
* }
33+
*
34+
* &#064;Theory
35+
* public void regexTheory(&#064;FromDataPoints("regexes") String regex,
36+
* &#064;FromDataPoints("forMatching") String value) {
37+
* // This will be called with only the values in 'regexStrings' as
38+
* // regex, only the values in 'testStrings' as value, and none
39+
* // of the values in 'unnamed'.
40+
* }
41+
* </pre>
42+
*
43+
* @see DataPoint
44+
* @see DataPoints
45+
* @see Theories
46+
* @see Theory
47+
*/
48+
@Retention(RUNTIME)
49+
@Target(PARAMETER)
50+
@ParametersSuppliedBy(SpecificDataPointsSupplier.class)
51+
public @interface FromDataPoints {
52+
String value();
53+
}

0 commit comments

Comments
 (0)