-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDiffBuilderTest.cs
More file actions
132 lines (114 loc) · 5.21 KB
/
DiffBuilderTest.cs
File metadata and controls
132 lines (114 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
namespace AngleSharp.Diffing;
public class DiffBuilderTest
{
[Fact(DisplayName = "Control and test html are set correctly")]
public void Test001()
{
var control = "<p>control</p>";
var test = "<p>test</p>";
var sut = DiffBuilder
.Compare(control)
.WithTest(test);
sut.Control.ShouldBe(control);
sut.Test.ShouldBe(test);
}
[Fact(DisplayName = "Builder throws if null is passed to control and test")]
public void Test002()
{
Should.Throw<ArgumentNullException>(() => DiffBuilder.Compare(null!)).ParamName.ShouldBe("value");
Should.Throw<ArgumentNullException>(() => DiffBuilder.Compare("").WithTest(null!)).ParamName.ShouldBe("value");
}
[Fact(DisplayName = "Calling Build() with DefaultOptions() returns expected diffs")]
public void Test003()
{
var control = @"<p attr=""foo"" missing>hello <em>world</em></p>";
var test = @"<p attr=""bar"" unexpected>world says <strong>hello</strong></p>";
var diffs = DiffBuilder
.Compare(control)
.WithTest(test)
.Build()
.ToList();
diffs.Count.ShouldBe(6);
diffs.SingleOrDefault(x => x is AttrDiff).ShouldNotBeNull();
diffs.SingleOrDefault(x => x is MissingAttrDiff).ShouldNotBeNull();
diffs.SingleOrDefault(x => x is UnexpectedAttrDiff).ShouldNotBeNull();
diffs.SingleOrDefault(x => x is NodeDiff).ShouldNotBeNull();
diffs.SingleOrDefault(x => x is MissingNodeDiff).ShouldNotBeNull();
diffs.SingleOrDefault(x => x is UnexpectedNodeDiff).ShouldNotBeNull();
}
[Fact(DisplayName = "Setting options works")]
public void Test004()
{
var control = "<p foo>hello world</p>";
var test = "<p foo>hello world</p>";
var nodeFilterCalled = false;
var attrFilterCalled = false;
var nodeMatcherCalled = false;
var attrMatcherCalled = false;
var nodeComparerCalled = false;
var attrComparerCalled = false;
var diffs = DiffBuilder
.Compare(control)
.WithTest(test)
.WithOptions(options => options
.AddDefaultOptions()
.AddFilter((in ComparisonSource source, FilterDecision currentDecision) => { nodeFilterCalled = true; return currentDecision; })
.AddFilter((in AttributeComparisonSource source, FilterDecision currentDecision) => { attrFilterCalled = true; return currentDecision; })
.AddMatcher((ctx, ctrlSrc, testSrc) => { nodeMatcherCalled = true; return Array.Empty<Comparison>(); })
.AddMatcher((ctx, ctrlSrc, testSrc) => { attrMatcherCalled = true; return Array.Empty<AttributeComparison>(); })
.AddComparer((in Comparison comparison, CompareResult currentDecision) => { nodeComparerCalled = true; return currentDecision; })
.AddComparer((in AttributeComparison comparison, CompareResult currentDecision) => { attrComparerCalled = true; return currentDecision; })
)
.Build()
.ToList();
nodeFilterCalled.ShouldBeTrue();
attrFilterCalled.ShouldBeTrue();
nodeMatcherCalled.ShouldBeTrue();
attrMatcherCalled.ShouldBeTrue();
nodeComparerCalled.ShouldBeTrue();
attrComparerCalled.ShouldBeTrue();
}
[Theory(DisplayName = "When a control element has 'diff:ignoreChildren', calling Build() with DefaultOptions() returns empty diffs")]
[InlineData(@"<p diff:ignoreChildren>hello <em>world</em></p>",
@"<p>world says <strong>hello</strong></p>")]
[InlineData(@"<p diff:ignoreChildren>hello</p>",
@"<p>world says <strong>hello</strong></p>")]
[InlineData(@"<p diff:ignoreChildren>hello <em>world</em></p>",
@"<p>world says</p>")]
public void Test005(string control, string test)
{
var diffs = DiffBuilder
.Compare(control)
.WithTest(test)
.Build()
.ToList();
diffs.ShouldBeEmpty();
}
[Theory(DisplayName = "When a control element has 'diff:ignoreAttributes', calling Build() with DefaultOptions() returns empty diffs")]
[InlineData(@"<p id=""foo"" diff:ignoreAttributes></p>",
@"<p id=""bar""></p>")]
[InlineData(@"<p diff:ignoreAttributes></p>",
@"<p unexpected></p>")]
[InlineData(@"<p id=""foo"" diff:ignoreAttributes></p>",
@"<p></p>")]
public void Test006(string control, string test)
{
var diffs = DiffBuilder
.Compare(control)
.WithTest(test)
.Build()
.ToList();
diffs.ShouldBeEmpty();
}
[Theory(DisplayName = "When a control element has 'class:ignore', elements with and without class should return empty diffs")]
[InlineData("<div class=\"ian-fleming\"></div>")]
[InlineData("<div class=\"\"></div>")]
[InlineData("<div class></div>")]
[InlineData("<div></div>")]
public void Test007(string testHtml)
{
var controlHtml = "<div class:ignore></div>";
var diffs = DiffBuilder.Compare(controlHtml).WithTest(testHtml).Build();
Assert.Empty(diffs);
}
}