Skip to content

Commit

Permalink
Merge pull request #8 from connect-group/has-attribute-enhancement
Browse files Browse the repository at this point in the history
HasAttribute enhancement
  • Loading branch information
rob-heaney-itg authored Jan 8, 2021
2 parents 167f6e4 + 07f4f6d commit a3a99a1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Include the latest release from Maven,
<dependency>
<groupId>com.connect-group</groupId>
<artifactId>thymeleaf-tdd</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<scope>test</scope>
</dependency>
Expand All @@ -20,7 +20,7 @@ If you are using Spring 4, the dependancy is slightly different.
<dependency>
<groupId>com.connect-group</groupId>
<artifactId>thymeleaf-tdd-spring4</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -237,7 +237,7 @@ Run the test again - Green bar! The test passes.
### Should Use Resource Bundle Text in Pre Section
Often the copy on the website will come from i18n resource bundles. These are usually handled by Spring MessageSource configurations.

Within Thymeleaf-TDD 1.0.4 you can test for these as well. As always in TDD, the test comes first.
Within Thymeleaf-TDD 1.0.5 you can test for these as well. As always in TDD, the test comes first.

@Test
public void shouldUseTextFromResourceBundle_WhenPreTagRefersToMessageSourceKey() throws Exception {
Expand Down
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;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public static Matcher<HtmlElement> hasAttribute(String attributeName) {
return new HasAttribute(attributeName);
}

@Factory
public static Matcher<HtmlElement> hasAttributeWithAnyNonNullValue(String attributeName) {
return new HasAttribute(attributeName, true);
}

@Factory
public static Matcher<HtmlElement> hasAttribute(String attributeName, String attributeValue) {
return new HasAttribute(attributeName, attributeValue);
Expand Down Expand Up @@ -77,5 +82,4 @@ public static Matcher<HtmlElement> hasTextBeforeElement(String text) {
public static Matcher<HtmlElement> hasTextAfterElement(String text) {
return new HasTextAfterElement(text);
}

}

0 comments on commit a3a99a1

Please sign in to comment.