ApiApprovalTests.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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;
  5. using ApprovalTests.Reporters;
  6. using PublicApiGenerator;
  7. using System;
  8. using System.Linq;
  9. using System.Reflection;
  10. using Xunit;
  11. using Xunit.Abstractions;
  12. namespace ReactiveTests.Tests.Api
  13. {
  14. #if DEBUG
  15. [UseReporter(typeof(DiffReporter))]
  16. #else
  17. [UseReporter(typeof(DiffPlexReporter))]
  18. #endif
  19. [IgnoreLineEndings(true)]
  20. public class ApiApprovalTests
  21. {
  22. public ApiApprovalTests(ITestOutputHelper output)
  23. {
  24. DiffPlexReporter.INSTANCE.Output = output;
  25. }
  26. [Fact]
  27. public void Core()
  28. {
  29. var publicApi = GeneratePublicApi(typeof(System.Reactive.Unit).Assembly);
  30. Approvals.Verify(new ApprovalTextWriter(publicApi, "cs"));
  31. }
  32. [Fact]
  33. public void Aliases()
  34. {
  35. var publicApi = GeneratePublicApi(typeof(System.Reactive.Observable.Aliases.QueryLanguage).Assembly);
  36. Approvals.Verify(new ApprovalTextWriter(publicApi, "cs"));
  37. }
  38. [Fact]
  39. public void Testing()
  40. {
  41. var publicApi = GeneratePublicApi(typeof(Microsoft.Reactive.Testing.TestScheduler).Assembly);
  42. Approvals.Verify(new ApprovalTextWriter(publicApi, "cs"));
  43. }
  44. private string GeneratePublicApi(Assembly assembly)
  45. {
  46. var namespacePrefixWhitelist = new[] { "System", "Microsoft" };
  47. return Filter(ApiGenerator.GeneratePublicApi(assembly, whitelistedNamespacePrefixes: namespacePrefixWhitelist));
  48. }
  49. private static string Filter(string text)
  50. {
  51. return string.Join(Environment.NewLine, text.Split(new[]
  52. {
  53. Environment.NewLine
  54. }, StringSplitOptions.RemoveEmptyEntries)
  55. .Where(l => !l.StartsWith("[assembly: AssemblyVersion("))
  56. .Where(l => !l.StartsWith("[assembly: AssemblyFileVersion("))
  57. .Where(l => !l.StartsWith("[assembly: AssemblyInformationalVersion("))
  58. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"CommitHash\""))
  59. .Where(l => !string.IsNullOrWhiteSpace(l))
  60. );
  61. }
  62. }
  63. }