com.connect_group.thymeleaf.testing.hamcrest.HasTextBeforeElement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thymeleaf-tdd Show documentation
Show all versions of thymeleaf-tdd Show documentation
Test-Driven Development framework for Thymeleaf and Thymesheet
The newest version!
package com.connect_group.thymeleaf.testing.hamcrest;
import com.connect_group.thymesheet.query.HtmlElement;
import java.util.List;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.thymeleaf.dom.Comment;
import org.thymeleaf.dom.Element;
import org.thymeleaf.dom.Node;
import org.thymeleaf.dom.Text;
/**
* Originally created by Ruth Mills
*/
public class HasTextBeforeElement extends TypeSafeMatcher {
private final String value;
public HasTextBeforeElement(String value) {
this.value = value;
}
@Override
public void describeTo(Description description) {
description.appendText("has text ").appendValue(value).appendValue(" before tag");
}
@Override
public boolean matchesSafely(HtmlElement item) {
StringBuilder text = new StringBuilder();
boolean fail = true;
boolean elementFound = false;
boolean textFound = false;
Element element = item.getElement();
if (element != null && element.hasChildren()) {
List children = element.getChildren();
for (Node child : children) {
if (child instanceof Text) {
text.append(((Text) child).getContent());
if (elementFound || textFound) {
fail = true;
break;
}
textFound = true;
} else if (!(child instanceof Comment)) {
elementFound = true;
if (textFound) {
fail = false;
}
}
}
}
return !fail && text.toString().equals(value);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy