ApiApprovalTests.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using PublicApiGenerator;
  5. using System;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. using VerifyTests;
  10. using VerifyXunit;
  11. using Xunit;
  12. namespace ReactiveTests.Tests.Api
  13. {
  14. public class ApiApprovalTests : VerifyBase
  15. {
  16. static ApiApprovalTests()
  17. {
  18. VerifierSettings.OnVerifyMismatch((filePair, message, autoVerify) => DiffPlexReporter.Report(filePair.ReceivedPath, filePair.VerifiedPath, message));
  19. }
  20. public ApiApprovalTests()
  21. : base()
  22. {
  23. }
  24. [Fact]
  25. public Task Core()
  26. {
  27. var publicApi = GeneratePublicApi(typeof(System.Reactive.Unit).Assembly);
  28. return Verify(publicApi, "cs");
  29. }
  30. [Fact]
  31. public Task Aliases()
  32. {
  33. var publicApi = GeneratePublicApi(typeof(System.Reactive.Observable.Aliases.QueryLanguage).Assembly);
  34. return Verify(publicApi, "cs");
  35. }
  36. [Fact]
  37. public Task Testing()
  38. {
  39. var publicApi = GeneratePublicApi(typeof(Microsoft.Reactive.Testing.TestScheduler).Assembly);
  40. return Verify(publicApi, "cs");
  41. }
  42. private string GeneratePublicApi(Assembly assembly)
  43. {
  44. ApiGeneratorOptions options = new()
  45. {
  46. AllowNamespacePrefixes = ["System", "Microsoft"]
  47. };
  48. return Filter(ApiGenerator.GeneratePublicApi(assembly, options));
  49. }
  50. private static string Filter(string text)
  51. {
  52. return string.Join(Environment.NewLine, text.Split(
  53. [
  54. Environment.NewLine
  55. ], StringSplitOptions.RemoveEmptyEntries)
  56. .Where(l => !l.StartsWith("[assembly: AssemblyVersion("))
  57. .Where(l => !l.StartsWith("[assembly: AssemblyFileVersion("))
  58. .Where(l => !l.StartsWith("[assembly: AssemblyInformationalVersion("))
  59. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"CommitHash\""))
  60. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"RepositoryUrl\""))
  61. .Where(l => !string.IsNullOrWhiteSpace(l))
  62. );
  63. }
  64. }
  65. }