Skip to content

Commit e029a45

Browse files
committed
port matchesRegex placeholder from #xmlunit/xmlunit/178
1 parent df361ba commit e029a45

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ element/attribute. This allows placeholders like
1414
[#32](https://github.com/xmlunit/xmlunit.net/pull/32) by
1515
[MilkyWare](https://github.com/MilkyWare)
1616

17+
* added a new `${xmlunit.matchesRegex(regex)}` placeholder
18+
based on Java PR [xmlunit/#178](https://github.com/xmlunit/xmlunit/issues/178) by
19+
[@Jazzyekim](https://github.com/Jazzyekim).
20+
1721
## XMLUnit.NET 2.7.2 - /Released 2020-03-08/
1822

1923
* the XPath values for comparisons resulting in `CHILD_LOOKUP`

src/main/net-placeholders/IsDateTimePlaceholderHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ namespace Org.XmlUnit.Placeholder
2424
/// <summary>
2525
/// Handler for the "isDateTime" handler placeholder keyword
2626
/// </summary>
27+
/// <remarks>
28+
/// <para>
29+
/// since 2.8.0
30+
/// </para>
31+
/// </remarks>
2732
public class IsDateTimePlaceholderHandler : IPlaceholderHandler
2833
{
2934
private const string _keyword = "isDateTime";
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
This file is licensed to You under the Apache License, Version 2.0
3+
(the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
using System.Text.RegularExpressions;
15+
using Org.XmlUnit.Diff;
16+
17+
namespace Org.XmlUnit.Placeholder {
18+
/// <summary>
19+
/// Handler for the "matchesRegex()" placeholder keyword.
20+
/// </summary>
21+
/// <remarks>
22+
/// <para>
23+
/// since 2.8.0
24+
/// </para>
25+
/// </remarks>
26+
public class MatchesRegexPlaceholderHandler : IPlaceholderHandler {
27+
private const string PLACEHOLDER_NAME = "matchesRegex";
28+
29+
/// <inheritdoc/>
30+
public string Keyword { get { return PLACEHOLDER_NAME; } }
31+
/// <inheritdoc/>
32+
public ComparisonResult Evaluate(string testText, params string[] args) {
33+
if (args.Length > 0 && args[0] != null && args[0] != "") {
34+
var pattern = new Regex(args[0].Trim());
35+
if (testText != null && Evaluate(testText.Trim(), pattern)) {
36+
return ComparisonResult.EQUAL;
37+
}
38+
}
39+
return ComparisonResult.DIFFERENT;
40+
}
41+
42+
private bool Evaluate(string testText, Regex pattern) {
43+
return pattern.Match(testText).Success;
44+
}
45+
}
46+
}

src/main/net-placeholders/NetFramework/XMLUnit.Placeholders.NetFramework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="..\IgnorePlaceholderHandler.cs" />
7575
<Compile Include="..\IsDateTimePlaceholderHandler.cs" />
7676
<Compile Include="..\IsNumberPlaceholderHandler.cs" />
77+
<Compile Include="..\MatchesRegexPlaceholderHandler.cs" />
7778
<Compile Include="..\PlaceholderDifferenceEvaluator.cs" />
7879
<Compile Include="..\PlaceholderSupport.cs" />
7980
</ItemGroup>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
This file is licensed to You under the Apache License, Version 2.0
3+
(the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
using NUnit.Framework;
15+
using Org.XmlUnit.Diff;
16+
using Org.XmlUnit.Placeholder;
17+
18+
namespace Org.XmlUnit.Placeholder
19+
{
20+
[TestFixture]
21+
public class MatchesRegexPlaceholderHandlerTest {
22+
23+
private IPlaceholderHandler placeholderHandler = new MatchesRegexPlaceholderHandler();
24+
25+
[Test]
26+
public void ShouldGetKeyword() {
27+
string expected = "matchesRegex";
28+
string keyword = placeholderHandler.Keyword;
29+
30+
Assert.AreEqual(expected, keyword);
31+
}
32+
33+
[Test]
34+
public void ShouldEvaluateGivenSimpleRegex() {
35+
string testTest = "1234";
36+
string regex = "^\\d+$";
37+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest, regex);
38+
39+
Assert.AreEqual(ComparisonResult.EQUAL, comparisonResult);
40+
}
41+
42+
[Test]
43+
public void ShouldNotEvaluateGivenNull() {
44+
string testTest = null;
45+
string regex = "^\\d+$";
46+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest, regex);
47+
48+
Assert.AreEqual(ComparisonResult.DIFFERENT, comparisonResult);
49+
}
50+
51+
[Test]
52+
public void ShouldNotEvaluateGivenEmptystring() {
53+
string testTest = "";
54+
string regex = "^\\d+$";
55+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest, regex);
56+
57+
Assert.AreEqual(ComparisonResult.DIFFERENT, comparisonResult);
58+
}
59+
60+
[Test]
61+
public void ShouldNotEvaluatestringDoesNotMatchRegex() {
62+
string testTest = "not parsable as a number even though it contains 123 numbers";
63+
string regex = "^\\d+$";
64+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest, regex);
65+
66+
Assert.AreEqual(ComparisonResult.DIFFERENT, comparisonResult);
67+
}
68+
69+
[Test]
70+
public void ShouldNotEvaluateWithNullRegex() {
71+
string testTest = "a string";
72+
string regex = null;
73+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest, regex);
74+
75+
Assert.AreEqual(ComparisonResult.DIFFERENT, comparisonResult);
76+
}
77+
78+
[Test]
79+
public void ShouldNotEvaluateWithEmptyRegex() {
80+
string testTest = "a string";
81+
string regex = "";
82+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest, regex);
83+
84+
Assert.AreEqual(ComparisonResult.DIFFERENT, comparisonResult);
85+
}
86+
}
87+
88+
}

src/tests/net-placeholders/NetFramework/XMLUnit.Placeholders.Tests.NetFramework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
</Compile>
6868
<Compile Include="..\IsDateTimePlaceholderHandlerTests.cs" />
6969
<Compile Include="..\IsNumberPlaceholderHandlerTest.cs" />
70+
<Compile Include="..\MatchesRegexPlaceholderHandlerTest.cs" />
7071
<Compile Include="..\PlaceholderDifferenceEvaluatorTest.cs" />
7172
<Compile Include="..\PlaceholderSupportTest.cs" />
7273
</ItemGroup>

0 commit comments

Comments
 (0)