1 package com.lexicalscope.jewel.cli;
2
3 import static ch.lambdaj.Lambda.selectFirst;
4
5 import org.hamcrest.Description;
6 import org.hamcrest.Matcher;
7 import org.hamcrest.TypeSafeMatcher;
8
9 import com.lexicalscope.jewel.UtilitiesForTesting;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public class ArgumentValidationExceptionMatcher {
28 public static Matcher<ArgumentValidationException> hasValidationException(final ValidationFailureType expectedErrorType) {
29 return new TypeSafeMatcher<ArgumentValidationException>() {
30 @Override public void describeTo(final Description description) {
31 description
32 .appendText(ArgumentValidationException.class.getSimpleName())
33 .appendText(" with error type ")
34 .appendValue(expectedErrorType);
35 }
36
37 @Override protected boolean matchesSafely(final ArgumentValidationException item) {
38 return selectFirst(item.getValidationFailures(), new TypeSafeMatcher<ValidationFailureImpl>() {
39 @Override public void describeTo(final Description description) {
40 description
41 .appendText(ValidationFailureImpl.class.getSimpleName())
42 .appendText(" with error type ")
43 .appendValue(expectedErrorType);
44 }
45
46 @Override protected boolean matchesSafely(final ValidationFailureImpl actualError) {
47 return actualError.getFailureType().equals(expectedErrorType);
48 }
49 }) != null;
50 }
51 };
52 }
53
54 public static Matcher<ArgumentValidationException> validationExceptionWithMessage(final String message) {
55 return new TypeSafeMatcher<ArgumentValidationException>() {
56 @Override public void describeTo(final Description description) {
57 description.appendText(ArgumentValidationException.class.getSimpleName() + " with message ").appendValue(
58 message);
59 }
60
61 @Override protected boolean matchesSafely(final ArgumentValidationException item) {
62 return item.getMessage().equals(message);
63 }
64 };
65 }
66
67 public static Matcher<ArgumentValidationException> validationExceptionWithMessageLines(final String... message) {
68 return validationExceptionWithMessage(UtilitiesForTesting.joinLines(message));
69 }
70 }