| 1 | package fr.sii.ogham.testing.assertion.util; | |
| 2 | ||
| 3 | import org.hamcrest.Description; | |
| 4 | import org.hamcrest.Matcher; | |
| 5 | import org.hamcrest.MatcherAssert; | |
| 6 | import org.hamcrest.StringDescription; | |
| 7 | import org.junit.ComparisonFailure; | |
| 8 | ||
| 9 | import fr.sii.ogham.testing.assertion.context.Context; | |
| 10 | import fr.sii.ogham.testing.assertion.hamcrest.ComparisonAwareMatcher; | |
| 11 | import fr.sii.ogham.testing.assertion.hamcrest.CustomDescriptionProvider; | |
| 12 | import fr.sii.ogham.testing.assertion.hamcrest.CustomReason; | |
| 13 | import fr.sii.ogham.testing.assertion.hamcrest.DecoratorMatcher; | |
| 14 | import fr.sii.ogham.testing.assertion.hamcrest.ExpectedValueProvider; | |
| 15 | import fr.sii.ogham.testing.assertion.hamcrest.OverrideDescription; | |
| 16 | ||
| 17 | /** | |
| 18 | * Utility class for Ogham assertions. | |
| 19 | * | |
| 20 | * @author Aurélien Baudet | |
| 21 | * | |
| 22 | */ | |
| 23 | public final class AssertionHelper { | |
| 24 | ||
| 25 | /** | |
| 26 | * Copy of {@link MatcherAssert#assertThat(Object, Matcher)} with the | |
| 27 | * following additions: | |
| 28 | * <ul> | |
| 29 | * <li>If the matcher can provide expected value, a | |
| 30 | * {@link ComparisonFailure} exception is thrown instead of | |
| 31 | * {@link AssertionError} in order to display differences between expected | |
| 32 | * string and actual string in the IDE.</li> | |
| 33 | * <li>If the matcher is a {@link CustomReason} matcher and no reason is | |
| 34 | * provided, the reason of the matcher is used to provide more information | |
| 35 | * about the context (which message has failed for example)</li> | |
| 36 | * </ul> | |
| 37 | * | |
| 38 | * @param actual | |
| 39 | * the actual value | |
| 40 | * @param matcher | |
| 41 | * the matcher to apply | |
| 42 | * @param <T> | |
| 43 | * the type used for the matcher | |
| 44 | */ | |
| 45 | public static <T> void assertThat(T actual, Matcher<? super T> matcher) { | |
| 46 |
4
1. assertThat : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → NO_COVERAGE 2. assertThat : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → SURVIVED 3. assertThat : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → TIMED_OUT 4. assertThat : removed call to fr/sii/ogham/testing/assertion/util/AssertionHelper::assertThat → KILLED |
assertThat("", actual, matcher); |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Copy of {@link MatcherAssert#assertThat(String, Object, Matcher)} with | |
| 51 | * the following additions: | |
| 52 | * <ul> | |
| 53 | * <li>If the matcher can provide expected value, a | |
| 54 | * {@link ComparisonFailure} exception is thrown instead of | |
| 55 | * {@link AssertionError} in order to display differences between expected | |
| 56 | * string and actual string in the IDE.</li> | |
| 57 | * <li>If the matcher is a {@link CustomReason} matcher and no reason is | |
| 58 | * provided, the reason of the matcher is used to provide more information | |
| 59 | * about the context (which message has failed for example)</li> | |
| 60 | * </ul> | |
| 61 | * | |
| 62 | * @param reason | |
| 63 | * the reason | |
| 64 | * @param actual | |
| 65 | * the actual value | |
| 66 | * @param matcher | |
| 67 | * the matcher to apply | |
| 68 | * @param <T> | |
| 69 | * the type used for the matcher | |
| 70 | */ | |
| 71 | public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) { | |
| 72 |
7
1. assertThat : negated conditional → NO_COVERAGE 2. assertThat : negated conditional → TIMED_OUT 3. assertThat : negated conditional → KILLED 4. assertThat : negated conditional → KILLED 5. assertThat : negated conditional → KILLED 6. assertThat : negated conditional → KILLED 7. assertThat : negated conditional → KILLED |
if (!matcher.matches(actual)) { |
| 73 | Description description = getDescription(reason, actual, matcher); | |
| 74 | ||
| 75 |
2
1. assertThat : negated conditional → NO_COVERAGE 2. assertThat : negated conditional → KILLED |
if (hasExpectedValue(matcher)) { |
| 76 | ExpectedValueProvider<T> comparable = getComparable(matcher); | |
| 77 |
2
1. assertThat : negated conditional → NO_COVERAGE 2. assertThat : negated conditional → KILLED |
throw new ComparisonFailure(description.toString(), String.valueOf(comparable == null ? null : comparable.getExpectedValue()), String.valueOf(actual)); |
| 78 | } else { | |
| 79 | throw new AssertionError(description.toString()); | |
| 80 | } | |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Ogham helper for keeping context information when using fluent | |
| 86 | * assertions. | |
| 87 | * | |
| 88 | * @param reasonTemplate | |
| 89 | * the template for the reason | |
| 90 | * @param context | |
| 91 | * the evaluation context | |
| 92 | * @param delegate | |
| 93 | * the matcher to decorate | |
| 94 | * @param <T> | |
| 95 | * the type used for the matcher | |
| 96 | * @return the matcher | |
| 97 | */ | |
| 98 | public static <T> Matcher<T> usingContext(String reasonTemplate, Context context, Matcher<T> delegate) { | |
| 99 |
6
1. usingContext : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::usingContext → NO_COVERAGE 2. usingContext : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::usingContext → TIMED_OUT 3. usingContext : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::usingContext → KILLED 4. usingContext : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::usingContext → KILLED 5. usingContext : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::usingContext → KILLED 6. usingContext : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::usingContext → KILLED |
return new CustomReason<>(context.evaluate(reasonTemplate), delegate); |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Ogham helper for overriding default description. | |
| 104 | * | |
| 105 | * @param description | |
| 106 | * the description to display | |
| 107 | * @param delegate | |
| 108 | * the matcher to decorate | |
| 109 | * @param <T> | |
| 110 | * the type used for the matcher | |
| 111 | * @return the matcher | |
| 112 | */ | |
| 113 | public static <T> Matcher<T> overrideDescription(String description, Matcher<T> delegate) { | |
| 114 |
6
1. overrideDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::overrideDescription → NO_COVERAGE 2. overrideDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::overrideDescription → TIMED_OUT 3. overrideDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::overrideDescription → KILLED 4. overrideDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::overrideDescription → KILLED 5. overrideDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::overrideDescription → KILLED 6. overrideDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::overrideDescription → KILLED |
return new OverrideDescription<>(description, delegate); |
| 115 | } | |
| 116 | | |
| 117 | @SuppressWarnings("unchecked") | |
| 118 | private static <T> boolean hasExpectedValue(Matcher<? super T> matcher) { | |
| 119 |
2
1. hasExpectedValue : negated conditional → NO_COVERAGE 2. hasExpectedValue : negated conditional → KILLED |
if (matcher instanceof ExpectedValueProvider) { |
| 120 |
2
1. hasExpectedValue : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/AssertionHelper::hasExpectedValue → NO_COVERAGE 2. hasExpectedValue : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/AssertionHelper::hasExpectedValue → KILLED |
return true; |
| 121 | } | |
| 122 |
2
1. hasExpectedValue : negated conditional → NO_COVERAGE 2. hasExpectedValue : negated conditional → KILLED |
if (matcher instanceof DecoratorMatcher) { |
| 123 |
4
1. hasExpectedValue : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/AssertionHelper::hasExpectedValue → NO_COVERAGE 2. hasExpectedValue : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/AssertionHelper::hasExpectedValue → NO_COVERAGE 3. hasExpectedValue : replaced boolean return with false for fr/sii/ogham/testing/assertion/util/AssertionHelper::hasExpectedValue → KILLED 4. hasExpectedValue : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/AssertionHelper::hasExpectedValue → KILLED |
return hasExpectedValue(((DecoratorMatcher<T>) matcher).getDecoree()); |
| 124 | } | |
| 125 |
2
1. hasExpectedValue : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/AssertionHelper::hasExpectedValue → NO_COVERAGE 2. hasExpectedValue : replaced boolean return with true for fr/sii/ogham/testing/assertion/util/AssertionHelper::hasExpectedValue → KILLED |
return false; |
| 126 | } | |
| 127 | ||
| 128 | @SuppressWarnings("unchecked") | |
| 129 | private static <T> ExpectedValueProvider<T> getComparable(Matcher<? super T> matcher) { | |
| 130 |
2
1. getComparable : negated conditional → NO_COVERAGE 2. getComparable : negated conditional → KILLED |
if (matcher instanceof ExpectedValueProvider) { |
| 131 |
2
1. getComparable : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getComparable → NO_COVERAGE 2. getComparable : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getComparable → KILLED |
return (ExpectedValueProvider<T>) matcher; |
| 132 | } | |
| 133 |
2
1. getComparable : negated conditional → NO_COVERAGE 2. getComparable : negated conditional → KILLED |
if (matcher instanceof DecoratorMatcher) { |
| 134 |
2
1. getComparable : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getComparable → NO_COVERAGE 2. getComparable : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getComparable → KILLED |
return getComparable(((DecoratorMatcher<T>) matcher).getDecoree()); |
| 135 | } | |
| 136 | return null; | |
| 137 | } | |
| 138 | ||
| 139 | private static <T> Description getDescription(String reason, T actual, Matcher<? super T> matcher) { | |
| 140 | String additionalText = null; | |
| 141 | ComparisonAwareMatcher cam = getComparisonAwareMatcher(matcher); | |
| 142 |
2
1. getDescription : negated conditional → NO_COVERAGE 2. getDescription : negated conditional → KILLED |
if (cam != null) { |
| 143 | additionalText = cam.comparisonMessage(); | |
| 144 | } | |
| 145 |
2
1. getDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getDescription → NO_COVERAGE 2. getDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getDescription → KILLED |
return getDescription(reason, actual, matcher, additionalText); |
| 146 | } | |
| 147 | ||
| 148 | @SuppressWarnings("unchecked") | |
| 149 | private static <T> ComparisonAwareMatcher getComparisonAwareMatcher(Matcher<? super T> matcher) { | |
| 150 |
2
1. getComparisonAwareMatcher : negated conditional → NO_COVERAGE 2. getComparisonAwareMatcher : negated conditional → KILLED |
if (matcher instanceof ComparisonAwareMatcher) { |
| 151 |
2
1. getComparisonAwareMatcher : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getComparisonAwareMatcher → NO_COVERAGE 2. getComparisonAwareMatcher : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getComparisonAwareMatcher → KILLED |
return (ComparisonAwareMatcher) matcher; |
| 152 | } | |
| 153 |
2
1. getComparisonAwareMatcher : negated conditional → NO_COVERAGE 2. getComparisonAwareMatcher : negated conditional → KILLED |
if (matcher instanceof DecoratorMatcher) { |
| 154 |
2
1. getComparisonAwareMatcher : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getComparisonAwareMatcher → NO_COVERAGE 2. getComparisonAwareMatcher : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getComparisonAwareMatcher → KILLED |
return getComparisonAwareMatcher(((DecoratorMatcher<T>) matcher).getDecoree()); |
| 155 | } | |
| 156 | return null; | |
| 157 | } | |
| 158 | ||
| 159 | @SuppressWarnings("unchecked") | |
| 160 | private static <T> Description getDescription(String reason, T actual, Matcher<? super T> matcher, String additionalText) { | |
| 161 |
2
1. getDescription : negated conditional → NO_COVERAGE 2. getDescription : negated conditional → KILLED |
if (matcher instanceof CustomDescriptionProvider) { |
| 162 |
2
1. getDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getDescription → NO_COVERAGE 2. getDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getDescription → KILLED |
return ((CustomDescriptionProvider<T>) matcher).describe(reason, actual, additionalText); |
| 163 | } | |
| 164 | // @formatter:off | |
| 165 | Description description = new StringDescription(); | |
| 166 |
2
1. getDescription : negated conditional → NO_COVERAGE 2. getDescription : negated conditional → KILLED |
description.appendText(getReason(reason, matcher)) |
| 167 | .appendText(additionalText==null ? "" : ("\n"+additionalText)) | |
| 168 | .appendText("\nExpected: ") | |
| 169 | .appendDescriptionOf(matcher) | |
| 170 | .appendText("\n but: "); | |
| 171 |
2
1. getDescription : removed call to org/hamcrest/Matcher::describeMismatch → NO_COVERAGE 2. getDescription : removed call to org/hamcrest/Matcher::describeMismatch → KILLED |
matcher.describeMismatch(actual, description); |
| 172 | // @formatter:on | |
| 173 |
2
1. getDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getDescription → NO_COVERAGE 2. getDescription : replaced return value with null for fr/sii/ogham/testing/assertion/util/AssertionHelper::getDescription → KILLED |
return description; |
| 174 | } | |
| 175 | ||
| 176 | private static <T> String getReason(String reason, Matcher<? super T> matcher) { | |
| 177 |
4
1. getReason : negated conditional → NO_COVERAGE 2. getReason : negated conditional → NO_COVERAGE 3. getReason : negated conditional → KILLED 4. getReason : negated conditional → KILLED |
if (reason != null && !reason.isEmpty()) { |
| 178 |
2
1. getReason : replaced return value with "" for fr/sii/ogham/testing/assertion/util/AssertionHelper::getReason → NO_COVERAGE 2. getReason : replaced return value with "" for fr/sii/ogham/testing/assertion/util/AssertionHelper::getReason → KILLED |
return reason; |
| 179 | } | |
| 180 |
2
1. getReason : negated conditional → NO_COVERAGE 2. getReason : negated conditional → KILLED |
if (matcher instanceof CustomReason) { |
| 181 |
2
1. getReason : replaced return value with "" for fr/sii/ogham/testing/assertion/util/AssertionHelper::getReason → NO_COVERAGE 2. getReason : replaced return value with "" for fr/sii/ogham/testing/assertion/util/AssertionHelper::getReason → KILLED |
return ((CustomReason<?>) matcher).getReason(); |
| 182 | } | |
| 183 | return ""; | |
| 184 | } | |
| 185 | ||
| 186 | private AssertionHelper() { | |
| 187 | super(); | |
| 188 | } | |
| 189 | ||
| 190 | } | |
Mutations | ||
| 46 |
1.1 2.2 3.3 4.4 |
|
| 72 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
| 75 |
1.1 2.2 |
|
| 77 |
1.1 2.2 |
|
| 99 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
| 114 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
| 119 |
1.1 2.2 |
|
| 120 |
1.1 2.2 |
|
| 122 |
1.1 2.2 |
|
| 123 |
1.1 2.2 3.3 4.4 |
|
| 125 |
1.1 2.2 |
|
| 130 |
1.1 2.2 |
|
| 131 |
1.1 2.2 |
|
| 133 |
1.1 2.2 |
|
| 134 |
1.1 2.2 |
|
| 142 |
1.1 2.2 |
|
| 145 |
1.1 2.2 |
|
| 150 |
1.1 2.2 |
|
| 151 |
1.1 2.2 |
|
| 153 |
1.1 2.2 |
|
| 154 |
1.1 2.2 |
|
| 161 |
1.1 2.2 |
|
| 162 |
1.1 2.2 |
|
| 166 |
1.1 2.2 |
|
| 171 |
1.1 2.2 |
|
| 173 |
1.1 2.2 |
|
| 177 |
1.1 2.2 3.3 4.4 |
|
| 178 |
1.1 2.2 |
|
| 180 |
1.1 2.2 |
|
| 181 |
1.1 2.2 |