ApiApprovalTests.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. private readonly VerifySettings verifySettings;
  17. public ApiApprovalTests()
  18. : base()
  19. {
  20. verifySettings = new VerifySettings();
  21. verifySettings.UseExtension("cs");
  22. VerifierSettings.OnVerifyMismatch((filePair, message) => DiffPlexReporter.Report(filePair.Received, filePair.Verified, message));
  23. }
  24. [Fact]
  25. public Task Core()
  26. {
  27. var publicApi = GeneratePublicApi(typeof(System.Reactive.Unit).Assembly);
  28. return Verify(publicApi, verifySettings);
  29. }
  30. [Fact]
  31. public Task Aliases()
  32. {
  33. var publicApi = GeneratePublicApi(typeof(System.Reactive.Observable.Aliases.QueryLanguage).Assembly);
  34. return Verify(publicApi, verifySettings);
  35. }
  36. [Fact]
  37. public Task Testing()
  38. {
  39. var publicApi = GeneratePublicApi(typeof(Microsoft.Reactive.Testing.TestScheduler).Assembly);
  40. return Verify(publicApi, verifySettings);
  41. }
  42. private string GeneratePublicApi(Assembly assembly)
  43. {
  44. var namespacePrefixWhitelist = new[] { "System", "Microsoft" };
  45. return Filter(ApiGenerator.GeneratePublicApi(assembly, whitelistedNamespacePrefixes: namespacePrefixWhitelist));
  46. }
  47. private static string Filter(string text)
  48. {
  49. return string.Join(Environment.NewLine, text.Split(new[]
  50. {
  51. Environment.NewLine
  52. }, StringSplitOptions.RemoveEmptyEntries)
  53. .Where(l => !l.StartsWith("[assembly: AssemblyVersion("))
  54. .Where(l => !l.StartsWith("[assembly: AssemblyFileVersion("))
  55. .Where(l => !l.StartsWith("[assembly: AssemblyInformationalVersion("))
  56. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"CommitHash\""))
  57. .Where(l => !l.StartsWith("[assembly: System.Reflection.AssemblyMetadata(\"RepositoryUrl\""))
  58. .Where(l => !string.IsNullOrWhiteSpace(l))
  59. );
  60. }
  61. }
  62. }