|
| 1 | +using Egil.AngleSharp.Diffing.Core; |
| 2 | +using Shouldly; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Egil.AngleSharp.Diffing.Strategies.AttributeStrategies |
| 11 | +{ |
| 12 | + |
| 13 | + public class AttributeComparerTest : DiffingTestBase |
| 14 | + { |
| 15 | + // Name comparer (attr) |
| 16 | + [Fact(DisplayName = "When compare is called with a current decision of Same or SameAndBreak, the current decision is returned")] |
| 17 | + public void Test001() |
| 18 | + { |
| 19 | + var sut = new AttributeComparer(); |
| 20 | + var comparison = ToAttributeComparison(@"<b foo>", "foo", |
| 21 | + "<b bar>", "bar"); |
| 22 | + |
| 23 | + sut.Compare(comparison, CompareResult.Same).ShouldBe(CompareResult.Same); |
| 24 | + sut.Compare(comparison, CompareResult.SameAndBreak).ShouldBe(CompareResult.SameAndBreak); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact(DisplayName = "When two attributes has the same name and no value, the compare result is Same")] |
| 28 | + public void Test002() |
| 29 | + { |
| 30 | + var sut = new AttributeComparer(); |
| 31 | + var comparison = ToAttributeComparison(@"<b foo>", "foo", |
| 32 | + "<b foo>", "foo"); |
| 33 | + |
| 34 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact(DisplayName = "When two attributes does not have the same name, the compare result is Different")] |
| 38 | + public void Test003() |
| 39 | + { |
| 40 | + var sut = new AttributeComparer(); |
| 41 | + var comparison = ToAttributeComparison(@"<b foo>", "foo", |
| 42 | + "<b bar>", "bar"); |
| 43 | + |
| 44 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Different); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact(DisplayName = "When two attribute values are the same, the compare result is Same")] |
| 48 | + public void Test004() |
| 49 | + { |
| 50 | + var sut = new AttributeComparer(); |
| 51 | + var comparison = ToAttributeComparison(@"<b foo=""bar"">", "foo", |
| 52 | + @"<b foo=""bar"">", "foo"); |
| 53 | + |
| 54 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact(DisplayName = "When two attribute values are different, the compare result is Different")] |
| 58 | + public void Test005() |
| 59 | + { |
| 60 | + var sut = new AttributeComparer(); |
| 61 | + var comparison = ToAttributeComparison(@"<b foo=""bar"">", "foo", |
| 62 | + @"<b foo=""baz"">", "foo"); |
| 63 | + |
| 64 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Different); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact(DisplayName = "When the control attribute is postfixed with :ignoreCase, " + |
| 68 | + "a case insensitive comparison between control and test attributes is performed")] |
| 69 | + public void Test006() |
| 70 | + { |
| 71 | + var sut = new AttributeComparer(); |
| 72 | + var comparison = ToAttributeComparison(@"<b foo:ignoreCase=""BAR"">", "foo:ignorecase", |
| 73 | + @"<b foo=""bar"">", "foo"); |
| 74 | + |
| 75 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact(DisplayName = "When the control attribute is postfixed with :regex, " + |
| 79 | + "the control attributes value is assumed to be a regular expression and " + |
| 80 | + "that is used to match against the test attributes value")] |
| 81 | + public void Test007() |
| 82 | + { |
| 83 | + var sut = new AttributeComparer(); |
| 84 | + var comparison = ToAttributeComparison(@"<b foo:regex=""foobar-\d{4}"">", "foo:regex", |
| 85 | + @"<b foo=""foobar-2000"">", "foo"); |
| 86 | + |
| 87 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 88 | + } |
| 89 | + |
| 90 | + [Theory(DisplayName = "When the control attribute is postfixed with :regex:ignoreCase " + |
| 91 | + "or :ignoreCase:regex, the control attributes value is assumed " + |
| 92 | + "to be a regular expression and that is used to do a case insensitive " + |
| 93 | + "match against the test attributes value")] |
| 94 | + [InlineData(":regex:ignorecase")] |
| 95 | + [InlineData(":ignorecase:regex")] |
| 96 | + public void Test008(string attrNamePostfix) |
| 97 | + { |
| 98 | + var sut = new AttributeComparer(); |
| 99 | + var controlAttrName = $"foo{attrNamePostfix}"; |
| 100 | + var comparison = ToAttributeComparison($@"<b {controlAttrName}=""foobar-\d{{4}}"">", controlAttrName, |
| 101 | + @"<b foo=""FOOBAR-2000"">", "foo"); |
| 102 | + |
| 103 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 104 | + } |
| 105 | + |
| 106 | + //[Theory(DisplayName = "When a control attribute is a boolean attribute, its presents represent " + |
| 107 | + // "a truthy value, and its absence a falsy value, independent of the actual value")] |
| 108 | + //[InlineData(@"<p required>", @"<p required=""required"">")] |
| 109 | + //public void Test0009(string controlHtml, string testHtml) |
| 110 | + //{ |
| 111 | + // var sut = new AttributeComparer(); |
| 112 | + // var comparison = ToAttributeComparison(controlHtml, "required", testHtml, "required"); |
| 113 | + |
| 114 | + // sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 115 | + //} |
| 116 | + |
| 117 | + // Boolean-attribute comparer (attr) |
| 118 | + } |
| 119 | +} |
0 commit comments