Skip to content

Commit d60d352

Browse files
committed
port xmlunit/#154 to XMLUnit.Net
1 parent f5a1715 commit d60d352

6 files changed

Lines changed: 193 additions & 0 deletions

File tree

RELEASE_NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## XMLUnit.NET 2.7.1 - /Not Released, yet/
44

5+
* add a new `${xmlunit.isNumber}` placeholder
6+
Based on the Java PR
7+
[xmlunit/#154](https://github.com/xmlunit/xmlunit/pull/154) by
8+
[@NathanAtClarity](https://github.com/NathanAtClarity).
9+
510
## XMLUnit.NET 2.7.0 - /Released 2019-04-13/
611

712
This release is identical to 2.7.0-beta-01 with only the version
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
15+
using System.Text.RegularExpressions;
16+
17+
using Org.XmlUnit.Diff;
18+
19+
namespace Org.XmlUnit.Placeholder {
20+
/// <summary>
21+
/// Handler for the "isNumber" placeholder keyword.
22+
/// </summary>
23+
/// <remarks>
24+
/// <para>
25+
/// since 2.7.1
26+
/// </para>
27+
/// </remarks>
28+
public class IsNumberPlaceholderHandler : IPlaceholderHandler {
29+
private const string PLACEHOLDER_NAME = "isNumber";
30+
private const string NUMBER_PATTERN = "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$";
31+
private static readonly Regex NUMBER_PATTERN_REGEX = new Regex(NUMBER_PATTERN);
32+
33+
/// <inheritdoc/>
34+
public string Keyword { get { return PLACEHOLDER_NAME; } }
35+
/// <inheritdoc/>
36+
public ComparisonResult Evaluate(string testText) {
37+
return testText != null && NUMBER_PATTERN_REGEX.Match(testText).Success
38+
? ComparisonResult.EQUAL : ComparisonResult.DIFFERENT;
39+
}
40+
}
41+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="..\AssemblyInfo.cs" />
7373
<Compile Include="..\IPlaceholderHandler.cs" />
7474
<Compile Include="..\IgnorePlaceholderHandler.cs" />
75+
<Compile Include="..\IsNumberPlaceholderHandler.cs" />
7576
<Compile Include="..\PlaceholderDifferenceEvaluator.cs" />
7677
<Compile Include="..\PlaceholderSupport.cs" />
7778
</ItemGroup>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
15+
using NUnit.Framework;
16+
using Org.XmlUnit;
17+
using Org.XmlUnit.Diff;
18+
19+
namespace Org.XmlUnit.Placeholder {
20+
21+
[TestFixture]
22+
public class IsNumberPlaceholderHandlerTest {
23+
private IPlaceholderHandler placeholderHandler = new IsNumberPlaceholderHandler();
24+
25+
[Test]
26+
public void ShouldGetKeyword() {
27+
string expected = "isNumber";
28+
string keyword = placeholderHandler.Keyword;
29+
30+
Assert.AreEqual(expected, keyword);
31+
}
32+
33+
[Test]
34+
public void ShouldEvaluateGivenSimpleNumber() {
35+
string testTest = "1234";
36+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
37+
38+
Assert.AreEqual(ComparisonResult.EQUAL, comparisonResult);
39+
}
40+
41+
[Test]
42+
public void ShouldEvaluateGivenFloatingPointNumber() {
43+
string testTest = "12.34";
44+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
45+
46+
Assert.AreEqual(ComparisonResult.EQUAL, comparisonResult);
47+
}
48+
49+
[Test]
50+
public void ShouldEvaluateGivenNegativeNumber() {
51+
string testTest = "-1234";
52+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
53+
54+
Assert.AreEqual(ComparisonResult.EQUAL, comparisonResult);
55+
}
56+
57+
[Test]
58+
public void ShouldEvaluateGivenNegativeFloatingPointNumber() {
59+
string testTest = "-12.34";
60+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
61+
62+
Assert.AreEqual(ComparisonResult.EQUAL, comparisonResult);
63+
}
64+
65+
[Test]
66+
public void ShouldEvaluateGivenEngineeringNotationFloatingPointNumber() {
67+
string testTest = "1.7E+3";
68+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
69+
70+
Assert.AreEqual(ComparisonResult.EQUAL, comparisonResult);
71+
}
72+
73+
[Test]
74+
public void ShouldEvaluateGivenNegativeEngineeringNotationFloatingPointNumber() {
75+
string testTest = "-1.7E+3";
76+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
77+
78+
Assert.AreEqual(ComparisonResult.EQUAL, comparisonResult);
79+
}
80+
81+
[Test]
82+
public void ShouldNotEvaluateGivenNull() {
83+
string testTest = null;
84+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
85+
86+
Assert.AreEqual(ComparisonResult.DIFFERENT, comparisonResult);
87+
}
88+
89+
[Test]
90+
public void ShouldNotEvaluateGivenEmptystring() {
91+
string testTest = "";
92+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
93+
94+
Assert.AreEqual(ComparisonResult.DIFFERENT, comparisonResult);
95+
}
96+
97+
[Test]
98+
public void ShouldNotEvaluateGivenNonNumberstring() {
99+
string testTest = "not parsable as a number even though it contains 123 numbers";
100+
ComparisonResult comparisonResult = placeholderHandler.Evaluate(testTest);
101+
102+
Assert.AreEqual(ComparisonResult.DIFFERENT, comparisonResult);
103+
}
104+
105+
}
106+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="..\..\..\shared\CommonAssemblyInfo.cs">
6666
<Link>..\CommonAssemblyInfo.cs</Link>
6767
</Compile>
68+
<Compile Include="..\IsNumberPlaceholderHandlerTest.cs" />
6869
<Compile Include="..\PlaceholderDifferenceEvaluatorTest.cs" />
6970
<Compile Include="..\PlaceholderSupportTest.cs" />
7071
</ItemGroup>

src/tests/net-placeholders/PlaceholderDifferenceEvaluatorTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,44 @@ public void HasIgnorePlaceholder_Attribute_Exception_ExclusivelyOccupy() {
308308
Assert.Throws<XMLUnitException>(() => diffBuilder.Build(), "The placeholder must exclusively occupy the text node.");
309309
}
310310

311+
[Test]
312+
public void HasIsNumberPlaceholder_Attribute_NotNumber() {
313+
string control = "<elem1 attr='${xmlunit.isNumber}'/>";
314+
string test = "<elem1 attr='abc'/>";
315+
var diff = DiffBuilder.Compare(control).WithTest(test)
316+
.WithDifferenceEvaluator(new PlaceholderDifferenceEvaluator().Evaluate).Build();
317+
318+
Assert.IsTrue(diff.HasDifferences());
319+
}
320+
321+
[Test]
322+
public void HasIsNumberPlaceholder_Attribute_IsNumber() {
323+
string control = "<elem1 attr='${xmlunit.isNumber}'/>";
324+
string test = "<elem1 attr='123'/>";
325+
var diff = DiffBuilder.Compare(control).WithTest(test)
326+
.WithDifferenceEvaluator(new PlaceholderDifferenceEvaluator().Evaluate).Build();
327+
328+
Assert.IsFalse(diff.HasDifferences());
329+
}
330+
331+
[Test]
332+
public void HasIsNumberPlaceholder_Element_NotNumber() {
333+
string control = "<elem1>${xmlunit.isNumber}</elem1>";
334+
string test = "<elem1>abc</elem1>";
335+
var diff = DiffBuilder.Compare(control).WithTest(test)
336+
.WithDifferenceEvaluator(new PlaceholderDifferenceEvaluator().Evaluate).Build();
337+
338+
Assert.IsTrue(diff.HasDifferences());
339+
}
340+
341+
[Test]
342+
public void HasIsNumberPlaceholder_Element_IsNumber() {
343+
string control = "<elem1>${xmlunit.isNumber}</elem1>";
344+
string test = "<elem1>123</elem1>";
345+
var diff = DiffBuilder.Compare(control).WithTest(test)
346+
.WithDifferenceEvaluator(new PlaceholderDifferenceEvaluator().Evaluate).Build();
347+
348+
Assert.IsFalse(diff.HasDifferences());
349+
}
311350
}
312351
}

0 commit comments

Comments
 (0)