Skip to content

Commit fbab1a7

Browse files
committed
Added extra unit tests
1 parent dc6b674 commit fbab1a7

36 files changed

+564
-103
lines changed

src/Core/AttributeComparison.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ public AttributeComparison(in AttributeComparisonSource control, in AttributeCom
1515
Test = test;
1616
}
1717

18-
public (IElement ControlElement, IElement TestElement) GetNodesAsElements()
18+
public (IElement ControlElement, IElement TestElement) GetAttributeElements()
1919
=> ((IElement)Control.ElementSource.Node, (IElement)Test.ElementSource.Node);
2020

2121
#region Equals and HashCode
2222
public bool Equals(AttributeComparison other) => Control == other.Control && Test == other.Test;
2323
public override bool Equals(object obj) => obj is AttributeComparison other && Equals(other);
2424
public override int GetHashCode() => (Control, Test).GetHashCode();
2525
public static bool operator ==(AttributeComparison left, AttributeComparison right) => left.Equals(right);
26-
public static bool operator !=(AttributeComparison left, AttributeComparison right) => !(left == right);
26+
public static bool operator !=(AttributeComparison left, AttributeComparison right) => !left.Equals(right);
2727
#endregion
2828
}
2929
}

src/Core/AttributeComparisonSource.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
1+
using AngleSharp.Dom;
2+
using Egil.AngleSharp.Diffing.Extensions;
3+
using System;
24
using System.Diagnostics.CodeAnalysis;
3-
using AngleSharp.Dom;
45

56
namespace Egil.AngleSharp.Diffing.Core
67
{
@@ -15,9 +16,11 @@ namespace Egil.AngleSharp.Diffing.Core
1516
public ComparisonSourceType SourceType { get; }
1617

1718
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Path should be in lower case")]
18-
public AttributeComparisonSource(IAttr attribute, in ComparisonSource elementSource)
19+
public AttributeComparisonSource(string attributeName, in ComparisonSource elementSource)
1920
{
20-
if (attribute is null) throw new ArgumentNullException(nameof(attribute));
21+
if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException(nameof(attributeName));
22+
if (!elementSource.Node.TryGetAttr(attributeName, out var attribute))
23+
throw new ArgumentException("The comparison source does not contain an element or the specified attribute is missing on the element.", nameof(elementSource));
2124

2225
Attribute = attribute;
2326
ElementSource = elementSource;
@@ -30,7 +33,7 @@ public AttributeComparisonSource(IAttr attribute, in ComparisonSource elementSou
3033
public override int GetHashCode() => (Attribute, ElementSource).GetHashCode();
3134
public override bool Equals(object obj) => obj is AttributeComparisonSource other && Equals(other);
3235
public static bool operator ==(AttributeComparisonSource left, AttributeComparisonSource right) => left.Equals(right);
33-
public static bool operator !=(AttributeComparisonSource left, AttributeComparisonSource right) => !(left == right);
36+
public static bool operator !=(AttributeComparisonSource left, AttributeComparisonSource right) => !left.Equals(right);
3437
#endregion
3538
}
3639
}

src/Core/CompareResult.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,22 @@
33
public enum CompareResult
44
{
55
/// <summary>
6-
/// Indicates the two compared nodes or attributes are the same.
6+
/// Use when the two compared nodes or attributes are the same.
77
/// </summary>
88
Same,
99
/// <summary>
10-
/// Indicates the two compared nodes or attributes are the same AND no further comparison should happen on any child nodes or attributes.
11-
/// </summary>
12-
SameAndBreak,
13-
/// <summary>
14-
/// Indicates the two compared nodes or attributes are the different.
10+
/// Use when the two compared nodes or attributes are the different.
1511
/// </summary>
1612
Different,
1713
/// <summary>
18-
/// Indicates the two compared nodes or attributes are the different AND no further comparison should happen on any child nodes or attributes.
14+
/// Use when the comparison should be skipped and any child-nodes or attributes skipped as well.
1915
/// </summary>
20-
DifferentAndBreak
16+
Skip
2117
}
2218

2319
public static class CompareResultExtensions
2420
{
25-
public static bool IsSame(this CompareResult compareResult) => compareResult == CompareResult.Same;
26-
public static bool IsSameAndBreak(this CompareResult compareResult) => compareResult == CompareResult.SameAndBreak;
27-
public static bool IsDifferent(this CompareResult compareResult) => compareResult == CompareResult.Different;
28-
public static bool IsDifferentAndBreak(this CompareResult compareResult) => compareResult == CompareResult.DifferentAndBreak;
29-
public static bool IsDecisionFinal(this CompareResult compareResult) => !compareResult.IsDifferent();
21+
public static bool IsSameOrSkip(this CompareResult compareResult) => compareResult == CompareResult.Same || compareResult == CompareResult.Skip;
3022
}
3123
}
3224

src/Core/Comparison.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public bool TryGetNodesAsType<TNode>([NotNullWhen(true)]out TNode? controlNode,
4141
}
4242

4343
#region Equals and HashCode
44-
public bool Equals(Comparison other) => Control == other.Control && Test == other.Test;
44+
public bool Equals(Comparison other) => Control.Equals(other.Control) && Test.Equals(other.Test);
4545
public override bool Equals(object obj) => obj is Comparison other && Equals(other);
4646
public override int GetHashCode() => (Control, Test).GetHashCode();
4747
public static bool operator ==(Comparison left, Comparison right) => left.Equals(right);
48-
public static bool operator !=(Comparison left, Comparison right) => !(left == right);
48+
public static bool operator !=(Comparison left, Comparison right) => !left.Equals(right);
4949
#endregion
5050
}
5151
}

src/Core/ComparisonSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ private static string CalculateNodePath(INode node, int index)
6868
}
6969

7070
#region Equals and HashCode
71-
public bool Equals(ComparisonSource other) => Node.Equals(other.Node) && Index == other.Index && Path.Equals(other.Path, StringComparison.Ordinal) && SourceType == other.SourceType;
71+
public bool Equals(ComparisonSource other) => Node == other.Node && Index == other.Index && Path.Equals(other.Path, StringComparison.Ordinal) && SourceType == other.SourceType;
7272
public override int GetHashCode() => _hashCode;
7373
public override bool Equals(object obj) => obj is ComparisonSource other && Equals(other);
7474
public static bool operator ==(ComparisonSource left, ComparisonSource right) => left.Equals(right);
75-
public static bool operator !=(ComparisonSource left, ComparisonSource right) => !(left == right);
75+
public static bool operator !=(ComparisonSource left, ComparisonSource right) => !left.Equals(right);
7676
#endregion
7777
}
7878
}

src/Core/HtmlDifferenceEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private IEnumerable<IDiff> CompareNode(DiffContext context, in Comparison compar
101101
}
102102

103103
var compareRes = _compareStrategy.Compare(comparison);
104-
if (compareRes == CompareResult.Different || compareRes == CompareResult.DifferentAndBreak)
104+
if (compareRes == CompareResult.Different)
105105
{
106106
IDiff diff = new Diff(comparison);
107107
return new[] { diff };
@@ -115,12 +115,12 @@ private IEnumerable<IDiff> CompareElement(DiffContext context, in Comparison com
115115
var result = new List<IDiff>();
116116

117117
var compareRes = _compareStrategy.Compare(comparison);
118-
if (compareRes == CompareResult.Different || compareRes == CompareResult.DifferentAndBreak)
118+
if (compareRes == CompareResult.Different)
119119
{
120120
result.Add(new Diff(comparison));
121121
}
122122

123-
if (compareRes == CompareResult.Same || compareRes == CompareResult.Different)
123+
if (compareRes != CompareResult.Skip)
124124
{
125125
result.AddRange(CompareElementAttributes(context, comparison));
126126
result.AddRange(CompareChildNodes(context, comparison));
@@ -198,7 +198,7 @@ private IEnumerable<IDiff> CompareAttributes(IEnumerable<AttributeComparison> co
198198
foreach (var comparison in comparisons)
199199
{
200200
var compareRes = _compareStrategy.Compare(comparison);
201-
if (compareRes == CompareResult.Different || compareRes == CompareResult.DifferentAndBreak)
201+
if (compareRes == CompareResult.Different)
202202
yield return new AttrDiff(comparison);
203203
}
204204
}

src/Core/SourceMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public SourceMap(in ComparisonSource elementSource)
3535
SourceType = elementSource.SourceType;
3636
foreach (var attr in element.Attributes)
3737
{
38-
_sources.Add(attr.Name, new AttributeComparisonSource(attr, elementSource));
38+
_sources.Add(attr.Name, new AttributeComparisonSource(attr.Name, elementSource));
3939
}
4040
}
4141
else

src/DiffBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public DiffBuilder WithFilter(FilterStrategy<ComparisonSource> filterStrategy)
3636

3737
public DiffBuilder WithFilter(FilterStrategy<AttributeComparisonSource> filterStrategy)
3838
{
39+
3940
return this;
4041
}
4142

src/DiffingStrategyPipeline.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ public IEnumerable<AttributeComparison> Match(DiffContext context, SourceMap con
4545
}
4646
}
4747

48-
public CompareResult Compare(in Comparison comparison)
49-
=> Compare(comparison, _nodeComparers, CompareResult.DifferentAndBreak);
50-
public CompareResult Compare(in AttributeComparison comparison)
51-
=> Compare(comparison, _attrComparers, CompareResult.Different);
48+
public CompareResult Compare(in Comparison comparison) => Compare(comparison, _nodeComparers, CompareResult.Different);
49+
public CompareResult Compare(in AttributeComparison comparison) => Compare(comparison, _attrComparers, CompareResult.Different);
5250

5351
/// <summary>
5452
/// Adds a node filter to the pipeline.

src/Extensions/NodeExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ public static bool HasAttributes(this INode node)
1111
return node is IElement element && element.Attributes.Length > 0;
1212
}
1313

14+
public static bool TryGetAttr(this INode node, string attributeName, [NotNullWhen(true)]out IAttr? attribute)
15+
{
16+
if (node is IElement element && element.HasAttribute(attributeName))
17+
{
18+
attribute = element.Attributes[attributeName];
19+
return true;
20+
}
21+
else
22+
{
23+
attribute = default;
24+
return false;
25+
}
26+
}
27+
1428
public static bool TryGetAttrValue(this INode node, string attributeName, out bool result)
1529
{
1630
#pragma warning disable CS8653 // A default expression introduces a null value for a type parameter.

0 commit comments

Comments
 (0)