Skip to content

Commit 1877aaf

Browse files
committed
Ported @InTestsUseEnums from internal repository
1 parent fe4974d commit 1877aaf

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

README.md

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

108+
### Using `@InTestsUseEnum`
109+
110+
The `@InTestsUseEnum` annotation allows the user to recommend specific `enum` 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+
113+
```java
114+
public static boolean isDateOrTimeBased(@InTestsUseEnums({"SECONDS", "YEARS", "FOREVER"}) ChronoUnit chronoUnit) {
115+
return chronoUnit.isDateBased() || chronoUnit.isTimeBased();
116+
}
117+
```
118+
108119
### Using `@InTestsUseClasses`
109120

110121
The `@InTestsUseClasses` annotation allows the user to recommend specific `Class` literal values to use in tests.
@@ -126,9 +137,9 @@ For example the following method is annotated with some genuine examples of song
126137
```java
127138
public static boolean isDayRelatedSongTitle(@InTestsUseStrings({"I Don't Like Mondays", "Here Comes The Weekend"}) String title) {
128139
return Stream.of(DayOfWeek.values())
129-
.map(DayOfWeek::name)
130-
.map(String::toLowerCase)
131-
.anyMatch(title.toLowerCase()::contains);
140+
.map(DayOfWeek::name)
141+
.map(String::toLowerCase)
142+
.anyMatch(title.toLowerCase()::contains);
132143
}
133144
```
134145

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+
* Enum}.
30+
*/
31+
@Retention(RUNTIME)
32+
@Repeatable(InTestsUseEnums.Repeatable.class)
33+
public @interface InTestsUseEnums {
34+
35+
/** Collects multiple {@link InTestsUseEnums} annotations. */
36+
@Retention(RUNTIME)
37+
@Target({PACKAGE, TYPE, METHOD, PARAMETER})
38+
@interface Repeatable {
39+
40+
/**
41+
* @return the repeated {@link InTestsUseEnums} annotations.
42+
*/
43+
InTestsUseEnums[] value();
44+
}
45+
46+
/**
47+
* @return {@link String} values that should be tried as names of the annotated enum.
48+
*/
49+
String[] value() default {};
50+
}

0 commit comments

Comments
 (0)