Skip to content

Commit 07614ad

Browse files
committed
Ported latest annotations from internal repository
1 parent 55bcb4c commit 07614ad

12 files changed

+678
-0
lines changed

README.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,156 @@ public class ClassUnderTest {
105105
}
106106
```
107107

108+
### Using `@InTestsUseClasses`
109+
110+
The `@InTestsUseClasses` annotation allows the user to recommend specific `Class` literal values to use in tests.
111+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
112+
For example the following method is annotated with an example class literal to achieve a positive test:
113+
114+
```java
115+
public static boolean isAnnotation(@InTestsUseClasses(Nullable.class) Class<?> theClass) {
116+
return theClass.isAnnotation();
117+
}
118+
```
119+
120+
### Using `@InTestsUseStrings`
121+
122+
The `@InTestsUseStrings` annotation allows the user to recommend specific `String` values to use in tests.
123+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
124+
For example the following method is annotated with some genuine examples of song titles that can be used to achieve coverage:
125+
126+
```java
127+
public static boolean isDayRelatedSongTitle(@InTestsUseStrings({"I Don't Like Mondays", "Here Comes The Weekend"}) String title) {
128+
return Stream.of(DayOfWeek.values())
129+
.map(DayOfWeek::name)
130+
.map(String::toLowerCase)
131+
.anyMatch(title.toLowerCase()::contains);
132+
}
133+
```
134+
135+
### Using `@InTestsUseCharacters`
136+
137+
The `@InTestsUseCharacters` annotation allows the user to recommend specific `char` values to use in tests.
138+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
139+
For example the following method is annotated with a genuine examples characters that make up a Unicode surrogate pair that can be used to achieve a positive test:
140+
141+
```java
142+
@Nullable
143+
public static Integer toNullableCodePoint(
144+
@InTestsUseCharacters('\uD801') char high,
145+
@InTestsUseCharacters('\uDC37') char low) {
146+
if (Character.isSurrogatePair(high, low)) {
147+
return Character.toCodePoint(high, low);
148+
}
149+
return null;
150+
}
151+
```
152+
153+
### Using `@InTestsUseBytes`
154+
155+
The `@InTestsUseBytes` annotation allows the user to recommend specific `byte` values to use in tests.
156+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
157+
For example the following method is annotated to use a specific preferred value:
158+
159+
```java
160+
public static String toUpperHexString(@InTestsUseBytes((byte)0xD1) byte input) {
161+
return Long.toHexString(input).toUpperCase();
162+
}
163+
```
164+
165+
### Using `@InTestsUseShorts`
166+
167+
The `@InTestsUseShorts` annotation allows the user to recommend specific `short` values to use in tests.
168+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
169+
For example the following method is annotated to use a specific preferred value:
170+
171+
```java
172+
public static String toUpperHexString(@InTestsUseShorts((short)0xD1FF) short input) {
173+
return Long.toHexString(input).toUpperCase();
174+
}
175+
```
176+
177+
### Using `@InTestsUseIntegers`
178+
179+
The `@InTestsUseIntegers` annotation allows the user to recommend specific `int` values to use in tests.
180+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
181+
For example the following method is annotated to use a specific preferred value:
182+
183+
```java
184+
public static String toUpperHexString(@InTestsUseIntegers(0xD1FFB) int input) {
185+
return Long.toHexString(input).toUpperCase();
186+
}
187+
```
188+
189+
### Using `@InTestsUseLongs`
190+
191+
The `@InTestsUseLongs` annotation allows the user to recommend specific `long` values to use in tests.
192+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
193+
For example the following method is annotated to use a specific preferred value:
194+
195+
```java
196+
public static String toUpperHexString(@InTestsUseLongs(0xD1FFBL) long input) {
197+
return Long.toHexString(input).toUpperCase();
198+
}
199+
```
200+
201+
### Using `@InTestsUseFloats`
202+
203+
The `@InTestsUseFloats` annotation allows the user to recommend specific `float` values to use in tests.
204+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
205+
For example the following method is annotated to use a specific preferred value:
206+
207+
```java
208+
public static boolean isNearPi(@InTestsUseFloats(3.14159f) float input) {
209+
return Float.toString(input).startsWith("3.14");
210+
}
211+
```
212+
213+
### Using `@InTestsUseDoubles`
214+
215+
The `@InTestsUseDoubles` annotation allows the user to recommend specific `double` values to use in tests.
216+
Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases.
217+
For example the following method is annotated to use a specific preferred value:
218+
219+
```java
220+
public static boolean isNearPi(@InTestsUseDoubles(Math.PI) float input) {
221+
return Double.toString(input).startsWith("3.14");
222+
}
223+
```
224+
225+
### Using `@InterestingTestFactory`
226+
227+
Indicates the annotated method as a useful factory method for use in tests.
228+
Cover will automatically recognise factory methods that simply return a newly created instance, but may not identify more complicated factories.
229+
This annotation allows such factory methods to be manually annotated so that Cover considers them for producing inputs.
230+
For example the following method under test takes a `User` as input, but the `User` constructor is private and Cover doesn't naturally consider `ofStaff(String)` to be a safe factory method to call.
231+
By annotating the `ofStaff(String)` with `@InterstingTestFactory` we can tell Cover that this should be considered a good factory method to use in tests.
232+
233+
```java
234+
public String getUserDisplayString(User user) {
235+
if (user.manager) {
236+
return user.username + " (manager)";
237+
}
238+
else {
239+
return user.username;
240+
}
241+
}
242+
243+
class User {
244+
private static Map<String, User> staff = new HashMap<String, User>();
245+
246+
@InterestingTestFactory
247+
public static User ofStaff(String name) {
248+
return staff.computeIfAbsent(name, ignored -> new User(name, false));
249+
}
250+
251+
public final String username;
252+
public final boolean manager;
253+
254+
private User(String username, boolean manager) {
255+
this.username = username;
256+
this.manager = manager;
257+
}
258+
}
259+
```
260+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Diffblue Limited.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.diffblue.cover.annotations;
16+
17+
import static java.lang.annotation.ElementType.METHOD;
18+
import static java.lang.annotation.ElementType.PACKAGE;
19+
import static java.lang.annotation.ElementType.PARAMETER;
20+
import static java.lang.annotation.ElementType.TYPE;
21+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
23+
import java.lang.annotation.Repeatable;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Identifies values that should be considered when writing tests that require inputs of type {@code
29+
* byte} or {@link Byte}.
30+
*/
31+
@Retention(RUNTIME)
32+
@Repeatable(InTestsUseBytes.Repeatable.class)
33+
public @interface InTestsUseBytes {
34+
35+
/** Collects multiple {@link InTestsUseBytes} annotations. */
36+
@Retention(RUNTIME)
37+
@Target({PACKAGE, TYPE, METHOD, PARAMETER})
38+
@interface Repeatable {
39+
40+
/**
41+
* @return the repeated {@link InTestsUseBytes} annotations.
42+
*/
43+
InTestsUseBytes[] value();
44+
}
45+
46+
/**
47+
* @return {@code byte} values that should be tried.
48+
*/
49+
byte[] value() default {};
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Diffblue Limited.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.diffblue.cover.annotations;
16+
17+
import static java.lang.annotation.ElementType.METHOD;
18+
import static java.lang.annotation.ElementType.PACKAGE;
19+
import static java.lang.annotation.ElementType.PARAMETER;
20+
import static java.lang.annotation.ElementType.TYPE;
21+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
23+
import java.lang.annotation.Repeatable;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Identifies values that should be considered when writing tests that require inputs of type {@code
29+
* char} or {@link Character}.
30+
*/
31+
@Retention(RUNTIME)
32+
@Repeatable(InTestsUseCharacters.Repeatable.class)
33+
public @interface InTestsUseCharacters {
34+
35+
/** Collects multiple {@link InTestsUseCharacters} annotations. */
36+
@Retention(RUNTIME)
37+
@Target({PACKAGE, TYPE, METHOD, PARAMETER})
38+
@interface Repeatable {
39+
40+
/**
41+
* @return the repeated {@link InTestsUseCharacters} annotations.
42+
*/
43+
InTestsUseCharacters[] value();
44+
}
45+
46+
/**
47+
* @return {@link Character} values that should be tried.
48+
*/
49+
char[] value() default {};
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Diffblue Limited.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.diffblue.cover.annotations;
16+
17+
import static java.lang.annotation.ElementType.METHOD;
18+
import static java.lang.annotation.ElementType.PACKAGE;
19+
import static java.lang.annotation.ElementType.PARAMETER;
20+
import static java.lang.annotation.ElementType.TYPE;
21+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
23+
import java.lang.annotation.Repeatable;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Identifies values that should be considered when writing tests that require inputs of type {@link
29+
* Class}.
30+
*/
31+
@Retention(RUNTIME)
32+
@Repeatable(InTestsUseClasses.Repeatable.class)
33+
public @interface InTestsUseClasses {
34+
35+
/** Collects multiple {@link InTestsUseClasses} annotations. */
36+
@Retention(RUNTIME)
37+
@Target({PACKAGE, TYPE, METHOD, PARAMETER})
38+
@interface Repeatable {
39+
40+
/**
41+
* @return the repeated {@link InTestsUseClasses} annotations.
42+
*/
43+
InTestsUseClasses[] value();
44+
}
45+
46+
/**
47+
* @return {@link Class} values that should be tried.
48+
*/
49+
Class<?>[] value() default {};
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Diffblue Limited.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.diffblue.cover.annotations;
16+
17+
import static java.lang.annotation.ElementType.METHOD;
18+
import static java.lang.annotation.ElementType.PACKAGE;
19+
import static java.lang.annotation.ElementType.PARAMETER;
20+
import static java.lang.annotation.ElementType.TYPE;
21+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
23+
import java.lang.annotation.Repeatable;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Identifies values that should be considered when writing tests that require inputs of type {@code
29+
* double} or {@link Double}.
30+
*/
31+
@Retention(RUNTIME)
32+
@Repeatable(InTestsUseDoubles.Repeatable.class)
33+
public @interface InTestsUseDoubles {
34+
35+
/** Collects multiple {@link InTestsUseDoubles} annotations. */
36+
@Retention(RUNTIME)
37+
@Target({PACKAGE, TYPE, METHOD, PARAMETER})
38+
@interface Repeatable {
39+
40+
/**
41+
* @return the repeated {@link InTestsUseDoubles} annotations.
42+
*/
43+
InTestsUseDoubles[] value();
44+
}
45+
46+
/**
47+
* @return {@code double} values that should be tried.
48+
*/
49+
double[] value() default {};
50+
}

0 commit comments

Comments
 (0)