Skip to content

Commit

Permalink
Merge pull request #31 from MilkyWare/master
Browse files Browse the repository at this point in the history
Added placeholder for isDate
  • Loading branch information
bodewig committed May 6, 2020
2 parents 523f85f + bbcabc3 commit 18c6308
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/net-placeholders/IsDatePlaceholderHandler.cs
@@ -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 src/tests/net-placeholders/IsDatePlaceholderHandlerTests.cs
@@ -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);
}
}
}

0 comments on commit 18c6308

Please sign in to comment.