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 #31 from MilkyWare/master
Added placeholder for isDate
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
This file is licensed to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using Org.XmlUnit.Diff; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Org.XmlUnit.Placeholder | ||
{ | ||
/// <summary> | ||
/// Handler for the "isDate" handler placeholder keyword | ||
/// </summary> | ||
public class IsDatePlaceholderHandler : IPlaceholderHandler | ||
{ | ||
private const string _keyword = "isDate"; | ||
|
||
/// <inheritdoc/> | ||
public string Keyword => _keyword; | ||
|
||
/// <inheritdoc/> | ||
public ComparisonResult Evaluate(string testText) | ||
{ | ||
var result = DateTime.TryParse(testText, out _); | ||
return result | ||
? ComparisonResult.EQUAL | ||
: ComparisonResult.DIFFERENT; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/tests/net-placeholders/IsDatePlaceholderHandlerTests.cs
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using NUnit.Framework; | ||
using Org.XmlUnit.Diff; | ||
using Org.XmlUnit.Placeholder; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace Org.XmlUnit.Placeholder | ||
{ | ||
[TestFixture()] | ||
public class IsDatePlaceholderHandlerTests | ||
{ | ||
private IPlaceholderHandler placeholderHandler = new IsDatePlaceholderHandler(); | ||
|
||
[Test] | ||
[TestCase("01-01-2020", TestName = "ShouldEvaluateDateWithDashes", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase("01/01/2020", TestName = "ShouldEvaluateDateWithDots", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase("01/01/2020", TestName = "ShouldEvaluateDateWithSlashes", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase("2020-01-01T15:00", TestName = "ShouldEvaluateSortableDate", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase("2020-01-01 15:00:00Z", TestName = "ShouldEvaluateUniversalDate", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase("01/01/2020 15:00", TestName = "ShouldEvaluateDateWithTime", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase(null, TestName = "ShouldNotEvaluateNull", ExpectedResult = ComparisonResult.DIFFERENT)] | ||
[TestCase("", TestName = "ShouldNotEvaluateEmpty", ExpectedResult = ComparisonResult.DIFFERENT)] | ||
[TestCase("This is a test date 01/01/2020", TestName = "ShouldNotEvaluateStringContainingDate", ExpectedResult = ComparisonResult.DIFFERENT)] | ||
public ComparisonResult EvaluateTest(string testText) | ||
{ | ||
return placeholderHandler.Evaluate(testText); | ||
} | ||
|
||
[Test] | ||
[TestCase("19/06/2020", "en-GB", TestName = "ShouldEvaluateUKDate", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase("06/19/2020", "en-US", TestName = "ShouldEvaluateUSDate", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase("19/06/2020", "fr-FR", TestName = "ShouldEvaluateFRDate", ExpectedResult = ComparisonResult.EQUAL)] | ||
[TestCase("19/06/2020", "es-ES", TestName = "ShouldEvaluateESDate", ExpectedResult = ComparisonResult.EQUAL)] | ||
public ComparisonResult EvaluateOtherCulturesTest(string testText, string cultureCode) | ||
{ | ||
CultureInfo.CurrentCulture = new CultureInfo(cultureCode); | ||
return placeholderHandler.Evaluate(testText); | ||
} | ||
|
||
[Test] | ||
public void ShouldGetKeyword() | ||
{ | ||
string expected = "isDate"; | ||
string keyword = placeholderHandler.Keyword; | ||
|
||
Assert.AreEqual(expected, keyword); | ||
} | ||
} | ||
} |