Skip to content

Commit 64bae82

Browse files
committed
more fixes after merge
1 parent baef331 commit 64bae82

File tree

9 files changed

+68
-40
lines changed

9 files changed

+68
-40
lines changed

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using AngleSharp.Dom;
4+
using AngleSharp.Diffing.Strategies.NodeStrategies;
45
using Shouldly;
56
using Xunit;
67

@@ -412,14 +413,7 @@ private static Func<DiffContext, SourceCollection, SourceCollection, IEnumerable
412413
private static IEnumerable<Comparison> OneToOneNodeListMatcher(
413414
DiffContext context,
414415
SourceCollection controlNodes,
415-
SourceCollection testNodes)
416-
{
417-
var matchLength = Math.Min(controlNodes.Count, testNodes.Count);
418-
for (int i = 0; i < matchLength; i++)
419-
{
420-
yield return new Comparison(controlNodes[i], testNodes[i]);
421-
}
422-
}
416+
SourceCollection testNodes) => OneToOneNodeMatcher.Match(context, controlNodes, testNodes);
423417

424418
#endregion
425419

src/AngleSharp.Diffing.Tests/DiffBuilderTest.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ public void Test6()
5151
diffs.ShouldNotBeEmpty();
5252
}
5353

54-
[Fact(DisplayName = "Test")]
55-
public void MyTestMethod()
56-
{
57-
var control = "\r\n <h1>Hello world</h1>\r\n <h1>Hello world</h1>\r\n ";
58-
var test = "\r\n <h1>Hello world</h1>\r\n <h1>Hello world</h1>\r\n ";
54+
//[Fact(DisplayName = "Calling Build() with DefaultOptions() returns expected diffs")]
55+
//public void Test7()
56+
//{
57+
// var control = "<h1>Hello World</h1>";
58+
// var test = "<h1>Hello world</h1>";
5959

60-
var diffs = new DiffBuilder(DefaultStrategy)
61-
.Compare(control)
62-
.WithTest(test)
63-
.Build();
60+
// var diffs = new DiffBuilder(DefaultStrategy)
61+
// .Compare(control)
62+
// .WithTest(test)
63+
// .Build();
6464

65-
diffs.ShouldBeEmpty();
66-
}
65+
// diffs.ShouldNotBeEmpty();
66+
//}
6767
}
6868
}

src/AngleSharp.Diffing.Tests/Strategies/NodeStrategies/ForwardSearchingNodeMatcherTest.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System.Linq;
2+
using AngleSharp.Dom;
23
using AngleSharp.Diffing.Core;
34
using Shouldly;
45
using Xunit;
@@ -28,6 +29,21 @@ public void Test001(string controlHtml, string testHtml)
2829
actual.ShouldAllBe((c, idx) => c.Control == controls[idx] && c.Test == tests[idx]);
2930
}
3031

32+
[Theory(DisplayName = "The matcher matches two nodes with the same node name")]
33+
[InlineData("asdf<h1>Hello world</h1>asdf<h1>Hello world</h1>", "asdf<h1>Hello world</h1>asdf<h1>Hello world</h1>")]
34+
public void Test0011(string controlHtml, string testHtml)
35+
{
36+
var controls = ToSourceCollection(controlHtml, ComparisonSourceType.Control);
37+
var tests = ToSourceCollection(testHtml, ComparisonSourceType.Test);
38+
controls.Remove((in ComparisonSource x) => x.Node is IElement ? FilterDecision.Keep : FilterDecision.Exclude);
39+
tests.Remove((in ComparisonSource x) => x.Node is IElement ? FilterDecision.Keep : FilterDecision.Exclude);
40+
41+
var actual = ForwardSearchingNodeMatcher.Match(_context, controls, tests).ToList();
42+
43+
actual.Count.ShouldBe(2);
44+
actual.ShouldAllBe(c => c.Control == controls[c.Control.Index] && c.Test == tests[c.Test.Index]);
45+
}
46+
3147
[Theory(DisplayName = "The matcher does not matches two nodes with the different node names")]
3248
[InlineData("textnode", "<!--comment-->")]
3349
[InlineData("textnode", "<p></p>")]

src/AngleSharp.Diffing.sln

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngleSharp.Diffing", "Angle
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AngleSharp.DiffingTests", "AngleSharp.Diffing.Tests\AngleSharp.DiffingTests.csproj", "{18203D98-66B4-4736-B79A-3B7D02EFA9E8}"
99
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E8E3C8B4-92C3-4DB7-B920-D28651E24A57}"
11+
ProjectSection(SolutionItems) = preProject
12+
AngleSharp.Diffing.nuspec = AngleSharp.Diffing.nuspec
13+
Directory.Build.props = Directory.Build.props
14+
EndProjectSection
15+
EndProject
1016
Global
1117
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1218
Debug|Any CPU = Debug|Any CPU

src/AngleSharp.Diffing/Core/HtmlDifferenceEngine.cs

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ private IEnumerable<Comparison> MatchNodes(DiffContext context, SourceCollection
7676

7777
void UpdateMatchedTracking(in Comparison comparison)
7878
{
79+
controls.MarkAsMatched(comparison.Control);
80+
tests.MarkAsMatched(comparison.Test);
7981
context.MissingSources.Remove(comparison.Control);
8082
context.UnexpectedSources.Remove(comparison.Test);
8183
}

src/AngleSharp.Diffing/Core/SourceCollection.cs

+25-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class SourceCollection : IEnumerable<ComparisonSource>
1111
{
1212
private const int SOURCE_REMOVED = -1;
1313
private const int SOURCE_UNMATCHED = 0;
14+
private const int SOURCE_MATCHED = 1;
1415

1516
private readonly int[] _status;
1617
private ComparisonSource[] _sources;
@@ -21,7 +22,12 @@ public class SourceCollection : IEnumerable<ComparisonSource>
2122

2223
public ComparisonSource this[int index]
2324
{
24-
get => _sources[index];
25+
get
26+
{
27+
if (_status[_sources[index].Index] == SOURCE_REMOVED)
28+
throw new InvalidOperationException("The source at the specified index has been removed.");
29+
return _sources[index];
30+
}
2531
}
2632

2733
public SourceCollection(ComparisonSourceType sourceType, IEnumerable<ComparisonSource> sources)
@@ -49,21 +55,37 @@ public IEnumerator<ComparisonSource> GetEnumerator()
4955
{
5056
for (int i = 0; i < _sources.Length; i++)
5157
{
52-
yield return _sources[i];
58+
if (_status[_sources[i].Index] != SOURCE_REMOVED)
59+
{
60+
yield return _sources[i];
61+
}
5362
}
5463
yield break;
5564
}
5665

5766
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
5867

68+
/// <summary>
69+
/// Gets all the sources originally in the collection, even those removed and those marked as matched.
70+
/// </summary>
71+
public IEnumerable<ComparisonSource> GetAllSources() => _sources;
72+
73+
/// <summary>
74+
/// Mark a source as matched. After it has been marked, it will not be returned by <see cref="GetUnmatched(int)"/>.
75+
/// </summary>
5976
public void MarkAsMatched(in ComparisonSource source)
6077
{
6178
if (_status[source.Index] == SOURCE_REMOVED)
6279
throw new InvalidOperationException("A removed source cannot be marked as matched. The source is not supposed to be part of the comparison.");
6380

64-
_status[source.Index]++;
81+
_status[source.Index] = SOURCE_MATCHED;
6582
}
6683

84+
/// <summary>
85+
/// Apply a filter predicate to the collection. All matched sources will not be returned
86+
/// by <see cref="GetUnmatched(int)"/> or by <see cref="GetEnumerator"/>.
87+
/// </summary>
88+
/// <param name="predicate"></param>
6789
public void Remove(SourceCollectionRemovePredicate predicate)
6890
{
6991
for (int i = 0; i < _sources.Length; i++)
@@ -75,18 +97,6 @@ public void Remove(SourceCollectionRemovePredicate predicate)
7597
Count--;
7698
}
7799
}
78-
var oldSources = _sources;
79-
_sources = Count == 0 ? Array.Empty<ComparisonSource>() : new ComparisonSource[Count];
80-
if (Count > 0)
81-
{
82-
for (int i = 0, j = 0; i < oldSources.Length; i++)
83-
{
84-
if (_status[i] != SOURCE_REMOVED)
85-
{
86-
_sources[j++] = oldSources[i];
87-
}
88-
}
89-
}
90100
}
91101

92102
private void EnsureSourcesAreInCorrectOrder()

src/AngleSharp.Diffing/Strategies/NodeStrategies/NodeComparer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using AngleSharp.Diffing.Core;
1+
using AngleSharp.Diffing.Core;
22

33
namespace AngleSharp.Diffing.Strategies.NodeStrategies
44
{

src/AngleSharp.Diffing/Strategies/TextNodeStrategies/DiffingStrategyPipelineBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static partial class DiffingStrategyPipelineBuilderExtensions
1010
public static IDiffingStrategyPipelineBuilder WithTextComparer(this IDiffingStrategyPipelineBuilder builder, WhitespaceOption whitespaceOption, bool ignoreCase)
1111
{
1212
builder.WithFilter(new TextNodeFilter(whitespaceOption).Filter, isSpecializedFilter: true);
13-
builder.WithComparer(new TextNodeComparer(whitespaceOption, ignoreCase).Compare, isSpecializedComparer: false);
13+
builder.WithComparer(new TextNodeComparer(whitespaceOption, ignoreCase).Compare, isSpecializedComparer: true);
1414
return builder;
1515
}
1616

src/AngleSharp.Diffing/Strategies/TextNodeStrategies/TextNodeComparer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Text.RegularExpressions;
33
using AngleSharp.Dom;
44
using AngleSharp.Diffing.Core;
@@ -44,7 +44,7 @@ private CompareResult Compare(IText controlTextNode, IText testTextNode)
4444
if (option == WhitespaceOption.Normalize)
4545
{
4646
controlText = WhitespaceReplace.Replace(controlText.Trim(), " ");
47-
testText = WhitespaceReplace.Replace(controlText.Trim(), " ");
47+
testText = WhitespaceReplace.Replace(testText.Trim(), " ");
4848
}
4949

5050
var isRegexCompare = GetIsRegexComparison(controlTextNode);

0 commit comments

Comments
 (0)