|
4 | 4 |
|
5 | 5 | import org.hamcrest.Description; |
6 | 6 | import org.hamcrest.Matcher; |
| 7 | +import org.hamcrest.Factory; |
7 | 8 |
|
8 | 9 | /** |
9 | | - * Always evaluate to true or false (depending on what's passed into the constructor). |
| 10 | + * Always passes or failes, ignoring what value is passed to it. |
10 | 11 | */ |
11 | | -public class Always implements Matcher { |
| 12 | +public class Always<T> implements Matcher<T> { |
12 | 13 |
|
13 | | - private final boolean matches; |
| 14 | + private final boolean pass; |
14 | 15 | private final String description; |
15 | 16 |
|
16 | | - public Always(boolean matches) { |
17 | | - this(matches, "always evaluates to " + matches); |
| 17 | + public Always(boolean pass) { |
| 18 | + this(pass, "always " + (pass ? "passes" : "fails")); |
18 | 19 | } |
19 | 20 |
|
20 | 21 | public Always(boolean matches, String description) { |
21 | | - this.matches = matches; |
| 22 | + this.pass = matches; |
22 | 23 | this.description = description; |
23 | 24 | } |
24 | 25 |
|
25 | | - public boolean match(Object o) { |
26 | | - return matches; |
| 26 | + public boolean match(T o) { |
| 27 | + return pass; |
27 | 28 | } |
28 | 29 |
|
29 | 30 | public void describeTo(Description description) { |
30 | 31 | description.appendText(this.description); |
31 | 32 | } |
32 | 33 |
|
| 34 | + @Factory |
| 35 | + public static <T> Matcher<T> alwaysPasses() { |
| 36 | + return new Always<T>(true); |
| 37 | + } |
| 38 | + |
| 39 | + @Factory |
| 40 | + public static <T> Matcher<T> alwaysPasses(String description) { |
| 41 | + return new Always<T>(true, description); |
| 42 | + } |
| 43 | + |
| 44 | + @Factory |
| 45 | + public static <T> Matcher<T> alwaysFails() { |
| 46 | + return new Always<T>(false); |
| 47 | + } |
| 48 | + |
| 49 | + @Factory |
| 50 | + public static <T> Matcher<T> alwaysFails(String description) { |
| 51 | + return new Always<T>(false, description); |
| 52 | + } |
33 | 53 | } |
0 commit comments