|
| 1 | +using Egil.AngleSharp.Diffing.Core; |
| 2 | +using Shouldly; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Egil.AngleSharp.Diffing.Strategies.AttributeStrategies |
| 8 | +{ |
| 9 | + public class BooleanAttributeComparerTest : DiffingTestBase |
| 10 | + { |
| 11 | + public static IEnumerable<object[]> BooleanAttributes = BooleanAttributeComparer.BooleanAttributes.Select(x => new string[] { x }).ToArray(); |
| 12 | + |
| 13 | + [Fact(DisplayName = "When attribute names are not the same comparer returns different")] |
| 14 | + public void Test001() |
| 15 | + { |
| 16 | + var sut = new BooleanAttributeComparer(BooleanAttributeComparision.Strict); |
| 17 | + var comparison = ToAttributeComparison("<b foo>", "foo", "<b bar>", "bar"); |
| 18 | + |
| 19 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Different); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact(DisplayName = "When attribute name is not an boolean attribute, its current result is returned")] |
| 23 | + public void Test002() |
| 24 | + { |
| 25 | + var sut = new BooleanAttributeComparer(BooleanAttributeComparision.Strict); |
| 26 | + var comparison = ToAttributeComparison(@"<b class="""">", "class", @"<b class="""">", "class"); |
| 27 | + |
| 28 | + sut.Compare(comparison, CompareResult.Different).ShouldBe(CompareResult.Different); |
| 29 | + } |
| 30 | + |
| 31 | + [Theory(DisplayName = "When attributes is boolean and mode is strict, " + |
| 32 | + "the attribute is considered truthy if conforms to the html5 spec and" + |
| 33 | + "the result is Same if both control and test attribute are truthy")] |
| 34 | + [MemberData(nameof(BooleanAttributes))] |
| 35 | + public void Test003(string attrName) |
| 36 | + { |
| 37 | + var sut = new BooleanAttributeComparer(BooleanAttributeComparision.Strict); |
| 38 | + var c1 = ToAttributeComparison($@"<b {attrName}="""">", attrName, $@"<b {attrName}=""{attrName}"">", attrName); |
| 39 | + var c2 = ToAttributeComparison($@"<b {attrName}=""{attrName}"">", attrName, $@"<b {attrName}="""">", attrName); |
| 40 | + var c3 = ToAttributeComparison($@"<b {attrName}>", attrName, $@"<b {attrName}="""">", attrName); |
| 41 | + var c4 = ToAttributeComparison($@"<b {attrName}>", attrName, $@"<b {attrName}=""{attrName}"">", attrName); |
| 42 | + var c5 = ToAttributeComparison($@"<b {attrName}="""">", attrName, $@"<b {attrName}>", attrName); |
| 43 | + var c6 = ToAttributeComparison($@"<b {attrName}=""{attrName}"">", attrName, $@"<b {attrName}>", attrName); |
| 44 | + |
| 45 | + sut.Compare(c1, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 46 | + sut.Compare(c2, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 47 | + sut.Compare(c3, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 48 | + sut.Compare(c4, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 49 | + sut.Compare(c5, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 50 | + sut.Compare(c6, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 51 | + } |
| 52 | + |
| 53 | + [Theory(DisplayName = "When attributes is boolean and mode is loose, the presence of " + |
| 54 | + "two attributes with the same name returns compare result Same, no matter what their value is")] |
| 55 | + [MemberData(nameof(BooleanAttributes))] |
| 56 | + public void Test004(string attrName) |
| 57 | + { |
| 58 | + var sut = new BooleanAttributeComparer(BooleanAttributeComparision.Loose); |
| 59 | + var c1 = ToAttributeComparison($@"<b {attrName}=""foo"">", attrName, $@"<b {attrName}=""bar"">", attrName); |
| 60 | + var c2 = ToAttributeComparison($@"<b {attrName}=""true"">", attrName, $@"<b {attrName}=""true"">", attrName); |
| 61 | + var c3 = ToAttributeComparison($@"<b {attrName}=""true"">", attrName, $@"<b {attrName}=""false"">", attrName); |
| 62 | + |
| 63 | + |
| 64 | + sut.Compare(c1, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 65 | + sut.Compare(c2, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 66 | + sut.Compare(c3, CompareResult.Different).ShouldBe(CompareResult.Same); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments