|
| 1 | +public class BypassComparerTests |
| 2 | +{ |
| 3 | + // Mimics a converter that emits a canonical source target (eg a document) alongside a derived |
| 4 | + // target (eg a rendered image) whose comparer can mask a real difference in the source. |
| 5 | + static Target[] BuildTargets() => |
| 6 | + [ |
| 7 | + new("bypasssource", new MemoryStream("source-content"u8.ToArray())) |
| 8 | + { |
| 9 | + BypassComparersForSubsequentOnDifference = true |
| 10 | + }, |
| 11 | + new("bypassderived", new MemoryStream("derived-content"u8.ToArray())) |
| 12 | + ]; |
| 13 | + |
| 14 | + static int derivedCompareCount; |
| 15 | + |
| 16 | + // A comparer that masks every difference by always reporting equal. |
| 17 | + static Task<CompareResult> MaskingDerivedComparer(Stream received, Stream verified, IReadOnlyDictionary<string, object> context) |
| 18 | + { |
| 19 | + derivedCompareCount++; |
| 20 | + return Task.FromResult(CompareResult.Equal); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public async Task SourceEqual_DerivedComparerUsed() |
| 25 | + { |
| 26 | + derivedCompareCount = 0; |
| 27 | + // The source matches its verified file, so no bypass is triggered and the derived comparer |
| 28 | + // runs and masks the difference in the derived target. |
| 29 | + await Verify(null, BuildTargets()) |
| 30 | + .UseStreamComparer(MaskingDerivedComparer, "bypassderived") |
| 31 | + .DisableDiff(); |
| 32 | + Assert.Equal(1, derivedCompareCount); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public async Task SourceDiffers_DerivedComparerBypassed() |
| 37 | + { |
| 38 | + derivedCompareCount = 0; |
| 39 | + // The source differs from its verified file. Because it is flagged, the derived comparer is |
| 40 | + // bypassed (never invoked) and the otherwise-masked derived difference is surfaced. |
| 41 | + var exception = await Assert.ThrowsAsync<VerifyException>( |
| 42 | + () => Verify(null, BuildTargets()) |
| 43 | + .UseStreamComparer(MaskingDerivedComparer, "bypassderived") |
| 44 | + .DisableDiff()); |
| 45 | + Assert.Equal(0, derivedCompareCount); |
| 46 | + Assert.Contains("bypassderived", exception.Message); |
| 47 | + } |
| 48 | +} |
0 commit comments