Skip to content

Commit 10e3ce2

Browse files
committed
port NodeFilters.SatisfiesAll|Any from XMLUnit.NET
1 parent 331348a commit 10e3ce2

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

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.9.2 - /not released, yet/
44

5+
* added `NodeFilters#SatisfiesAll` and `SatifiesAny` methods to make
6+
it easier to combine multiple node filters.
7+
added to simplify the use case of
8+
[xmlunit/#249](https://github.com/xmlunit/xmlunit/issues/249)
9+
510
## XMLUnit.NET 2.9.1 - /Released 2021-12-16/
611

712
* improved comparison performance for documents with many siblings

src/main/net-core/Diff/NodeFilters.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ the License. You may obtain a copy of the License at
1212
limitations under the License.
1313
*/
1414

15+
using System;
16+
using System.Linq;
1517
using System.Xml;
1618

1719
namespace Org.XmlUnit.Diff {
@@ -45,5 +47,38 @@ public static bool Default(XmlNode n) {
4547
public static bool AcceptAll(XmlNode n) {
4648
return true;
4749
}
50+
51+
/// <summary>
52+
/// Accepts nodes that are accepted by all given filters.
53+
/// </summary>
54+
/// <remarks>
55+
/// <para>
56+
/// This short-circuits the given list of predicates and
57+
/// returns false as soon as the first predicate does.
58+
/// </para>
59+
/// <para>
60+
/// since XMLUnit 2.9.2
61+
/// </para>
62+
/// </remarks>
63+
public static Predicate<XmlNode> SatifiesAll(params Predicate<XmlNode>[] predicates) {
64+
return node => predicates.All(p => p(node));
65+
}
66+
67+
/// <summary>
68+
/// Accepts nodes that are accepted by at least on of the given filters.
69+
/// </summary>
70+
/// <remarks>
71+
/// <para>
72+
/// This short-circuits the given list of predicates and
73+
/// returns true as soon as the first predicate does.
74+
/// </para>
75+
/// <para>
76+
/// since XMLUnit 2.9.2
77+
/// </para>
78+
/// </remarks>
79+
public static Predicate<XmlNode> SatifiesAny(params Predicate<XmlNode>[] predicates) {
80+
return node => predicates.Any(p => p(node));
81+
}
82+
4883
}
4984
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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;
16+
using System.Xml;
17+
18+
using NUnit.Framework;
19+
20+
namespace Org.XmlUnit.Diff {
21+
22+
[TestFixture]
23+
public class NodeFiltersTest {
24+
25+
internal class TestFilter {
26+
private readonly bool doReturn;
27+
internal bool called;
28+
29+
internal TestFilter(bool ret) {
30+
doReturn = ret;
31+
}
32+
33+
public bool Test(XmlNode n) {
34+
called = true;
35+
return doReturn;
36+
}
37+
}
38+
39+
40+
[Test]
41+
public void EmptySatisfiesAllReturnsTrue() {
42+
Assert.IsTrue(NodeFilters.SatifiesAll()(null));
43+
}
44+
45+
[Test]
46+
public void EmptySatisfiesAnyReturnsFalse() {
47+
Assert.IsFalse(NodeFilters.SatifiesAny()(null));
48+
}
49+
50+
[Test]
51+
public void SatisfiesAllWorksAsPromised() {
52+
TestFilter n1 = new TestFilter(true);
53+
TestFilter n2 = new TestFilter(false);
54+
TestFilter n3 = new TestFilter(true);
55+
Assert.IsTrue(NodeFilters.SatifiesAll(n1.Test)(null));
56+
Assert.IsFalse(NodeFilters.SatifiesAll(n1.Test, n2.Test, n3.Test)(null));
57+
Assert.IsFalse(n3.called);
58+
Assert.IsTrue(NodeFilters.SatifiesAll(n1.Test, n3.Test)(null));
59+
}
60+
61+
[Test]
62+
public void SatisfiesAnyWorksAsPromised() {
63+
TestFilter n1 = new TestFilter(false);
64+
TestFilter n2 = new TestFilter(true);
65+
TestFilter n3 = new TestFilter(false);
66+
Assert.IsFalse(NodeFilters.SatifiesAny(n1.Test)(null));
67+
Assert.IsTrue(NodeFilters.SatifiesAny(n1.Test, n2.Test, n3.Test)(null));
68+
Assert.IsFalse(n3.called);
69+
Assert.IsFalse(NodeFilters.SatifiesAny(n1.Test, n3.Test)(null));
70+
}
71+
}
72+
}

src/tests/net-core/NetFramework/XMLUnit.Core.Tests.NetFramework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<Compile Include="..\Diff\DOMDifferenceEngineTest.cs" />
8282
<Compile Include="..\Diff\ElementSelectorsTest.cs" />
8383
<Compile Include="..\Diff\MultiLevelByNameAndTextSelectorTest.cs" />
84+
<Compile Include="..\Diff\NodeFiltersTest.cs" />
8485
<Compile Include="..\Diff\RecursiveXPathBuilderTest.cs" />
8586
<Compile Include="..\Diff\XPathContextTest.cs" />
8687
<Compile Include="..\Input\CommentLessSourceTest.cs" />

0 commit comments

Comments
 (0)