Skip to content

Commit eaf75a4

Browse files
committed
Refactored engine to clear single use.
1 parent 02484c7 commit eaf75a4

25 files changed

+223
-190
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ public DiffingEngineTestBase(DiffingTestFixture fixture) : base(fixture)
99
{
1010
}
1111

12-
protected static HtmlDifferenceEngine CreateHtmlDiffEngine(
13-
Func<DiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? nodeMatcher = null,
14-
Func<DiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? attrMatcher = null,
12+
protected static HtmlDiffer CreateHtmlDiffEngine(
13+
Func<IDiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? nodeMatcher = null,
14+
Func<IDiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? attrMatcher = null,
1515
Func<ComparisonSource, FilterDecision>? nodeFilter = null,
1616
Func<AttributeComparisonSource, FilterDecision>? attrFilter = null,
1717
Func<Comparison, CompareResult>? nodeComparer = null,
1818
Func<AttributeComparison, CompareResult>? attrComparer = null
1919
)
2020
{
21-
return new HtmlDifferenceEngine(
21+
return new HtmlDiffer(
2222
new MockDiffingStrategy(
2323
nodeFilter, attrFilter,
2424
nodeMatcher, attrMatcher,
@@ -31,16 +31,16 @@ class MockDiffingStrategy : IDiffingStrategy
3131
{
3232
private readonly Func<ComparisonSource, FilterDecision>? _nodeFilter;
3333
private readonly Func<AttributeComparisonSource, FilterDecision>? _attrFilter;
34-
private readonly Func<DiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? _nodeMatcher;
35-
private readonly Func<DiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? _attrMatcher;
34+
private readonly Func<IDiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? _nodeMatcher;
35+
private readonly Func<IDiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? _attrMatcher;
3636
private readonly Func<Comparison, CompareResult>? _nodeCompare;
3737
private readonly Func<AttributeComparison, CompareResult>? _attrCompare;
3838

3939
public MockDiffingStrategy(
4040
Func<ComparisonSource, FilterDecision>? nodeFilter = null,
4141
Func<AttributeComparisonSource, FilterDecision>? attrFilter = null,
42-
Func<DiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? nodeMatcher = null,
43-
Func<DiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? attrMatcher = null,
42+
Func<IDiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? nodeMatcher = null,
43+
Func<IDiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? attrMatcher = null,
4444
Func<Comparison, CompareResult>? nodeCompare = null,
4545
Func<AttributeComparison, CompareResult>? attrCompare = null)
4646
{
@@ -59,12 +59,12 @@ public FilterDecision Filter(in ComparisonSource comparisonSource)
5959
=> _nodeFilter!(comparisonSource);
6060

6161
public IEnumerable<Comparison> Match(
62-
DiffContext context,
62+
IDiffContext context,
6363
SourceCollection controlNodes,
6464
SourceCollection testNodes) => _nodeMatcher!(context, controlNodes, testNodes);
6565

6666
public IEnumerable<AttributeComparison> Match(
67-
DiffContext context,
67+
IDiffContext context,
6868
SourceMap controlAttributes,
6969
SourceMap testAttributes) => _attrMatcher!(context, controlAttributes, testAttributes);
7070

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,17 +360,17 @@ public void Test2()
360360
#endregion
361361

362362
#region NodeMatchers
363-
private static IEnumerable<Comparison> NoneNodeMatcher(DiffContext ctx, SourceCollection controlNodes, SourceCollection testNodes)
363+
private static IEnumerable<Comparison> NoneNodeMatcher(IDiffContext ctx, SourceCollection controlNodes, SourceCollection testNodes)
364364
=> Array.Empty<Comparison>();
365365

366-
private static Func<DiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>> SpecificIndexNodeMatcher(int index)
366+
private static Func<IDiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>> SpecificIndexNodeMatcher(int index)
367367
=> (ctx, controlNodes, testNodes) =>
368368
{
369369
return new List<Comparison> { new Comparison(controlNodes[index], testNodes[index]) };
370370
};
371371

372372
private static IEnumerable<Comparison> OneToOneNodeListMatcher(
373-
DiffContext context,
373+
IDiffContext context,
374374
SourceCollection controlNodes,
375375
SourceCollection testNodes) => OneToOneNodeMatcher.Match(context, controlNodes, testNodes);
376376

@@ -383,19 +383,19 @@ private static IEnumerable<Comparison> OneToOneNodeListMatcher(
383383

384384
#region AttributeMatchers
385385
private static IReadOnlyList<AttributeComparison> NoneAttributeMatcher(
386-
DiffContext context,
386+
IDiffContext context,
387387
SourceMap controlAttributes,
388388
SourceMap testAttributes) => Array.Empty<AttributeComparison>();
389389

390-
private static Func<DiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>> SpecificAttributeMatcher(string matchAttrName)
390+
private static Func<IDiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>> SpecificAttributeMatcher(string matchAttrName)
391391
{
392392
return (ctx, ctrlAttrs, testAttrs) => new List<AttributeComparison>
393393
{
394394
new AttributeComparison(ctrlAttrs[matchAttrName], testAttrs[matchAttrName] )
395395
};
396396
}
397397

398-
private static IEnumerable<AttributeComparison> AttributeNameMatcher(DiffContext context, SourceMap controlAttrs, SourceMap testAttrs)
398+
private static IEnumerable<AttributeComparison> AttributeNameMatcher(IDiffContext context, SourceMap controlAttrs, SourceMap testAttrs)
399399
{
400400
foreach (var ctrlAttrSrc in controlAttrs)
401401
{

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using Shouldly;
44
using Xunit;
@@ -49,22 +49,17 @@ public void Test2()
4949
sut.Count.ShouldBe(2);
5050
}
5151

52-
[Fact(DisplayName = "Collection should make sources accessible on the index they specify")]
52+
[Fact(DisplayName = "Collection throws if sources is not ordered source-index when enumerated")]
5353
public void Test3()
5454
{
5555
var sources = ToNodeList("<span></span><p></p><main></main><h1></h1><ul></ul><ol></ol><em></em>")
5656
.ToComparisonSourceList(ComparisonSourceType.Control)
5757
.OrderBy(x => x.Node.NodeName);
5858

59-
var sut = new SourceCollection(ComparisonSourceType.Control, sources);
59+
var cut = new SourceCollection(ComparisonSourceType.Control, sources);
6060

61-
sut[0].Index.ShouldBe(0);
62-
sut[1].Index.ShouldBe(1);
63-
sut[2].Index.ShouldBe(2);
64-
sut[3].Index.ShouldBe(3);
65-
sut[4].Index.ShouldBe(4);
66-
sut[5].Index.ShouldBe(5);
67-
sut[6].Index.ShouldBe(6);
61+
Should.Throw<InvalidOperationException>(() => cut.ToList());
62+
Should.Throw<InvalidOperationException>(() => cut.GetAllSources().ToList());
6863
}
6964

7065
[Fact(DisplayName = "When an source is removed from the list, it does not add to the count nor is it returned when iterating")]

src/AngleSharp.Diffing.Tests/DiffingTestBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public abstract class DiffingTestBase : IClassFixture<DiffingTestFixture>
1111
{
1212
private readonly DiffingTestFixture _testFixture;
1313

14+
protected IDiffContext DummyContext { get; } = new DiffContext(default(IElement), default(IElement));
15+
1416
protected INodeList EmptyNodeList => ToNodeList("");
1517

1618
public DiffingTestBase(DiffingTestFixture fixture)

src/AngleSharp.Diffing.Tests/Strategies/AttributeStrategies/AttributeNameMatcherTest.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
66
using AngleSharp.Diffing;
77
using AngleSharp.Diffing.Core;
8+
using AngleSharp.Dom;
89
using Shouldly;
910
using Xunit;
1011

1112
namespace AngleSharp.Diffing.Strategies.AttributeStrategies
1213
{
1314
public class AttributeNameMatcherTest : DiffingTestBase
1415
{
15-
private readonly DiffContext _context = new DiffContext(null, null);
16-
1716
public AttributeNameMatcherTest(DiffingTestFixture fixture) : base(fixture)
1817
{
1918
}
@@ -24,7 +23,7 @@ public void Test001()
2423
var controls = ToSourceMap("<p>", ComparisonSourceType.Control);
2524
var tests = ToSourceMap("<p>", ComparisonSourceType.Test);
2625

27-
var actual = AttributeNameMatcher.Match(_context, controls, tests).ToList();
26+
var actual = AttributeNameMatcher.Match(DummyContext, controls, tests).ToList();
2827

2928
actual.ShouldBeEmpty();
3029
}
@@ -38,7 +37,7 @@ public void Test002(string controlHtml, string testHtml, int expectedMatches)
3837
var controls = ToSourceMap(controlHtml, ComparisonSourceType.Control);
3938
var tests = ToSourceMap(testHtml, ComparisonSourceType.Test);
4039

41-
var actual = AttributeNameMatcher.Match(_context, controls, tests).ToList();
40+
var actual = AttributeNameMatcher.Match(DummyContext, controls, tests).ToList();
4241

4342
actual.Count.ShouldBe(expectedMatches);
4443
actual.ShouldAllBe(c => c.Control.Attribute.Name == c.Test.Attribute.Name);
@@ -53,7 +52,7 @@ public void Test003(string controlHtml, string testHtml, int expectedMatches)
5352
var controls = ToSourceMap(controlHtml, ComparisonSourceType.Control);
5453
var tests = ToSourceMap(testHtml, ComparisonSourceType.Test);
5554

56-
var actual = AttributeNameMatcher.Match(_context, controls, tests).ToList();
55+
var actual = AttributeNameMatcher.Match(DummyContext, controls, tests).ToList();
5756

5857
actual.Count.ShouldBe(expectedMatches);
5958
actual.ShouldAllBe(c => c.Control.Attribute.Name == c.Test.Attribute.Name);
@@ -66,7 +65,7 @@ public void Test004()
6665
var tests = ToSourceMap(@"<p foo=""bar"">", ComparisonSourceType.Test);
6766
controls.MarkAsMatched(controls["foo"]);
6867

69-
var actual = AttributeNameMatcher.Match(_context, controls, tests).ToList();
68+
var actual = AttributeNameMatcher.Match(DummyContext, controls, tests).ToList();
7069

7170
actual.ShouldBeEmpty();
7271
}
@@ -78,7 +77,7 @@ public void Test005()
7877
var tests = ToSourceMap(@"<p foo=""bar"">", ComparisonSourceType.Test);
7978
tests.MarkAsMatched(tests["foo"]);
8079

81-
var actual = AttributeNameMatcher.Match(_context, controls, tests).ToList();
80+
var actual = AttributeNameMatcher.Match(DummyContext, controls, tests).ToList();
8281

8382
actual.ShouldBeEmpty();
8483
}

src/AngleSharp.Diffing.Tests/Strategies/AttributeStrategies/PostfixedAttributeMatcherTest.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33
using AngleSharp.Diffing.Core;
4+
using AngleSharp.Dom;
45
using Shouldly;
56
using Xunit;
67

78
namespace AngleSharp.Diffing.Strategies.AttributeStrategies
89
{
910
public class PostfixedAttributeMatcherTest : DiffingTestBase
1011
{
11-
private readonly DiffContext _context = new DiffContext(null, null);
12-
1312
public PostfixedAttributeMatcherTest(DiffingTestFixture fixture) : base(fixture)
1413
{
1514
}
@@ -29,7 +28,7 @@ public void Test001()
2928
var controls = ToSourceMap(@"<p foo>", ComparisonSourceType.Control);
3029
var tests = ToSourceMap(@"<p foo>", ComparisonSourceType.Test);
3130

32-
var actual = PostfixedAttributeMatcher.Match(_context, controls, tests).ToList();
31+
var actual = PostfixedAttributeMatcher.Match(DummyContext, controls, tests).ToList();
3332

3433
actual.ShouldBeEmpty();
3534
}
@@ -42,7 +41,7 @@ public void Test002(string diffPostfix)
4241
var tests = ToSourceMap(@"<p foo>", ComparisonSourceType.Test);
4342
controls.MarkAsMatched(controls[$"foo{diffPostfix}"]);
4443

45-
var actual = PostfixedAttributeMatcher.Match(_context, controls, tests).ToList();
44+
var actual = PostfixedAttributeMatcher.Match(DummyContext, controls, tests).ToList();
4645

4746
actual.ShouldBeEmpty();
4847
}
@@ -55,7 +54,7 @@ public void Test003(string diffPostfix)
5554
var tests = ToSourceMap(@"<p foo>", ComparisonSourceType.Test);
5655
tests.MarkAsMatched(tests["foo"]);
5756

58-
var actual = PostfixedAttributeMatcher.Match(_context, controls, tests).ToList();
57+
var actual = PostfixedAttributeMatcher.Match(DummyContext, controls, tests).ToList();
5958

6059
actual.ShouldBeEmpty();
6160
}
@@ -67,7 +66,7 @@ public void Test004(string diffPostfix)
6766
var controls = ToSourceMap($@"<p foo{diffPostfix}>", ComparisonSourceType.Control);
6867
var tests = ToSourceMap(@"<p foo>", ComparisonSourceType.Test);
6968

70-
var actual = PostfixedAttributeMatcher.Match(_context, controls, tests).ToList();
69+
var actual = PostfixedAttributeMatcher.Match(DummyContext, controls, tests).ToList();
7170

7271
actual.Count.ShouldBe(1);
7372
actual[0].ShouldSatisfyAllConditions(

src/AngleSharp.Diffing.Tests/Strategies/DiffingStrategyPipelineTest.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using AngleSharp.Diffing.Core;
4+
using AngleSharp.Dom;
45
using Shouldly;
56
using Xunit;
67

78
namespace AngleSharp.Diffing.Strategies
89
{
910
public class DiffingStrategyPipelineTest : DiffingTestBase
1011
{
11-
private readonly DiffContext _dummyContext = new DiffContext(null, null);
12-
1312
public DiffingStrategyPipelineTest(DiffingTestFixture fixture) : base(fixture)
1413
{
1514
}
@@ -100,7 +99,7 @@ public void Test61()
10099
sut.AddMatcher((ctx, s, t) => new[] { new Comparison(s[0], t[0]) }, isSpecializedMatcher: true);
101100
sut.AddMatcher((ctx, s, t) => new[] { new Comparison(s[1], t[1]) }, isSpecializedMatcher: true);
102101

103-
var result = sut.Match(_dummyContext, sourceColllection, sourceColllection).ToList();
102+
var result = sut.Match(DummyContext, sourceColllection, sourceColllection).ToList();
104103

105104
result[0].Control.ShouldBe(sourceColllection[1]);
106105
result[1].Control.ShouldBe(sourceColllection[0]);
@@ -114,7 +113,7 @@ public void Test71()
114113
sut.AddMatcher((ctx, s, t) => new[] { new AttributeComparison(s["foo"], t["foo"]) }, isSpecializedMatcher: true);
115114
sut.AddMatcher((ctx, s, t) => new[] { new AttributeComparison(s["baz"], t["baz"]) }, isSpecializedMatcher: true);
116115

117-
var result = sut.Match(_dummyContext, sourceMap, sourceMap).ToList();
116+
var result = sut.Match(DummyContext, sourceMap, sourceMap).ToList();
118117

119118
result[0].Control.ShouldBe(sourceMap["baz"]);
120119
result[1].Control.ShouldBe(sourceMap["foo"]);
@@ -128,7 +127,7 @@ public void Test62()
128127
sut.AddMatcher((ctx, s, t) => new[] { new Comparison(s[0], t[0]) }, isSpecializedMatcher: false);
129128
sut.AddMatcher((ctx, s, t) => new[] { new Comparison(s[1], t[1]) }, isSpecializedMatcher: false);
130129

131-
var result = sut.Match(_dummyContext, sourceColllection, sourceColllection).ToList();
130+
var result = sut.Match(DummyContext, sourceColllection, sourceColllection).ToList();
132131

133132
result[0].Control.ShouldBe(sourceColllection[0]);
134133
result[1].Control.ShouldBe(sourceColllection[1]);
@@ -142,7 +141,7 @@ public void Test72()
142141
sut.AddMatcher((ctx, s, t) => new[] { new AttributeComparison(s["foo"], t["foo"]) }, isSpecializedMatcher: false);
143142
sut.AddMatcher((ctx, s, t) => new[] { new AttributeComparison(s["baz"], t["baz"]) }, isSpecializedMatcher: false);
144143

145-
var result = sut.Match(_dummyContext, sourceMap, sourceMap).ToList();
144+
var result = sut.Match(DummyContext, sourceMap, sourceMap).ToList();
146145

147146
result[0].Control.ShouldBe(sourceMap["foo"]);
148147
result[1].Control.ShouldBe(sourceMap["baz"]);
@@ -156,7 +155,7 @@ public void Test62123()
156155
sut.AddMatcher((ctx, s, t) => new[] { new Comparison(s[0], t[0]) }, isSpecializedMatcher: false);
157156
sut.AddMatcher((ctx, s, t) => new[] { new Comparison(s[1], t[1]) }, isSpecializedMatcher: true);
158157

159-
var result = sut.Match(_dummyContext, sourceColllection, sourceColllection).ToList();
158+
var result = sut.Match(DummyContext, sourceColllection, sourceColllection).ToList();
160159

161160
result[0].Control.ShouldBe(sourceColllection[1]);
162161
result[1].Control.ShouldBe(sourceColllection[0]);
@@ -170,7 +169,7 @@ public void Test74342()
170169
sut.AddMatcher((ctx, s, t) => new[] { new AttributeComparison(s["foo"], t["foo"]) }, isSpecializedMatcher: false);
171170
sut.AddMatcher((ctx, s, t) => new[] { new AttributeComparison(s["baz"], t["baz"]) }, isSpecializedMatcher: true);
172171

173-
var result = sut.Match(_dummyContext, sourceMap, sourceMap).ToList();
172+
var result = sut.Match(DummyContext, sourceMap, sourceMap).ToList();
174173

175174
result[0].Control.ShouldBe(sourceMap["baz"]);
176175
result[1].Control.ShouldBe(sourceMap["foo"]);
@@ -237,13 +236,12 @@ public void Test8314(CompareResult first, CompareResult final)
237236
[Fact(DisplayName = "After two nodes has been matched, they are marked as matched in the source collection")]
238237
public void Test101()
239238
{
240-
var context = new DiffContext(null, null);
241239
var controlSources = ToSourceCollection("<p></p>", ComparisonSourceType.Control);
242240
var testSources = ToSourceCollection("<p></p>", ComparisonSourceType.Test);
243241
var sut = new DiffingStrategyPipeline();
244242
sut.AddMatcher((ctx, s, t) => new[] { new Comparison(s[0], t[0]) }, isSpecializedMatcher: true);
245243

246-
var result = sut.Match(context, controlSources, testSources).ToList();
244+
var result = sut.Match(DummyContext, controlSources, testSources).ToList();
247245

248246
controlSources.GetUnmatched().ShouldBeEmpty();
249247
testSources.GetUnmatched().ShouldBeEmpty();
@@ -252,13 +250,12 @@ public void Test101()
252250
[Fact(DisplayName = "After two attributes has been matched, they are marked as matched in the source map")]
253251
public void Test102()
254252
{
255-
var context = new DiffContext(null, null);
256253
var controlSources = ToSourceMap(@"<p foo=""bar""></p>", ComparisonSourceType.Control);
257254
var testSources = ToSourceMap(@"<p foo=""bar""></p>", ComparisonSourceType.Test);
258255
var sut = new DiffingStrategyPipeline();
259256
sut.AddMatcher((ctx, s, t) => new[] { new AttributeComparison(s["foo"], t["foo"]) }, isSpecializedMatcher: true);
260257

261-
var result = sut.Match(context, controlSources, testSources).ToList();
258+
var result = sut.Match(DummyContext, controlSources, testSources).ToList();
262259

263260
controlSources.GetUnmatched().ShouldBeEmpty();
264261
testSources.GetUnmatched().ShouldBeEmpty();

src/AngleSharp.Diffing.Tests/Strategies/ElementStrategies/CssSelectorElementMatcherTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
@@ -24,11 +24,10 @@ public CssSelectorElementMatcherTest(DiffingTestFixture fixture) : base(fixture)
2424
[InlineData("<p></p>")]
2525
public void Test001(string html)
2626
{
27-
var context = new DiffContext(null, null);
2827
var controls = ToSourceCollection(html, ComparisonSourceType.Control);
2928
var tests = ToSourceCollection(html, ComparisonSourceType.Test);
3029

31-
var actual = CssSelectorElementMatcher.Match(context, controls, tests).ToList();
30+
var actual = CssSelectorElementMatcher.Match(DummyContext, controls, tests).ToList();
3231

3332
actual.ShouldBeEmpty();
3433
}

0 commit comments

Comments
 (0)