Skip to content

Commit e4502f6

Browse files
committed
Small tweaks to api
1 parent 8449eab commit e4502f6

16 files changed

+165
-157
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ trim_trailing_whitespace = true
1111
[*.csproj]
1212
indent_style = space
1313
indent_size = 2
14+
15+
# CS1591: Missing XML comment for publicly visible type or member
16+
dotnet_diagnostic.CS1591.severity = suggestion

src/AngleSharp.Diffing.Tests/Core/DiffingEngineTestBase.cs

+27-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33

44
namespace AngleSharp.Diffing.Core
@@ -19,25 +19,45 @@ protected static HtmlDifferenceEngine CreateHtmlDiffEngine(
1919
)
2020
{
2121
return new HtmlDifferenceEngine(
22-
new MockFilterStrategy(nodeFilter, attrFilter),
23-
new MockMatcherStrategy(nodeMatcher, attrMatcher),
24-
new MockCompareStrategy(nodeComparer, attrComparer)
22+
new MockDiffingStrategy(
23+
nodeFilter, attrFilter,
24+
nodeMatcher, attrMatcher,
25+
nodeComparer, attrComparer
26+
)
2527
);
2628
}
2729

28-
class MockMatcherStrategy : IMatcherStrategy
30+
class MockDiffingStrategy : IDiffingStrategy
2931
{
32+
private readonly Func<ComparisonSource, FilterDecision>? _nodeFilter;
33+
private readonly Func<AttributeComparisonSource, FilterDecision>? _attrFilter;
3034
private readonly Func<DiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? _nodeMatcher;
3135
private readonly Func<DiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? _attrMatcher;
36+
private readonly Func<Comparison, CompareResult>? _nodeCompare;
37+
private readonly Func<AttributeComparison, CompareResult>? _attrCompare;
3238

33-
public MockMatcherStrategy(
39+
public MockDiffingStrategy(
40+
Func<ComparisonSource, FilterDecision>? nodeFilter = null,
41+
Func<AttributeComparisonSource, FilterDecision>? attrFilter = null,
3442
Func<DiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? nodeMatcher = null,
35-
Func<DiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? attrMatcher = null)
43+
Func<DiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? attrMatcher = null,
44+
Func<Comparison, CompareResult>? nodeCompare = null,
45+
Func<AttributeComparison, CompareResult>? attrCompare = null)
3646
{
47+
_nodeFilter = nodeFilter;
48+
_attrFilter = attrFilter;
3749
_nodeMatcher = nodeMatcher;
3850
_attrMatcher = attrMatcher;
51+
_nodeCompare = nodeCompare;
52+
_attrCompare = attrCompare;
3953
}
4054

55+
public FilterDecision Filter(in AttributeComparisonSource attributeComparisonSource)
56+
=> _attrFilter!(attributeComparisonSource);
57+
58+
public FilterDecision Filter(in ComparisonSource comparisonSource)
59+
=> _nodeFilter!(comparisonSource);
60+
4161
public IEnumerable<Comparison> Match(
4262
DiffContext context,
4363
SourceCollection controlNodes,
@@ -47,36 +67,6 @@ public IEnumerable<AttributeComparison> Match(
4767
DiffContext context,
4868
SourceMap controlAttributes,
4969
SourceMap testAttributes) => _attrMatcher!(context, controlAttributes, testAttributes);
50-
}
51-
52-
class MockFilterStrategy : IFilterStrategy
53-
{
54-
private readonly Func<ComparisonSource, FilterDecision>? _nodeFilter;
55-
private readonly Func<AttributeComparisonSource, FilterDecision>? _attrFilter;
56-
57-
public MockFilterStrategy(Func<ComparisonSource, FilterDecision>? nodeFilter = null, Func<AttributeComparisonSource, FilterDecision>? attrFilter = null)
58-
{
59-
_nodeFilter = nodeFilter;
60-
_attrFilter = attrFilter;
61-
}
62-
63-
public FilterDecision Filter(in AttributeComparisonSource attributeComparisonSource)
64-
=> _attrFilter!(attributeComparisonSource);
65-
66-
public FilterDecision Filter(in ComparisonSource comparisonSource)
67-
=> _nodeFilter!(comparisonSource);
68-
}
69-
70-
class MockCompareStrategy : ICompareStrategy
71-
{
72-
private readonly Func<Comparison, CompareResult>? _nodeCompare;
73-
private readonly Func<AttributeComparison, CompareResult>? _attrCompare;
74-
75-
public MockCompareStrategy(Func<Comparison, CompareResult>? nodeCompare = null, Func<AttributeComparison, CompareResult>? attrCompare = null)
76-
{
77-
_nodeCompare = nodeCompare;
78-
_attrCompare = attrCompare;
79-
}
8070

8171
public CompareResult Compare(in Comparison comparison)
8272
=> _nodeCompare!(comparison);

src/AngleSharp.Diffing.Tests/Core/HtmlDifferenceEngineTest.cs

+17-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using AngleSharp.Diffing.Strategies.NodeStrategies;
55
using Shouldly;
66
using Xunit;
7+
using System.Linq;
78

89
namespace AngleSharp.Diffing.Core
910
{
@@ -18,7 +19,8 @@ public void UnmatchedNodesBecomesMissingUnexpectedDiffs()
1819
{
1920
var sut = CreateHtmlDiffEngine(nodeMatcher: NoneNodeMatcher, nodeFilter: NoneNodeFilter);
2021

21-
var results = sut.Compare(ToNodeList("<p></p><!--comment-->text"), ToNodeList("<p></p><!--comment-->text"));
22+
var results = sut.Compare(ToNodeList("<p></p><!--comment-->text"), ToNodeList("<p></p><!--comment-->text"))
23+
.ToList();
2224

2325
results.Count.ShouldBe(6);
2426
results[0].ShouldSatisfyAllConditions(
@@ -58,7 +60,7 @@ public void AnyUnmatchedNodesBecomesMissingUnexpectedDiffs(int matchIndex)
5860
nodeFilter: NoneNodeFilter,
5961
nodeComparer: SameResultNodeComparer);
6062

61-
var results = sut.Compare(nodes, nodes);
63+
var results = sut.Compare(nodes, nodes).ToList();
6264

6365
results.Count.ShouldBe(2);
6466
results[0].ShouldBeOfType<MissingNodeDiff>().Control.Node.ShouldNotBe(nodes[matchIndex]);
@@ -74,7 +76,7 @@ public void FilteredOutNodesNotPartOfComparison(string html)
7476
var nodes = ToNodeList(html);
7577
var sut = CreateHtmlDiffEngine(nodeMatcher: NoneNodeMatcher, nodeFilter: RemoveCommentNodeFilter);
7678

77-
var results = sut.Compare(nodes, nodes);
79+
var results = sut.Compare(nodes, nodes).ToList();
7880

7981
results.ShouldNotContain(diff => diff.Target == DiffTarget.Comment);
8082
}
@@ -86,7 +88,7 @@ public void IndexesAreBasedOnInputNodeLists()
8688
var nodes2 = ToNodeList("<p></p><!--removed comment--><span></span>");
8789
var sut = CreateHtmlDiffEngine(nodeMatcher: NoneNodeMatcher, nodeFilter: RemoveCommentNodeFilter);
8890

89-
var results = sut.Compare(nodes, nodes);
91+
var results = sut.Compare(nodes, nodes).ToList();
9092

9193
results.Count.ShouldBe(4);
9294
results[0].ShouldBeOfType<MissingNodeDiff>().Control.Index.ShouldBe(0);
@@ -104,7 +106,7 @@ public void WhenNodesAreDifferentADiffIsReturned()
104106
nodeFilter: NoneNodeFilter,
105107
nodeComparer: DiffResultNodeComparer);
106108

107-
var results = sut.Compare(nodes, nodes);
109+
var results = sut.Compare(nodes, nodes).ToList();
108110

109111
results.Count.ShouldBe(3);
110112
results[0].ShouldBeOfType<Diff>().ShouldSatisfyAllConditions(
@@ -133,7 +135,7 @@ public void WhenNodesAreSameNoDiffIsReturned()
133135
nodeFilter: NoneNodeFilter,
134136
nodeComparer: SameResultNodeComparer);
135137

136-
var results = sut.Compare(nodes, nodes);
138+
var results = sut.Compare(nodes, nodes).ToList();
137139

138140
results.ShouldBeEmpty();
139141
}
@@ -151,7 +153,7 @@ public void UnmatchedAttr()
151153
attrFilter: NoneAttrFilter,
152154
attrComparer: SameResultAttrComparer);
153155

154-
var results = sut.Compare(nodes, nodes);
156+
var results = sut.Compare(nodes, nodes).ToList();
155157

156158
results.Count.ShouldBe(2);
157159
results[0].ShouldBeOfType<MissingAttrDiff>().ShouldSatisfyAllConditions(
@@ -183,7 +185,7 @@ public void PartialUnmatchedAttrs(string matchedAttr)
183185
attrFilter: NoneAttrFilter,
184186
attrComparer: SameResultAttrComparer);
185187

186-
var results = sut.Compare(nodes, nodes);
188+
var results = sut.Compare(nodes, nodes).ToList();
187189

188190
results.Count.ShouldBe(4);
189191
results[0].ShouldBeOfType<MissingAttrDiff>().Control.Attribute.Name.ShouldNotBe(matchedAttr);
@@ -208,7 +210,7 @@ public void FilteredAttrNotPartOfComparison(string filterOutAttrName)
208210
attrFilter: SpecificAttrFilter(filterOutAttrName),
209211
attrComparer: SameResultAttrComparer);
210212

211-
var results = sut.Compare(nodes, nodes);
213+
var results = sut.Compare(nodes, nodes).ToList();
212214

213215
results.Count.ShouldBe(4);
214216
results[0].ShouldBeOfType<MissingAttrDiff>().Control.Attribute.Name.ShouldNotBe(filterOutAttrName);
@@ -230,7 +232,7 @@ public void WhenMatchedAttrsAreDiffAttrDiffIsReturned()
230232
attrFilter: NoneAttrFilter,
231233
attrComparer: DiffResultAttrComparer);
232234

233-
var results = sut.Compare(nodes, nodes);
235+
var results = sut.Compare(nodes, nodes).ToList();
234236

235237
results.Count.ShouldBe(1);
236238
results[0].ShouldBeOfType<AttrDiff>().ShouldSatisfyAllConditions(
@@ -269,7 +271,7 @@ public void WhenBothTestAndControlHaveChildNodesTheseAreCompared()
269271
nodeFilter: NoneNodeFilter,
270272
nodeComparer: DiffResultNodeComparer);
271273

272-
var results = sut.Compare(nodes, nodes);
274+
var results = sut.Compare(nodes, nodes).ToList();
273275

274276
results.Count.ShouldBe(5);
275277
results[0].ShouldBeOfType<Diff>().Control.Node.NodeName.ShouldBe("MAIN");
@@ -289,7 +291,7 @@ public void OnlyOnePartHasChildNodes(string control, string test, Type expectedD
289291
nodeFilter: NoneNodeFilter,
290292
nodeComparer: DiffResultNodeComparer);
291293

292-
var results = sut.Compare(ToNodeList(control), ToNodeList(test));
294+
var results = sut.Compare(ToNodeList(control), ToNodeList(test)).ToList();
293295

294296
results.Count.ShouldBe(2);
295297
results[0].ShouldBeOfType<Diff>();
@@ -307,7 +309,7 @@ public void PathIsSetCorrectly()
307309
nodeFilter: RemoveCommentNodeFilter,
308310
nodeComparer: DiffResultNodeComparer);
309311

310-
var results = sut.Compare(ctrlNodes, testNodes);
312+
var results = sut.Compare(ctrlNodes, testNodes).ToList();
311313

312314
results.Count.ShouldBe(4);
313315
results[0].ShouldBeOfType<Diff>().Control.Path.ShouldBe("main(0)");
@@ -333,7 +335,7 @@ public void AttributeSourcePathisBasedOnParentElements()
333335
attrFilter: NoneAttrFilter,
334336
attrComparer: DiffResultAttrComparer);
335337

336-
var results = sut.Compare(nodes, nodes);
338+
var results = sut.Compare(nodes, nodes).ToList();
337339

338340
results.Count.ShouldBe(1);
339341
results[0].ShouldBeOfType<AttrDiff>().Control.Path.ShouldBe("p(0)[id]");
@@ -352,7 +354,7 @@ public void ComparisonSourcesHaveCorrectType()
352354
attrFilter: NoneAttrFilter,
353355
attrComparer: DiffResultAttrComparer);
354356

355-
var results = sut.Compare(nodes, nodes);
357+
var results = sut.Compare(nodes, nodes).ToList();
356358

357359
results.Count.ShouldBe(2);
358360

src/AngleSharp.Diffing.Tests/DiffBuilderTest.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using AngleSharp.Diffing.Core;
34
using AngleSharp.Diffing.Strategies;
45
using Shouldly;
@@ -41,7 +42,7 @@ public void Test003()
4142
.WithTest(test)
4243
.Build();
4344

44-
diffs.Count.ShouldBe(3);
45+
diffs.Count().ShouldBe(3);
4546
}
4647

4748
[Fact(DisplayName = "Setting options works")]
@@ -69,7 +70,8 @@ public void Test004()
6970
.AddComparer((in Comparison comparison, CompareResult currentDecision) => { nodeComparerCalled = true; return currentDecision; })
7071
.AddComparer((in AttributeComparison comparison, CompareResult currentDecision) => { attrComparerCalled = true; return currentDecision; })
7172
)
72-
.Build();
73+
.Build()
74+
.ToList();
7375

7476
nodeFilterCalled.ShouldBeTrue();
7577
attrFilterCalled.ShouldBeTrue();

src/AngleSharp.Diffing.sln

+3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngleSharp.DiffingTests", "
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E8E3C8B4-92C3-4DB7-B920-D28651E24A57}"
1111
ProjectSection(SolutionItems) = preProject
12+
..\.editorconfig = ..\.editorconfig
1213
..\tools\anglesharp.cake = ..\tools\anglesharp.cake
1314
AngleSharp.Diffing.nuspec = AngleSharp.Diffing.nuspec
15+
..\build.cake = ..\build.cake
16+
..\build.ps1 = ..\build.ps1
1417
Directory.Build.props = Directory.Build.props
1518
EndProjectSection
1619
EndProject

src/AngleSharp.Diffing/AngleSharp.Diffing.csproj

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

77
<PropertyGroup Condition="'$(Configuration)'=='Release'">
8-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
</PropertyGroup>
1010

11+
<PropertyGroup>
12+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
13+
<IncludeSymbols>true</IncludeSymbols>
14+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19351-01" PrivateAssets="All"/>
19+
</ItemGroup>
20+
1121
<ItemGroup>
1222
<PackageReference Include="AngleSharp" Version="$(AngleSharpVersion)" />
1323
<PackageReference Include="AngleSharp.Css" Version="$(AngleSharpVersion)" />
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
1-
using AngleSharp.Dom;
1+
using AngleSharp.Dom;
22
using System;
33

44
namespace AngleSharp.Diffing.Core
55
{
6+
/// <summary>
7+
/// A match between two attributes that should be compared.
8+
/// </summary>
69
public readonly struct AttributeComparison : IEquatable<AttributeComparison>
710
{
11+
/// <summary>
12+
/// Gets the control attribute which the <see cref="Test"/> attribute is supposed to match.
13+
/// </summary>
814
public AttributeComparisonSource Control { get; }
915

16+
/// <summary>
17+
/// Gets the test attribute which should be compared to the <see cref="Control"/> attribute.
18+
/// </summary>
1019
public AttributeComparisonSource Test { get; }
1120

21+
/// <summary>
22+
/// Create a attribute comparison match.
23+
/// </summary>
24+
/// <param name="control">The attribute control source</param>
25+
/// <param name="test">The attribute test source</param>
1226
public AttributeComparison(in AttributeComparisonSource control, in AttributeComparisonSource test)
1327
{
1428
Control = control;
1529
Test = test;
1630
}
1731

32+
/// <summary>
33+
/// Returns the control and test elements which the control and test attributes belongs to.
34+
/// </summary>
1835
public (IElement ControlElement, IElement TestElement) GetAttributeElements()
1936
=> ((IElement)Control.ElementSource.Node, (IElement)Test.ElementSource.Node);
2037

2138
#region Equals and HashCode
39+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
2240
public bool Equals(AttributeComparison other) => Control.Equals(other.Control) && Test.Equals(other.Test);
2341
public override bool Equals(object? obj) => obj is AttributeComparison other && Equals(other);
2442
public override int GetHashCode() => (Control, Test).GetHashCode();
2543
public static bool operator ==(AttributeComparison left, AttributeComparison right) => left.Equals(right);
2644
public static bool operator !=(AttributeComparison left, AttributeComparison right) => !left.Equals(right);
45+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
2746
#endregion
2847
}
2948
}

src/AngleSharp.Diffing/Core/AttributeComparisonSource.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using AngleSharp.Dom;
1+
using AngleSharp.Dom;
22
using AngleSharp.Diffing.Extensions;
33
using System;
44
using System.Diagnostics.CodeAnalysis;
@@ -29,11 +29,13 @@ public AttributeComparisonSource(string attributeName, in ComparisonSource eleme
2929
}
3030

3131
#region Equals and HashCode
32-
public bool Equals(AttributeComparisonSource other) => Object.ReferenceEquals(Attribute, other.Attribute) && Path.Equals(other.Path, StringComparison.Ordinal) && ElementSource.Equals(other.ElementSource) ;
32+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
33+
public bool Equals(AttributeComparisonSource other) => Object.ReferenceEquals(Attribute, other.Attribute) && Path.Equals(other.Path, StringComparison.Ordinal) && ElementSource.Equals(other.ElementSource);
3334
public override int GetHashCode() => (Attribute, ElementSource).GetHashCode();
3435
public override bool Equals(object? obj) => obj is AttributeComparisonSource other && Equals(other);
3536
public static bool operator ==(AttributeComparisonSource left, AttributeComparisonSource right) => left.Equals(right);
3637
public static bool operator !=(AttributeComparisonSource left, AttributeComparisonSource right) => !left.Equals(right);
38+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
3739
#endregion
3840
}
3941
}

0 commit comments

Comments
 (0)