Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added placeholder for isDate #31

Merged
merged 7 commits into from May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}
}