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