-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from connect-group/has-attribute-enhancement
HasAttribute enhancement
- Loading branch information
Showing
3 changed files
with
38 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 30 additions & 13 deletions
43
src/main/java/com/connect_group/thymeleaf/testing/hamcrest/HasAttribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,67 @@ | ||
package com.connect_group.thymeleaf.testing.hamcrest; | ||
|
||
import com.connect_group.thymesheet.query.HtmlElement; | ||
import java.util.Map; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.TypeSafeMatcher; | ||
import org.thymeleaf.dom.Attribute; | ||
import org.thymeleaf.dom.Element; | ||
|
||
public class HasAttribute extends TypeSafeMatcher<HtmlElement> { | ||
|
||
private final String expectedAttributeName; | ||
private final String expectedAttributeValue; | ||
private final boolean testAttributeValue; | ||
private final boolean testAnyNonNullValue; | ||
|
||
public HasAttribute(String attributeName) { | ||
this.expectedAttributeName = attributeName; | ||
this.expectedAttributeValue = null; | ||
this.testAttributeValue = false; | ||
this.testAnyNonNullValue = false; | ||
} | ||
|
||
public HasAttribute(String attributeName, String attributeValue) { | ||
this.expectedAttributeName = attributeName; | ||
this.expectedAttributeValue = attributeValue; | ||
this.testAttributeValue = true; | ||
this.testAnyNonNullValue = false; | ||
} | ||
|
||
public HasAttribute(String attributeName, boolean testAnyNonNullValue) { | ||
this.expectedAttributeName = attributeName; | ||
this.expectedAttributeValue = null; | ||
this.testAttributeValue = true; | ||
this.testAnyNonNullValue = testAnyNonNullValue; | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
if (!testAttributeValue) { | ||
description.appendText("has attribute ").appendValue(expectedAttributeName); | ||
} else { | ||
description.appendText("has attribute ").appendValue(expectedAttributeName) | ||
.appendText(" with value ").appendValue(expectedAttributeValue); | ||
description.appendText("has attribute ").appendValue(expectedAttributeName); | ||
if (testAttributeValue) { | ||
if (testAnyNonNullValue) { | ||
description.appendText(" with any value "); | ||
} else { | ||
description.appendText(" with value ").appendValue(expectedAttributeValue); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean matchesSafely(HtmlElement item) { | ||
boolean hasNamedAttribute = item.getElement().getAttributeMap() | ||
.containsKey(expectedAttributeName); | ||
Element element = item.getElement(); | ||
Map<String, Attribute> attributeMap = element.getAttributeMap(); | ||
boolean hasNamedAttribute = attributeMap.containsKey(expectedAttributeName); | ||
|
||
if (hasNamedAttribute && testAttributeValue) { | ||
String actualAttributeValue = item.getElement().getAttributeMap().get(expectedAttributeName) | ||
.getValue(); | ||
return ((expectedAttributeValue == null && actualAttributeValue == null) || | ||
expectedAttributeValue.equals(actualAttributeValue)); | ||
String actualAttributeValue = attributeMap.get(expectedAttributeName).getValue(); | ||
if (testAnyNonNullValue) { | ||
return actualAttributeValue != null; | ||
} | ||
return expectedAttributeValue == null | ||
? actualAttributeValue == null : expectedAttributeValue.equals(actualAttributeValue); | ||
} else { | ||
return hasNamedAttribute; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters