1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Text ;
5
+ using System . Threading . Tasks ;
6
+ using AngleSharp . Dom ;
7
+ using Egil . AngleSharp . Diffing . Core ;
8
+ using Shouldly ;
9
+ using Xunit ;
10
+
11
+ namespace Egil . AngleSharp . Diffing
12
+ {
13
+ public class DiffingStrategyPipelineTest : DiffingTestBase
14
+ {
15
+ [ Fact ( DisplayName = "Wen zero filter strategies have been added, true is returned" ) ]
16
+ public void Test1 ( )
17
+ {
18
+ var sut = new DiffingStrategyPipeline ( ) ;
19
+
20
+ sut . Filter ( new ComparisonSource ( ) ) . ShouldBeTrue ( ) ;
21
+ sut . Filter ( new AttributeComparisonSource ( ) ) . ShouldBeTrue ( ) ;
22
+ }
23
+
24
+ [ Theory ( DisplayName = "When one or more filter strategy is added, the last decides the outcome" ) ]
25
+ [ InlineData ( true ) ]
26
+ [ InlineData ( false ) ]
27
+ public void Test5 ( bool expected )
28
+ {
29
+ var sut = new DiffingStrategyPipeline ( ) ;
30
+
31
+ sut . AddFilter ( ( in ComparisonSource s , bool currentDecision ) => ! expected) ;
32
+ sut . AddFilter ( ( in ComparisonSource s , bool currentDecision ) => expected) ;
33
+ sut . AddFilter ( ( in AttributeComparisonSource s , bool currentDecision ) => ! expected) ;
34
+ sut . AddFilter ( ( in AttributeComparisonSource s , bool currentDecision ) => expected) ;
35
+
36
+ sut . Filter ( new ComparisonSource ( ) ) . ShouldBe ( expected ) ;
37
+ sut . Filter ( new AttributeComparisonSource ( ) ) . ShouldBe ( expected ) ;
38
+ }
39
+
40
+ #pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
41
+ #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
42
+ [ Fact ( DisplayName = "When no matcher strategy have been added, none comparisons are returned" ) ]
43
+ public void Test2 ( )
44
+ {
45
+ var sut = new DiffingStrategyPipeline ( ) ;
46
+
47
+ sut . Match ( null , null , ( SourceCollection ) null ) . ShouldBeEmpty ( ) ;
48
+ sut . Match ( null , null , ( SourceMap ) null ) . ShouldBeEmpty ( ) ;
49
+ }
50
+ #pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
51
+ #pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
52
+
53
+ [ Fact ( DisplayName = "Node Matchers are allowed to match in the order they are added in" ) ]
54
+ public void Test6 ( )
55
+ {
56
+ var sources = ToComparisonSourceList ( "<p></p><span></span>" ) . ToList ( ) ;
57
+ var context = new DiffContext ( ( IElement ) sources [ 0 ] . Node , ( IElement ) sources [ 0 ] . Node ) ;
58
+ var sourceColllection = new SourceCollection ( ComparisonSourceType . Control , sources ) ;
59
+ var sut = new DiffingStrategyPipeline ( ) ;
60
+ sut . AddMatcher ( ( ctx , s , t ) => new [ ] { new Comparison ( s [ 0 ] , t [ 0 ] ) } ) ;
61
+ sut . AddMatcher ( ( ctx , s , t ) => new [ ] { new Comparison ( s [ 1 ] , t [ 1 ] ) } ) ;
62
+
63
+ var result = sut . Match ( context , sourceColllection , sourceColllection ) . ToList ( ) ;
64
+
65
+ result [ 0 ] . Control . ShouldBe ( sources [ 0 ] ) ;
66
+ result [ 1 ] . Control . ShouldBe ( sources [ 1 ] ) ;
67
+ }
68
+
69
+ [ Fact ( DisplayName = "Attributes Matchers are allowed to match in the order they are added in" ) ]
70
+ public void Test7 ( )
71
+ {
72
+ var source = ToComparisonSource ( @"<p foo=""bar"" baz=""bum""></p>" ) ;
73
+ var context = new DiffContext ( ( IElement ) source . Node , ( IElement ) source . Node ) ;
74
+ var sourceMap = new SourceMap ( source ) ;
75
+ var sut = new DiffingStrategyPipeline ( ) ;
76
+ sut . AddMatcher ( ( ctx , s , t ) => new [ ] { new AttributeComparison ( s [ "foo" ] , t [ "foo" ] ) } ) ;
77
+ sut . AddMatcher ( ( ctx , s , t ) => new [ ] { new AttributeComparison ( s [ "baz" ] , t [ "baz" ] ) } ) ;
78
+
79
+ var result = sut . Match ( context , sourceMap , sourceMap ) . ToList ( ) ;
80
+
81
+ result [ 0 ] . Control . ShouldBe ( sourceMap [ "foo" ] ) ;
82
+ result [ 1 ] . Control . ShouldBe ( sourceMap [ "baz" ] ) ;
83
+ }
84
+
85
+ [ Fact ( DisplayName = "When no compare strategy have been added, DifferentAndBreak is returned for node comparison and Different for attributes" ) ]
86
+ public void Test3 ( )
87
+ {
88
+ var sut = new DiffingStrategyPipeline ( ) ;
89
+
90
+ sut . Compare ( new Comparison ( ) ) . ShouldBe ( CompareResult . DifferentAndBreak ) ;
91
+ sut . Compare ( new AttributeComparison ( ) ) . ShouldBe ( CompareResult . Different ) ;
92
+ }
93
+
94
+ [ Theory ( DisplayName = "When multiple comparers are added, the last decides the outcome" ) ]
95
+ [ InlineData ( CompareResult . Different , CompareResult . Same ) ]
96
+ [ InlineData ( CompareResult . Same , CompareResult . Different ) ]
97
+ [ InlineData ( CompareResult . Same , CompareResult . SameAndBreak ) ]
98
+ [ InlineData ( CompareResult . Different , CompareResult . DifferentAndBreak ) ]
99
+
100
+ public void Test8 ( CompareResult first , CompareResult final )
101
+ {
102
+ var sut = new DiffingStrategyPipeline ( ) ;
103
+
104
+ sut . AddComparer ( ( in Comparison c , CompareResult current ) => first) ;
105
+ sut . AddComparer ( ( in Comparison c , CompareResult current ) => final) ;
106
+ sut . AddComparer ( ( in AttributeComparison c , CompareResult current ) => first) ;
107
+ sut . AddComparer ( ( in AttributeComparison c , CompareResult current ) => final) ;
108
+
109
+ sut . Compare ( new Comparison ( ) ) . ShouldBe ( final ) ;
110
+ sut . Compare ( new AttributeComparison ( ) ) . ShouldBe ( final ) ;
111
+ }
112
+
113
+ }
114
+ }
0 commit comments