DiffPlexReporter.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using ApprovalTests.Core;
  5. using DiffPlex;
  6. using DiffPlex.DiffBuilder;
  7. using DiffPlex.DiffBuilder.Model;
  8. using System.IO;
  9. using Xunit.Abstractions;
  10. namespace ReactiveTests.Tests
  11. {
  12. public class DiffPlexReporter : IApprovalFailureReporter
  13. {
  14. public static DiffPlexReporter INSTANCE = new DiffPlexReporter();
  15. public ITestOutputHelper Output { get; set; }
  16. public void Report(string approved, string received)
  17. {
  18. var approvedText = File.Exists(approved) ? File.ReadAllText(approved) : string.Empty;
  19. var receivedText = File.ReadAllText(received);
  20. var diffBuilder = new InlineDiffBuilder(new Differ());
  21. var diff = diffBuilder.BuildDiffModel(approvedText, receivedText);
  22. foreach (var line in diff.Lines)
  23. {
  24. if (line.Type == ChangeType.Unchanged) continue;
  25. var prefix = " ";
  26. switch (line.Type)
  27. {
  28. case ChangeType.Inserted:
  29. prefix = "+ ";
  30. break;
  31. case ChangeType.Deleted:
  32. prefix = "- ";
  33. break;
  34. }
  35. Output.WriteLine("{0}{1}", prefix, line.Text);
  36. }
  37. }
  38. }
  39. }