AbstractHtmlDiffMatcher.java

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

Mutations

51

1.1
Location : matches
Killed by :
negated conditional → NO_COVERAGE

52

1.1
Location : matches
Killed by :
removed call to java/util/function/Consumer::accept → NO_COVERAGE

54

1.1
Location : matches
Killed by :
replaced boolean return with true for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → NO_COVERAGE

2.2
Location : matches
Killed by :
replaced boolean return with false for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::matches → NO_COVERAGE

66

1.1
Location : getExpectedValue
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::getExpectedValue → NO_COVERAGE

78

1.1
Location : comparisonMessage
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/hamcrest/AbstractHtmlDiffMatcher::comparisonMessage → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1