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