|
| 1 | +using Egil.AngleSharp.Diffing.Core; |
| 2 | +using Shouldly; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace Egil.AngleSharp.Diffing.Strategies.TextNodeStrategies |
| 6 | +{ |
| 7 | + public class TextNodeComparerTest : TextnodeStrategyTestBase |
| 8 | + { |
| 9 | + [Theory(DisplayName = "When option is Preserve or RemoveWhitespaceNodes, comparer does not run nor change the current decision")] |
| 10 | + [InlineData(WhitespaceOption.Preserve)] |
| 11 | + [InlineData(WhitespaceOption.RemoveWhitespaceNodes)] |
| 12 | + public void Test5(WhitespaceOption whitespaceOption) |
| 13 | + { |
| 14 | + var comparison = new Comparison(ToComparisonSource("hello world", ComparisonSourceType.Control), ToComparisonSource(" hello world ", ComparisonSourceType.Test)); |
| 15 | + var sut = new TextNodeComparer(whitespaceOption); |
| 16 | + |
| 17 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Different); |
| 18 | + sut.Compare(comparison, CompareResult.DifferentAndBreak).ShouldBe(CompareResult.DifferentAndBreak); |
| 19 | + sut.Compare(comparison, CompareResult.Same).ShouldBe(CompareResult.Same); |
| 20 | + sut.Compare(comparison, CompareResult.SameAndBreak).ShouldBe(CompareResult.SameAndBreak); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact(DisplayName = "When option is Normalize and current decision is Same or SameAndBreak, compare uses the current decision")] |
| 24 | + public void Test55() |
| 25 | + { |
| 26 | + var comparison = new Comparison(); |
| 27 | + var sut = new TextNodeComparer(WhitespaceOption.Normalize); |
| 28 | + sut.Compare(comparison, CompareResult.Same).ShouldBe(CompareResult.Same); |
| 29 | + sut.Compare(comparison, CompareResult.SameAndBreak).ShouldBe(CompareResult.SameAndBreak); |
| 30 | + } |
| 31 | + |
| 32 | + [Theory(DisplayName = "When option is Normalize, any whitespace before and after a text node is removed before comparison")] |
| 33 | + [MemberData(nameof(WhitespaceCharStrings))] |
| 34 | + public void Test7(string whitespace) |
| 35 | + { |
| 36 | + var sut = new TextNodeComparer(WhitespaceOption.Normalize); |
| 37 | + var normalText = "text"; |
| 38 | + var whitespaceText = $"{whitespace}text{whitespace}"; |
| 39 | + var c1 = new Comparison(ToComparisonSource(normalText, ComparisonSourceType.Control), ToComparisonSource(normalText, ComparisonSourceType.Test)); |
| 40 | + var c2 = new Comparison(ToComparisonSource(normalText, ComparisonSourceType.Control), ToComparisonSource(whitespaceText, ComparisonSourceType.Test)); |
| 41 | + var c3 = new Comparison(ToComparisonSource(whitespaceText, ComparisonSourceType.Control), ToComparisonSource(normalText, ComparisonSourceType.Test)); |
| 42 | + var c4 = new Comparison(ToComparisonSource(whitespaceText, ComparisonSourceType.Control), ToComparisonSource(whitespaceText, ComparisonSourceType.Test)); |
| 43 | + |
| 44 | + sut.Compare(c1, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 45 | + sut.Compare(c2, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 46 | + sut.Compare(c3, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 47 | + sut.Compare(c4, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 48 | + } |
| 49 | + |
| 50 | + [Theory(DisplayName = "When option is Normalize, any consecutive whitespace characters are collapsed into one before comparison")] |
| 51 | + [MemberData(nameof(WhitespaceCharStrings))] |
| 52 | + public void Test9(string whitespace) |
| 53 | + { |
| 54 | + var sut = new TextNodeComparer(WhitespaceOption.Normalize); |
| 55 | + var normalText = "hello world"; |
| 56 | + var whitespaceText = $"{whitespace}hello{whitespace}{whitespace}world{whitespace}"; |
| 57 | + var c1 = new Comparison(ToComparisonSource(normalText, ComparisonSourceType.Control), ToComparisonSource(normalText, ComparisonSourceType.Test)); |
| 58 | + var c2 = new Comparison(ToComparisonSource(normalText, ComparisonSourceType.Control), ToComparisonSource(whitespaceText, ComparisonSourceType.Test)); |
| 59 | + var c3 = new Comparison(ToComparisonSource(whitespaceText, ComparisonSourceType.Control), ToComparisonSource(normalText, ComparisonSourceType.Test)); |
| 60 | + var c4 = new Comparison(ToComparisonSource(whitespaceText, ComparisonSourceType.Control), ToComparisonSource(whitespaceText, ComparisonSourceType.Test)); |
| 61 | + |
| 62 | + sut.Compare(c1, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 63 | + sut.Compare(c2, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 64 | + sut.Compare(c3, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 65 | + sut.Compare(c4, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 66 | + } |
| 67 | + |
| 68 | + // When a parent node has overridden the global whitespace option, that overridden option is used |
| 69 | + // When whitespace option is Preserve or RemoveWhitespaceNodes, a string ordinal comparison is performed |
| 70 | + // When whitespace option is Preserve or RemoveWhitespaceNodes and IgnoreCase is true, a string ordinal ignore case comparison is performed |
| 71 | + // When IgnoreCase is true, a case insensitve comparison is performed |
| 72 | + // When the parent element is <pre>, the is implicitly set to Preserve, unless explicitly overridden on the element |
| 73 | + // When diff:regex attribute is found on the containing element, the control text is expected to a regex and that used when comparing to the test text node. |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | + |
0 commit comments