1 | package fr.sii.ogham.testing.assertion.hamcrest; | |
2 | ||
3 | import java.util.function.Consumer; | |
4 | ||
5 | import org.custommonkey.xmlunit.DetailedDiff; | |
6 | import org.custommonkey.xmlunit.Difference; | |
7 | import org.hamcrest.BaseMatcher; | |
8 | import org.hamcrest.Description; | |
9 | import org.junit.ComparisonFailure; | |
10 | import org.w3c.dom.Document; | |
11 | ||
12 | import fr.sii.ogham.testing.assertion.OghamAssertions; | |
13 | import fr.sii.ogham.testing.assertion.util.HtmlUtils; | |
14 | ||
15 | /** | |
16 | * Check if the HTML is identical or similar to the expected. The HTML strings are parsed | |
17 | * into {@link Document}s. Two documents are considered to be "identical" if | |
18 | * they contain the same elements and attributes in the same order.Two documents are considered to be "similar" if they | |
19 | * contain the same elements and attributes regardless of order. | |
20 | * | |
21 | * <p> | |
22 | * This matcher is a {@link ExpectedValueProvider} for knowing the original expected | |
23 | * value. Thanks to this information, {@link OghamAssertions} will generate a | |
24 | * {@link ComparisonFailure} with the expected string and actual string in order | |
25 | * to be able to visualize the differences on sources directly in the IDE. | |
26 | * | |
27 | * <p> | |
28 | * See {@link HtmlUtils} for more information about "identical"/"similar" HTML. | |
29 | * | |
30 | * @author Aurélien Baudet | |
31 | * | |
32 | */ | |
33 | public abstract class AbstractHtmlDiffMatcher extends BaseMatcher<String> implements ExpectedValueProvider<String>, ComparisonAwareMatcher { | |
34 | protected final String expected; | |
35 | protected final Consumer<String> printer; | |
36 | protected final String name; | |
37 | protected DetailedDiff diff; | |
38 | ||
39 | public AbstractHtmlDiffMatcher(String expected, Consumer<String> printer, String name) { | |
40 | super(); | |
41 | this.expected = expected; | |
42 | this.printer = printer; | |
43 | this.name = name; | |
44 | } | |
45 | ||
46 | @Override | |
47 | public boolean matches(Object item) { | |
48 | diff = HtmlUtils.compare(expected, (String) item); | |
49 | boolean matches = matches(diff); | |
50 |
4
1. matches : negated conditional → SURVIVED 2. matches : negated conditional → NO_COVERAGE 3. matches : negated conditional → TIMED_OUT 4. matches : negated conditional → KILLED |
if(!matches) { |
51 |
2
1. matches : removed call to java/util/function/Consumer::accept → NO_COVERAGE 2. matches : removed call to java/util/function/Consumer::accept → KILLED |
printer.accept(comparisonMessage()); |
52 | } | |
53 |
8
1. matches : replaced boolean return with false for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → NO_COVERAGE 2. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → NO_COVERAGE 3. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → SURVIVED 4. matches : replaced boolean return with false for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → TIMED_OUT 5. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → TIMED_OUT 6. matches : replaced boolean return with false for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → KILLED 7. matches : replaced boolean return with false for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → KILLED 8. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → KILLED |
return matches; |
54 | } | |
55 | ||
56 | protected abstract boolean matches(DetailedDiff diff); | |
57 | ||
58 | @Override | |
59 | public void describeTo(Description description) { | |
60 | description.appendValue(expected); | |
61 | } | |
62 | ||
63 | @Override | |
64 | public String getExpectedValue() { | |
65 |
2
1. getExpectedValue : replaced return value with "" for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::getExpectedValue → NO_COVERAGE 2. getExpectedValue : replaced return value with "" for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::getExpectedValue → KILLED |
return expected; |
66 | } | |
67 | ||
68 | @Override | |
69 | public String comparisonMessage() { | |
70 | StringBuilder sb = new StringBuilder(); | |
71 | sb.append("The two HTML documents are not ").append(name).append(".\n"); | |
72 | sb.append("Here are the differences found:\n"); | |
73 | for(Difference d : diff.getAllDifferences()) { | |
74 | sb.append(" - ").append(d.toString()).append("\n"); | |
75 | } | |
76 | sb.append("\n"); | |
77 |
2
1. comparisonMessage : replaced return value with "" for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::comparisonMessage → NO_COVERAGE 2. comparisonMessage : replaced return value with "" for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::comparisonMessage → KILLED |
return sb.toString(); |
78 | } | |
79 | } | |
Mutations | ||
50 |
1.1 2.2 3.3 4.4 |
|
51 |
1.1 2.2 |
|
53 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
65 |
1.1 2.2 |
|
77 |
1.1 2.2 |