@@ -103,17 +103,46 @@ public void WhenNodesAreDifferentADiffIsReturned()
103
103
var results = sut . Compare ( nodes , nodes ) . ToList ( ) ;
104
104
105
105
results . Count . ShouldBe ( 3 ) ;
106
- results [ 0 ] . ShouldBeOfType < NodeDiff > ( ) . ShouldSatisfyAllConditions (
106
+ results [ 0 ] . ShouldBeAssignableTo < NodeDiff > ( ) . ShouldSatisfyAllConditions (
107
107
diff => diff . Control . Node . NodeName . ShouldBe ( "P" ) ,
108
108
diff => diff . Result . ShouldBe ( DiffResult . Different ) ,
109
109
diff => diff . Target . ShouldBe ( DiffTarget . Element )
110
110
) ;
111
- results [ 1 ] . ShouldBeOfType < NodeDiff > ( ) . ShouldSatisfyAllConditions (
111
+ results [ 1 ] . ShouldBeAssignableTo < NodeDiff > ( ) . ShouldSatisfyAllConditions (
112
112
diff => diff . Control . Node . NodeName . ShouldBe ( "#comment" ) ,
113
113
diff => diff . Result . ShouldBe ( DiffResult . Different ) ,
114
114
diff => diff . Target . ShouldBe ( DiffTarget . Comment )
115
115
) ;
116
- results [ 2 ] . ShouldBeOfType < NodeDiff > ( ) . ShouldSatisfyAllConditions (
116
+ results [ 2 ] . ShouldBeAssignableTo < NodeDiff > ( ) . ShouldSatisfyAllConditions (
117
+ diff => diff . Control . Node . NodeName . ShouldBe ( "#text" ) ,
118
+ diff => diff . Result . ShouldBe ( DiffResult . Different ) ,
119
+ diff => diff . Target . ShouldBe ( DiffTarget . Text )
120
+ ) ;
121
+ }
122
+
123
+ [ Fact ( DisplayName = "When matched control/test nodes are different, a custom diff is returned" ) ]
124
+ public void WhenNodesAreDifferentADiffIsReturnedWithCustomDiff ( )
125
+ {
126
+ var nodes = ToNodeList ( "<p></p><!--comment-->textnode" ) ;
127
+ var sut = CreateHtmlDiffer (
128
+ nodeMatcher : OneToOneNodeListMatcher ,
129
+ nodeFilter : NoneNodeFilter ,
130
+ nodeComparer : DiffResultCustomNodeComparer ) ;
131
+
132
+ var results = sut . Compare ( nodes , nodes ) . ToList ( ) ;
133
+
134
+ results . Count . ShouldBe ( 3 ) ;
135
+ results [ 0 ] . ShouldBeOfType < CustomNodeDiff > ( ) . ShouldSatisfyAllConditions (
136
+ diff => diff . Control . Node . NodeName . ShouldBe ( "P" ) ,
137
+ diff => diff . Result . ShouldBe ( DiffResult . Different ) ,
138
+ diff => diff . Target . ShouldBe ( DiffTarget . Element )
139
+ ) ;
140
+ results [ 1 ] . ShouldBeOfType < CustomNodeDiff > ( ) . ShouldSatisfyAllConditions (
141
+ diff => diff . Control . Node . NodeName . ShouldBe ( "#comment" ) ,
142
+ diff => diff . Result . ShouldBe ( DiffResult . Different ) ,
143
+ diff => diff . Target . ShouldBe ( DiffTarget . Comment )
144
+ ) ;
145
+ results [ 2 ] . ShouldBeOfType < CustomNodeDiff > ( ) . ShouldSatisfyAllConditions (
117
146
diff => diff . Control . Node . NodeName . ShouldBe ( "#text" ) ,
118
147
diff => diff . Result . ShouldBe ( DiffResult . Different ) ,
119
148
diff => diff . Target . ShouldBe ( DiffTarget . Text )
@@ -237,6 +266,30 @@ public void WhenMatchedAttrsAreDiffAttrDiffIsReturned()
237
266
) ;
238
267
}
239
268
269
+ [ Fact ( DisplayName = "When matched control/test attributes are different, a diff is returned with custom diff" ) ]
270
+ public void WhenMatchedAttrsAreDiffAttrDiffIsReturnedWithCustomDiff ( )
271
+ {
272
+ var nodes = ToNodeList ( @"<p id=""foo""></p>" ) ;
273
+
274
+ var sut = CreateHtmlDiffer (
275
+ nodeMatcher : OneToOneNodeListMatcher ,
276
+ nodeFilter : NoneNodeFilter ,
277
+ nodeComparer : SameResultNodeComparer ,
278
+ attrMatcher : AttributeNameMatcher ,
279
+ attrFilter : NoneAttrFilter ,
280
+ attrComparer : DiffResultCustomAttrComparer ) ;
281
+
282
+ var results = sut . Compare ( nodes , nodes ) . ToList ( ) ;
283
+
284
+ results . Count . ShouldBe ( 1 ) ;
285
+ results [ 0 ] . ShouldBeOfType < CustomAttrDiff > ( ) . ShouldSatisfyAllConditions (
286
+ diff => diff . Control . Attribute . Name . ShouldBe ( "id" ) ,
287
+ diff => diff . Test . Attribute . Name . ShouldBe ( "id" ) ,
288
+ diff => diff . Result . ShouldBe ( DiffResult . Different ) ,
289
+ diff => diff . Target . ShouldBe ( DiffTarget . Attribute )
290
+ ) ;
291
+ }
292
+
240
293
[ Fact ( DisplayName = "When matched control/test attributes are the same, no diffs are returned" ) ]
241
294
public void WhenMatchedAttrsAreSameNoDiffIsReturned ( )
242
295
{
@@ -268,11 +321,11 @@ public void WhenBothTestAndControlHaveChildNodesTheseAreCompared()
268
321
var results = sut . Compare ( nodes , nodes ) . ToList ( ) ;
269
322
270
323
results . Count . ShouldBe ( 5 ) ;
271
- results [ 0 ] . ShouldBeOfType < NodeDiff > ( ) . Control . Node . NodeName . ShouldBe ( "MAIN" ) ;
272
- results [ 1 ] . ShouldBeOfType < NodeDiff > ( ) . Control . Node . NodeName . ShouldBe ( "H1" ) ;
273
- results [ 2 ] . ShouldBeOfType < NodeDiff > ( ) . Control . Node . NodeValue . ShouldBe ( "foobar" ) ;
274
- results [ 3 ] . ShouldBeOfType < NodeDiff > ( ) . Control . Node . NodeName . ShouldBe ( "P" ) ;
275
- results [ 4 ] . ShouldBeOfType < NodeDiff > ( ) . Control . Node . NodeName . ShouldBe ( "#text" ) ;
324
+ results [ 0 ] . ShouldBeAssignableTo < NodeDiff > ( ) . Control . Node . NodeName . ShouldBe ( "MAIN" ) ;
325
+ results [ 1 ] . ShouldBeAssignableTo < NodeDiff > ( ) . Control . Node . NodeName . ShouldBe ( "H1" ) ;
326
+ results [ 2 ] . ShouldBeAssignableTo < NodeDiff > ( ) . Control . Node . NodeValue . ShouldBe ( "foobar" ) ;
327
+ results [ 3 ] . ShouldBeAssignableTo < NodeDiff > ( ) . Control . Node . NodeName . ShouldBe ( "P" ) ;
328
+ results [ 4 ] . ShouldBeAssignableTo < NodeDiff > ( ) . Control . Node . NodeName . ShouldBe ( "#text" ) ;
276
329
}
277
330
278
331
[ Theory ( DisplayName = "When only one of the control or test node in a comparison has child nodes, a missing/unexpected diff is returned" ) ]
@@ -288,7 +341,7 @@ public void OnlyOnePartHasChildNodes(string control, string test, Type expectedD
288
341
var results = sut . Compare ( ToNodeList ( control ) , ToNodeList ( test ) ) . ToList ( ) ;
289
342
290
343
results . Count . ShouldBe ( 2 ) ;
291
- results [ 0 ] . ShouldBeOfType < NodeDiff > ( ) ;
344
+ results [ 0 ] . ShouldBeAssignableTo < NodeDiff > ( ) ;
292
345
results [ 1 ] . ShouldBeOfType ( expectedDiffType ) ;
293
346
}
294
347
@@ -309,8 +362,8 @@ public void ComparisonSourcesHaveCorrectType()
309
362
310
363
results . Count . ShouldBe ( 2 ) ;
311
364
312
- results [ 0 ] . ShouldBeOfType < NodeDiff > ( ) . Control . SourceType . ShouldBe ( ComparisonSourceType . Control ) ;
313
- results [ 0 ] . ShouldBeOfType < NodeDiff > ( ) . Test . SourceType . ShouldBe ( ComparisonSourceType . Test ) ;
365
+ results [ 0 ] . ShouldBeAssignableTo < NodeDiff > ( ) . Control . SourceType . ShouldBe ( ComparisonSourceType . Control ) ;
366
+ results [ 0 ] . ShouldBeAssignableTo < NodeDiff > ( ) . Test . SourceType . ShouldBe ( ComparisonSourceType . Test ) ;
314
367
results [ 1 ] . ShouldBeOfType < AttrDiff > ( ) . Control . SourceType . ShouldBe ( ComparisonSourceType . Control ) ;
315
368
results [ 1 ] . ShouldBeOfType < AttrDiff > ( ) . Test . SourceType . ShouldBe ( ComparisonSourceType . Test ) ;
316
369
}
@@ -349,14 +402,14 @@ public void Test2()
349
402
}
350
403
351
404
[ Theory ( DisplayName = "When comparer returns SkipChildren flag from an element comparison, child nodes are not compared" ) ]
352
- [ InlineData ( CompareResult . Same | CompareResult . SkipChildren ) ]
353
- [ InlineData ( CompareResult . Skip | CompareResult . SkipChildren ) ]
354
- public void Test3 ( CompareResult compareResult )
405
+ [ InlineData ( CompareDecision . Same | CompareDecision . SkipChildren ) ]
406
+ [ InlineData ( CompareDecision . Skip | CompareDecision . SkipChildren ) ]
407
+ public void Test3 ( CompareDecision decision )
355
408
{
356
409
var sut = CreateHtmlDiffer (
357
410
nodeMatcher : OneToOneNodeListMatcher ,
358
411
nodeFilter : NoneNodeFilter ,
359
- nodeComparer : c => c . Control . Node . NodeName == "P" ? compareResult : throw new Exception ( "NODE COMPARER SHOULD NOT BE CALLED ON CHILD NODES" ) ,
412
+ nodeComparer : c => c . Control . Node . NodeName == "P" ? new CompareResult ( decision ) : throw new Exception ( "NODE COMPARER SHOULD NOT BE CALLED ON CHILD NODES" ) ,
360
413
attrMatcher : AttributeNameMatcher ,
361
414
attrFilter : NoneAttrFilter ,
362
415
attrComparer : SameResultAttrComparer
@@ -368,14 +421,14 @@ public void Test3(CompareResult compareResult)
368
421
}
369
422
370
423
[ Theory ( DisplayName = "When comparer returns SkipAttributes flag from an element comparison, attributes are not compared" ) ]
371
- [ InlineData ( CompareResult . Same | CompareResult . SkipAttributes ) ]
372
- [ InlineData ( CompareResult . Skip | CompareResult . SkipAttributes ) ]
373
- public void Test4 ( CompareResult compareResult )
424
+ [ InlineData ( CompareDecision . Same | CompareDecision . SkipAttributes ) ]
425
+ [ InlineData ( CompareDecision . Skip | CompareDecision . SkipAttributes ) ]
426
+ public void Test4 ( CompareDecision decision )
374
427
{
375
428
var sut = CreateHtmlDiffer (
376
429
nodeMatcher : OneToOneNodeListMatcher ,
377
430
nodeFilter : NoneNodeFilter ,
378
- nodeComparer : c => compareResult ,
431
+ nodeComparer : c => new CompareResult ( decision ) ,
379
432
attrMatcher : AttributeNameMatcher ,
380
433
attrFilter : NoneAttrFilter ,
381
434
attrComparer : SameResultAttrComparer
@@ -411,6 +464,7 @@ private static IEnumerable<Comparison> OneToOneNodeListMatcher(
411
464
#region NodeComparers
412
465
private static CompareResult SameResultNodeComparer ( Comparison comparison ) => CompareResult . Same ;
413
466
private static CompareResult DiffResultNodeComparer ( Comparison comparison ) => CompareResult . Different ;
467
+ private static CompareResult DiffResultCustomNodeComparer ( Comparison comparison ) => CompareResult . FromDiff ( new CustomNodeDiff ( comparison ) ) ;
414
468
#endregion
415
469
416
470
#region AttributeMatchers
@@ -452,5 +506,22 @@ private static Func<AttributeComparisonSource, FilterDecision> SpecificAttrFilte
452
506
#region AttributeComparers
453
507
public static CompareResult SameResultAttrComparer ( AttributeComparison comparison ) => CompareResult . Same ;
454
508
public static CompareResult DiffResultAttrComparer ( AttributeComparison comparison ) => CompareResult . Different ;
509
+ public static CompareResult DiffResultCustomAttrComparer ( AttributeComparison comparison ) => CompareResult . FromDiff ( new CustomAttrDiff ( comparison ) ) ;
510
+ #endregion
511
+
512
+ #region CustomDiff
513
+ public record CustomNodeDiff : NodeDiff
514
+ {
515
+ public CustomNodeDiff ( in Comparison comparison ) : base ( comparison )
516
+ {
517
+ }
518
+ }
519
+
520
+ public record CustomAttrDiff : AttrDiff
521
+ {
522
+ public CustomAttrDiff ( in AttributeComparison comparison ) : base ( comparison , AttrDiffKind . Unspecified )
523
+ {
524
+ }
525
+ }
455
526
#endregion
456
527
}
0 commit comments