ApiApprovalTests.cs 2.7 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 Verify;
  10. using VerifyXunit;
  11. using Xunit;
  12. using Xunit.Abstractions;
  13. namespace ReactiveTests.Tests.Api
  14. {
  15. public class ApiApprovalTests :
  16. VerifyBase
  17. {
  18. VerifySettings verifySettings;
  19. public ApiApprovalTests(ITestOutputHelper output) :
  20. base(output)
  21. {
  22. verifySettings = new VerifySettings();
  23. verifySettings.UseExtension("cs");
  24. verifySettings.OnVerifyMismatch(DiffPlexReporter.Report);
  25. }
  26. [Fact]
  27. public Task Core()
  28. {
  29. var publicApi = GeneratePublicApi(typeof(System.Reactive.Unit).Assembly);
  30. return Verify(publicApi, verifySettings);
  31. }
  32. [Fact]
  33. public Task Aliases()
  34. {
  35. var publicApi = GeneratePublicApi(typeof(System.Reactive.Observable.Aliases.QueryLanguage).Assembly);
  36. return Verify(publicApi, verifySettings);
  37. }
  38. [Fact]
  39. public Task Testing()
  40. {
  41. var publicApi = GeneratePublicApi(typeof(Microsoft.Reactive.Testing.TestScheduler).Assembly);
  42. return Verify(publicApi, verifySettings);
  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. }