Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extractingText, which extracts text content of given nodes #192 #193

Merged
merged 1 commit into from Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -241,6 +241,33 @@ public AbstractListAssert<?, List<? extends String>, String, ObjectAssert<String
return newListAssertInstance(values).as(description);
}

/**
* Extracting text content of given nodes.
* If a node doesn't have the text then an empty string is returned.
*
* @throws AssertionError if the actual nodes iterable is {@code null}.
* @since XMLUnit 2.8.0
*/
public AbstractListAssert<?, List<? extends String>, String, ObjectAssert<String>> extractingText() {
isNotNull();

List<String> values = new ArrayList<>();

for (Node node : actual) {
String textContent = node.getTextContent();
if (textContent != null) {
textContent = textContent.trim();
}
values.add(textContent);
}

String extractedDescription = "Extracted text content";
String description = Description.mostRelevantDescription(this.info.description(), extractedDescription);

return newListAssertInstance(values).as(description);
}


private void allSatisfy(SingleNodeAssertConsumer consumer) {
int index = 0;
for (Node node : actual) {
Expand Down
Expand Up @@ -115,4 +115,90 @@ public void testExtractingAttribute_shouldPass() {
.extractingAttribute("attr1")
.containsExactly("value1", "value2", null, "value4");
}

@Test
public void testExtractingTextSingleNode_shouldPass() {

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<feed>" +
" <title>My Simple Feed</title>" +
" <entry attr1=\"value1\" />" +
" <entry attr1=\"value2\"/>" +
" <entry />" +
" <entry attr1=\"value4\" />" +
"</feed>";

assertThat(xml)
.nodesByXPath("/feed/title")
.extractingText()
.containsExactly("My Simple Feed");
}

@Test
public void testExtractingTextSingleNodeWithWhiteSpace_shouldPass() {

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<feed>" +
" <title>" +
" My Simple Feed" +
" </title>" +
" <entry attr1=\"value1\" />" +
" <entry attr1=\"value2\"/>" +
" <entry />" +
" <entry attr1=\"value4\" />" +
"</feed>";

assertThat(xml)
.nodesByXPath("/feed/title")
.extractingText()
.containsExactly("My Simple Feed");
}

@Test
public void testExtractingTextMultipleNodes_shouldPass() {

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<document>" +
" <h2> A header</h2>" +
" <h2>Just another header</h2>" +
"</document>";

assertThat(xml)
.nodesByXPath("/document/h2")
.extractingText()
.containsExactly("A header", "Just another header");
}

@Test
public void testExtractingTextMultipleNodesOneEmpty_shouldPass() {

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<document>" +
" <h2> A header</h2>" +
" <h2/>" +
"</document>";

assertThat(xml)
.nodesByXPath("/document/h2")
.extractingText()
.containsExactly("A header", "");
}

@Test
public void testExtractingTextMultipleNodes_shouldFailed() {

thrown.expectAssertionError("[XPath \"/document/h2\" evaluated to node set] ");

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<document>" +
" <h2> Matching header</h2>" +
" <h2>Not matching</h2>" +
"</document>";

assertThat(xml)
.nodesByXPath("/document/h2")
.extractingText()
.containsExactly("Matching header", "Header not matching");
}

}