Skip to content

Commit

Permalink
Merge pull request #193 from squins/multiple-nodes-extracting-text
Browse files Browse the repository at this point in the history
Add extractingText, which extracts text content of given nodes #192
  • Loading branch information
bodewig committed Jul 28, 2020
2 parents cf0e547 + 8f913cf commit 39534ea
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
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");
}

}

0 comments on commit 39534ea

Please sign in to comment.