Skip to content

Commit

Permalink
isDateTime now accepts an optional pattern as argument, closes #174
Browse files Browse the repository at this point in the history
  • Loading branch information
bodewig committed May 9, 2020
1 parent 7dc79d7 commit 1c25e01
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Expand Up @@ -51,7 +51,12 @@ public String getKeyword() {
}

@Override
public ComparisonResult evaluate(final String testText, final String... param) {
public ComparisonResult evaluate(final String testText, final String... args) {
if (args != null && args.length == 1) {
return canParse(new SimpleDateFormat(args[0]), testText)
? ComparisonResult.EQUAL
: ComparisonResult.DIFFERENT;
}
return canParse(testText)
? ComparisonResult.EQUAL
: ComparisonResult.DIFFERENT;
Expand Down
Expand Up @@ -57,6 +57,13 @@
* no argument at all, the comparison will fail. This is handled by
* {@link MatchesRegexPlaceholderHandler}.</li>
*
* <li>{@code ${xmlunit.isDateTime}} makes the comparison pass if the
* textual content of the element or attributes looks like a date or
* datetime in the current locale or parsed by ISO rules. An optional
* argument can be used to specify a {@link
* java.text.SimpleDateFormat} pattern to use when trying to parse the
* test. This is handled by {@link IsDateTimePlaceholderHandler}.</li>
*
* </ul>
*
* <p>The default delimiters of <code>${</code> and <code>}</code> can
Expand Down
Expand Up @@ -65,4 +65,12 @@ public void shouldRejectExtraContent() {
assertThat(placeholderHandler.evaluate("This is a test date 2020-01-01"),
equalTo(ComparisonResult.DIFFERENT));
}

@Test
public void shouldParseExplicitPattern() {
assertThat(placeholderHandler.evaluate("31 01 2020 12:34", "dd MM yyyy HH:mm"),
equalTo(ComparisonResult.EQUAL));
assertThat(placeholderHandler.evaluate("abc", "dd MM yyyy HH:mm"),
equalTo(ComparisonResult.DIFFERENT));
}
}
Expand Up @@ -434,6 +434,16 @@ public void isDateTimePlaceholder_Attribute_IsDateTime() {
assertFalse(diff.hasDifferences());
}

@Test
public void isDateTimePlaceholder_Attribute_IsDateTime_CustomFormat() {
String control = "<elem1 attr='${xmlunit.isDateTime(dd.MM.yyyy)}'/>";
String test = "<elem1 attr='05.09.2020'/>";
Diff diff = DiffBuilder.compare(control).withTest(test)
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();

assertFalse(diff.hasDifferences());
}

@Test
public void isDateTimePlaceholder_Element_NotDateTime() {
String control = "<elem1>${xmlunit.isDateTime}</elem1>";
Expand All @@ -451,4 +461,15 @@ public void isDateTimePlaceholder_Element_IsDateTime() {

assertFalse(diff.hasDifferences());
}

@Test
public void isDateTimePlaceholder_Element_IsDateTime_CustomFormat() {
String control = "<elem1>${xmlunit.isDateTime(dd.MM.yyyy)}</elem1>";
String test = "<elem1>05.09.2020</elem1>";
Diff diff = DiffBuilder.compare(control).withTest(test)
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();

assertFalse(diff.hasDifferences());
}

}

0 comments on commit 1c25e01

Please sign in to comment.